Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 54
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
PluginRequirements
0.00% covered (danger)
0.00%
0 / 54
0.00% covered (danger)
0.00%
0 / 5
870
0.00% covered (danger)
0.00%
0 / 1
 get
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 get_patterns_requirements
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 add_logo
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
 get_logo_for_plugin
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
42
 enhance_requirements
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
272
1<?php
2namespace NewfoldLabs\WP\Module\Patterns\Data;
3
4/**
5 * Class PluginRequirements
6 *
7 * Manages plugin dependencies in the Patterns module.
8 * This class provides functionality to retrieve, enhance, and process plugin
9 * requirements for different pattern types, adding additional information
10 * such as logos and premium status.
11 */
12final class PluginRequirements {
13
14    /**
15     * An array of pattern-specific plugin requirements.
16     *
17     * @var array
18     */
19    protected static $patterns = array(
20        'form-1'     => array(
21            array(
22                'slug'        => 'jetpack',
23                'name'        => 'Jetpack',
24                'path'        => 'jetpack/jetpack.php',
25                'url'         => 'https://wordpress.org/plugins/jetpack/',
26                'description' => 'This plugin is essential for adding the contact form to this pattern!',
27            ),
28        ),
29        'form-2'     => array(
30            array(
31                'slug'        => 'jetpack',
32                'name'        => 'Jetpack',
33                'path'        => 'jetpack/jetpack.php',
34                'url'         => 'https://wordpress.org/plugins/jetpack/',
35                'description' => 'This plugin is essential for adding the contact form to this pattern!',
36            ),
37        ),
38        'form-3'     => array(
39            array(
40                'slug'        => 'jetpack',
41                'name'        => 'Jetpack',
42                'path'        => 'jetpack/jetpack.php',
43                'url'         => 'https://wordpress.org/plugins/jetpack/',
44                'description' => 'This plugin is essential for adding the contact form to this pattern!',
45            ),
46        ),
47        'form-4'     => array(
48            array(
49                'slug'        => 'jetpack',
50                'name'        => 'Jetpack',
51                'path'        => 'jetpack/jetpack.php',
52                'url'         => 'https://wordpress.org/plugins/jetpack/',
53                'description' => 'This plugin is essential for adding the contact form to this pattern!',
54            ),
55        ),
56        'form-5'     => array(
57            array(
58                'slug'        => 'jetpack',
59                'name'        => 'Jetpack',
60                'path'        => 'jetpack/jetpack.php',
61                'url'         => 'https://wordpress.org/plugins/jetpack/',
62                'description' => 'This plugin is essential for adding the contact form to this pattern!',
63            ),
64        ),
65        'products-1' => array(
66            array(
67                'slug'        => 'woocommerce',
68                'name'        => 'WooCommerce',
69                'path'        => 'woocommerce/woocommerce.php',
70                'url'         => 'https://wordpress.org/plugins/woocommerce/',
71                'description' => 'This plugin is essential for adding the product grid to this pattern!',
72            ),
73        ),
74        'products-2' => array(
75            array(
76                'slug'        => 'woocommerce',
77                'name'        => 'WooCommerce',
78                'path'        => 'woocommerce/woocommerce.php',
79                'url'         => 'https://wordpress.org/plugins/woocommerce/',
80                'description' => 'This plugin is essential for adding the product grid to this pattern!',
81            ),
82        ),
83        'products-3' => array(
84            array(
85                'slug'        => 'woocommerce',
86                'name'        => 'WooCommerce',
87                'path'        => 'woocommerce/woocommerce.php',
88                'url'         => 'https://wordpress.org/plugins/woocommerce/',
89                'description' => 'This plugin is essential for adding the product grid to this pattern!',
90            ),
91        ),
92        'products-4' => array(
93            array(
94                'slug'        => 'woocommerce',
95                'name'        => 'WooCommerce',
96                'path'        => 'woocommerce/woocommerce.php',
97                'url'         => 'https://wordpress.org/plugins/woocommerce/',
98                'description' => 'This plugin is essential for adding the product grid to this pattern!',
99            ),
100        ),
101        'products-5' => array(
102            array(
103                'slug'        => 'woocommerce',
104                'name'        => 'WooCommerce',
105                'path'        => 'woocommerce/woocommerce.php',
106                'url'         => 'https://wordpress.org/plugins/woocommerce/',
107                'description' => 'This plugin is essential for adding the product grid to this pattern!',
108            ),
109        ),
110        'products-6' => array(
111            array(
112                'slug'        => 'woocommerce',
113                'name'        => 'WooCommerce',
114                'path'        => 'woocommerce/woocommerce.php',
115                'url'         => 'https://wordpress.org/plugins/woocommerce/',
116                'description' => 'This plugin is essential for adding the product grid to this pattern!',
117            ),
118        ),
119        'table-of-contents-1' => array(
120            array(
121                'slug'        => 'yoast-seo-wordpress-premium',
122                'name'        => 'Yoast SEO',
123                'path'        => 'wordpress-seo-premium/wp-seo-premium.php',
124                'url'         => 'https://yoast.com',
125                'description' => 'SEO made easy! Improve your ranking in search engines, boost performance and visibility, and get 24/7 premium support.',
126                'isPremium'   => true,
127                'ctbId'       => '57d6a568-783c-45e2-a388-847cff155897',
128            ),
129        ),
130        'table-of-contents-2' => array(
131            array(
132                'slug'        => 'yoast-seo-wordpress-premium',
133                'name'        => 'Yoast SEO',
134                'path'        => 'wordpress-seo-premium/wp-seo-premium.php',
135                'url'         => 'https://yoast.com',
136                'description' => 'SEO made easy! Improve your ranking in search engines, boost performance and visibility, and get 24/7 premium support.',
137                'isPremium'   => true,
138                'ctbId'       => '57d6a568-783c-45e2-a388-847cff155897',
139            ),
140        ),
141        'table-of-contents-3' => array(
142            array(
143                'slug'        => 'yoast-seo-wordpress-premium',
144                'name'        => 'Yoast SEO',
145                'path'        => 'wordpress-seo-premium/wp-seo-premium.php',
146                'url'         => 'https://yoast.com',
147                'description' => 'SEO made easy! Improve your ranking in search engines, boost performance and visibility, and get 24/7 premium support.',
148                'isPremium'   => true,
149                'ctbId'       => '57d6a568-783c-45e2-a388-847cff155897',
150            ),
151        ),
152        'header-20'  => array(
153            array(
154                'slug'        => 'woocommerce',
155                'name'        => 'WooCommerce',
156                'path'        => 'woocommerce/woocommerce.php',
157                'url'         => 'https://wordpress.org/plugins/woocommerce/',
158                'description' => 'This plugin is essential for adding the product grid to this pattern!',
159            ),
160        ),
161        'header-21'  => array(
162            array(
163                'slug'        => 'woocommerce',
164                'name'        => 'WooCommerce',
165                'path'        => 'woocommerce/woocommerce.php',
166                'url'         => 'https://wordpress.org/plugins/woocommerce/',
167                'description' => 'This plugin is essential for adding the product grid to this pattern!',
168            ),
169        ),
170        'header-22'  => array(
171            array(
172                'slug'        => 'woocommerce',
173                'name'        => 'WooCommerce',
174                'path'        => 'woocommerce/woocommerce.php',
175                'url'         => 'https://wordpress.org/plugins/woocommerce/',
176                'description' => 'This plugin is essential for adding the product grid to this pattern!',
177            ),
178        ),
179    );
180
181    /**
182     * Retrieves plugin requirements for a specific type.
183     *
184     * @param string $type The type of requirements to retrieve. Defaults to 'patterns'.
185     *
186     * @return array The array of plugin requirements for the specified type.
187     */
188    public static function get( $type = 'patterns' ) {
189
190        if ( 'patterns' === $type ) {
191            return self::get_patterns_requirements();
192        }
193
194        return array();
195    }
196
197    /**
198     * Retrieves the plugin requirements for patterns.
199     *
200     * @return array The array of plugin requirements for patterns.
201     */
202    private static function get_patterns_requirements() {
203
204        $patterns = apply_filters( 'nfd_wb_patterns_plugin_requirements', self::$patterns );
205
206        $patterns = array_map(
207            function ( $pattern ) {
208                $pattern = self::enhance_requirements( $pattern );
209                $pattern = self::add_logo( $pattern );
210                return $pattern;
211            },
212            $patterns
213        );
214
215        return $patterns;
216    }
217
218    /**
219     * Adds a logo identifier to each plugin requirement in a pattern.
220     *
221     * @param array $pattern An array of plugin requirements for a specific pattern.
222     *               Each requirement is an associative array containing plugin details.
223     *
224     * @return array The pattern with logo identifiers added to each plugin requirement.
225     */
226    private static function add_logo( $pattern ) {
227        foreach ( $pattern as $key => $requirement ) {
228            $slug     = isset( $requirement['slug'] ) ? $requirement['slug'] : '';
229            $pls_slug = isset( $requirement['plsSlug'] ) ? $requirement['plsSlug'] : '';
230
231            $pattern[ $key ]['logo'] = self::get_logo_for_plugin( $slug, $pls_slug );
232        }
233
234        return $pattern;
235    }
236
237    /**
238     * Get the logo identifier for a plugin based on its slug.
239     *
240     * @param string $slug    The plugin slug.
241     * @param string $pls_slug Optional PLS slug.
242     *
243     * @return string The logo identifier or empty string if no match.
244     */
245    private static function get_logo_for_plugin( $slug, $pls_slug = '' ) {
246        $known_logos = array(
247            'yoast'       => array( 'yoast' ),
248            'yith'        => array( 'yith' ),
249            'jetpack'     => array( 'jetpack' ),
250            'woocommerce' => array( 'woocommerce', 'wc-' ),
251        );
252
253        foreach ( $known_logos as $logo => $patterns ) {
254            foreach ( $patterns as $pattern ) {
255                if ( strpos( $slug, $pattern ) !== false || ( $pls_slug && strpos( $pls_slug, $pattern ) !== false ) ) {
256                    return $logo;
257                }
258            }
259        }
260
261        return '';
262    }
263
264    /**
265     * Enhances the plugin requirements with additional information.
266     *
267     * @param array $requirements The original plugin requirements.
268     *
269     * @return array The enhanced plugin requirements.
270     */
271    private static function enhance_requirements( $requirements ) {
272
273        $solutions = get_transient( 'newfold_solutions' );
274
275        if ( ! $solutions || empty( $solutions['entitlements'] ) ) {
276            return $requirements;
277        }
278
279        $entitlements = isset( $solutions['entitlements'] ) ? $solutions['entitlements'] : array();
280
281        foreach ( $requirements as $key => $requirement ) {
282
283            $slug = isset( $requirement['slug'] ) ? $requirement['slug'] : '';
284
285            if ( ! $slug ) {
286                continue;
287            }
288
289            if ( ! isset( $requirement['isPremium'] ) || ( isset( $requirement['isPremium'] ) && ! $requirement['isPremium'] ) ) {
290                continue;
291            }
292
293            if ( ! empty( $entitlements ) ) {
294                foreach ( $entitlements as $entitlement ) {
295                    $entitlement_slug     = isset( $entitlement['slug'] ) ? $entitlement['slug'] : '';
296                    $entitlement_pls_slug = isset( $entitlement['plsSlug'] ) ? $entitlement['plsSlug'] : '';
297
298                    if ( $slug === $entitlement_slug || $slug === $entitlement_pls_slug ) {
299                        $requirements[ $key ] = array_merge(
300                            $requirement,
301                            $entitlement,
302                            array(
303                                'isPremium'  => $requirement['isPremium'] ?? false,
304                                'isEntitled' => true,
305                                'ctbId'      => '',
306                            )
307                        );
308                    }
309                }
310            }
311        }
312
313        return $requirements;
314    }
315}