Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
81.54% covered (warning)
81.54%
53 / 65
76.92% covered (warning)
76.92%
10 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
Snap_Marketing_Treatment_Catalog
84.13% covered (warning)
84.13%
53 / 63
76.92% covered (warning)
76.92%
10 / 13
38.62
0.00% covered (danger)
0.00%
0 / 1
 types
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 shortcode_slugs
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 estimates
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
2
 alignments
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 active_states
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 is_valid_type
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 is_valid_shortcode_slug
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 is_valid_estimate
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 is_valid_alignment
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 normalize_alignment
63.64% covered (warning)
63.64%
7 / 11
0.00% covered (danger)
0.00%
0 / 1
11.08
 slug_to_type
62.50% covered (warning)
62.50%
5 / 8
0.00% covered (danger)
0.00%
0 / 1
6.32
 type_to_slug
62.50% covered (warning)
62.50%
5 / 8
0.00% covered (danger)
0.00%
0 / 1
6.32
 shortcode_tag
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * Pure helpers for Snap Marketing treatment catalogs (unit-testable).
4 *
5 * @package snap_marketing
6 */
7
8if ( ! defined( 'ABSPATH' ) && ! defined( 'SNAP_MARKETING_TESTING' ) ) {
9    exit;
10}
11
12/**
13 * Treatment type, estimate, and alignment catalogs.
14 */
15class Snap_Marketing_Treatment_Catalog {
16
17    /**
18     * Admin treatment type map (code => label).
19     *
20     * @return array<string,string>
21     */
22    public static function types() {
23        return array(
24            'PRE_APPROVAL' => 'Get Approved',
25            'AS_LOW_AS'    => 'Get Approved – As low as',
26            'BANNER'       => 'Banner',
27        );
28    }
29
30    /**
31     * Front-end shortcode treatment type slugs.
32     *
33     * @return string[]
34     */
35    public static function shortcode_slugs() {
36        return array(
37            'get_approved_as_low_as',
38            'get_approved',
39            'banner',
40        );
41    }
42
43    /**
44     * Payment estimate frequency codes.
45     *
46     * @param bool $admin_labels When true, use admin wording for Weekly.
47     * @return array<string,string>
48     */
49    public static function estimates( $admin_labels = false ) {
50        if ( $admin_labels ) {
51            return array(
52                'BW' => 'Bi Weekly',
53                'W'  => 'Weekly',
54                'M'  => 'Monthly',
55                'MW' => 'Monthly Week',
56                'SM' => 'Semi Monthly',
57            );
58        }
59
60        return array(
61            'BW' => 'Bi Weekly',
62            'W'  => 'week',
63            'M'  => 'Monthly',
64            'MW' => 'Monthly Week',
65            'SM' => 'Semi Monthly',
66        );
67    }
68
69    /**
70     * Alignment options.
71     *
72     * @return string[]
73     */
74    public static function alignments() {
75        return array( 'Left', 'Center', 'Right' );
76    }
77
78    /**
79     * Active flag options.
80     *
81     * @return string[]
82     */
83    public static function active_states() {
84        return array( 'Enable', 'Disable' );
85    }
86
87    /**
88     * Whether a treatment type code is supported.
89     *
90     * @param string $type Type code.
91     * @return bool
92     */
93    public static function is_valid_type( $type ) {
94        $types = self::types();
95        return is_string( $type ) && isset( $types[ $type ] );
96    }
97
98    /**
99     * Whether a shortcode slug is supported.
100     *
101     * @param string $slug Shortcode slug.
102     * @return bool
103     */
104    public static function is_valid_shortcode_slug( $slug ) {
105        return is_string( $slug ) && in_array( $slug, self::shortcode_slugs(), true );
106    }
107
108    /**
109     * Whether an estimate frequency code is supported.
110     *
111     * @param string $code Frequency code.
112     * @return bool
113     */
114    public static function is_valid_estimate( $code ) {
115        $estimates = self::estimates( false );
116        return is_string( $code ) && isset( $estimates[ $code ] );
117    }
118
119    /**
120     * Whether an alignment value is supported.
121     *
122     * @param string $alignment Alignment.
123     * @return bool
124     */
125    public static function is_valid_alignment( $alignment ) {
126        return is_string( $alignment ) && in_array( $alignment, self::alignments(), true );
127    }
128
129    /**
130     * Normalize alignment to a known value.
131     *
132     * @param string $alignment Raw alignment.
133     * @return string
134     */
135    public static function normalize_alignment( $alignment ) {
136        if ( self::is_valid_alignment( $alignment ) ) {
137            return $alignment;
138        }
139
140        $lower = is_string( $alignment ) ? strtolower( trim( $alignment ) ) : '';
141        switch ( $lower ) {
142            case 'left':
143                return 'Left';
144            case 'right':
145                return 'Right';
146            case 'center':
147            case 'centre':
148                return 'Center';
149            default:
150                return 'Left';
151        }
152    }
153
154    /**
155     * Map a shortcode slug to an admin treatment type code.
156     *
157     * @param string $slug Shortcode slug.
158     * @return string|null
159     */
160    public static function slug_to_type( $slug ) {
161        switch ( (string) $slug ) {
162            case 'get_approved':
163                return 'PRE_APPROVAL';
164            case 'get_approved_as_low_as':
165                return 'AS_LOW_AS';
166            case 'banner':
167                return 'BANNER';
168            default:
169                return null;
170        }
171    }
172
173    /**
174     * Map an admin treatment type code to a shortcode slug.
175     *
176     * @param string $type Type code.
177     * @return string|null
178     */
179    public static function type_to_slug( $type ) {
180        switch ( (string) $type ) {
181            case 'PRE_APPROVAL':
182                return 'get_approved';
183            case 'AS_LOW_AS':
184                return 'get_approved_as_low_as';
185            case 'BANNER':
186                return 'banner';
187            default:
188                return null;
189        }
190    }
191
192    /**
193     * Build the shortcode tag for a slug.
194     *
195     * @param string $slug Shortcode slug.
196     * @return string|null
197     */
198    public static function shortcode_tag( $slug ) {
199        if ( ! self::is_valid_shortcode_slug( $slug ) ) {
200            return null;
201        }
202        return 'snap_treatment_add_' . $slug;
203    }
204}