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        'form-6'     => array(
66            array(
67                'slug'        => 'jetpack',
68                'name'        => 'Jetpack',
69                'path'        => 'jetpack/jetpack.php',
70                'url'         => 'https://wordpress.org/plugins/jetpack/',
71                'description' => 'This plugin is essential for adding the contact form to this pattern!',
72            ),
73        ),
74        'form-7'     => array(
75            array(
76                'slug'        => 'jetpack',
77                'name'        => 'Jetpack',
78                'path'        => 'jetpack/jetpack.php',
79                'url'         => 'https://wordpress.org/plugins/jetpack/',
80                'description' => 'This plugin is essential for adding the contact form to this pattern!',
81            ),
82        ),
83        'products-1' => 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-2' => 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-3' => 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-4' => 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        'products-5' => array(
120            array(
121                'slug'        => 'woocommerce',
122                'name'        => 'WooCommerce',
123                'path'        => 'woocommerce/woocommerce.php',
124                'url'         => 'https://wordpress.org/plugins/woocommerce/',
125                'description' => 'This plugin is essential for adding the product grid to this pattern!',
126            ),
127        ),
128        'products-6' => array(
129            array(
130                'slug'        => 'woocommerce',
131                'name'        => 'WooCommerce',
132                'path'        => 'woocommerce/woocommerce.php',
133                'url'         => 'https://wordpress.org/plugins/woocommerce/',
134                'description' => 'This plugin is essential for adding the product grid to this pattern!',
135            ),
136        ),
137        'products-7' => array(
138            array(
139                'slug'        => 'woocommerce',
140                'name'        => 'WooCommerce',
141                'path'        => 'woocommerce/woocommerce.php',
142                'url'         => 'https://wordpress.org/plugins/woocommerce/',
143                'description' => 'This plugin is essential for adding the product grid to this pattern!',
144            ),
145        ),
146        'table-of-contents-1' => array(
147            array(
148                'slug'        => 'yoast-seo-wordpress-premium',
149                'name'        => 'Yoast SEO',
150                'path'        => 'wordpress-seo-premium/wp-seo-premium.php',
151                'url'         => 'https://yoast.com',
152                'description' => 'SEO made easy! Improve your ranking in search engines, boost performance and visibility, and get 24/7 premium support.',
153                'isPremium'   => true,
154                'ctbId'       => '57d6a568-783c-45e2-a388-847cff155897',
155            ),
156        ),
157        'table-of-contents-2' => array(
158            array(
159                'slug'        => 'yoast-seo-wordpress-premium',
160                'name'        => 'Yoast SEO',
161                'path'        => 'wordpress-seo-premium/wp-seo-premium.php',
162                'url'         => 'https://yoast.com',
163                'description' => 'SEO made easy! Improve your ranking in search engines, boost performance and visibility, and get 24/7 premium support.',
164                'isPremium'   => true,
165                'ctbId'       => '57d6a568-783c-45e2-a388-847cff155897',
166            ),
167        ),
168        'table-of-contents-3' => array(
169            array(
170                'slug'        => 'yoast-seo-wordpress-premium',
171                'name'        => 'Yoast SEO',
172                'path'        => 'wordpress-seo-premium/wp-seo-premium.php',
173                'url'         => 'https://yoast.com',
174                'description' => 'SEO made easy! Improve your ranking in search engines, boost performance and visibility, and get 24/7 premium support.',
175                'isPremium'   => true,
176                'ctbId'       => '57d6a568-783c-45e2-a388-847cff155897',
177            ),
178        ),
179        'header-20'  => array(
180            array(
181                'slug'        => 'woocommerce',
182                'name'        => 'WooCommerce',
183                'path'        => 'woocommerce/woocommerce.php',
184                'url'         => 'https://wordpress.org/plugins/woocommerce/',
185                'description' => 'This plugin is essential for adding the product grid to this pattern!',
186            ),
187        ),
188        'header-21'  => array(
189            array(
190                'slug'        => 'woocommerce',
191                'name'        => 'WooCommerce',
192                'path'        => 'woocommerce/woocommerce.php',
193                'url'         => 'https://wordpress.org/plugins/woocommerce/',
194                'description' => 'This plugin is essential for adding the product grid to this pattern!',
195            ),
196        ),
197        'header-22'  => array(
198            array(
199                'slug'        => 'woocommerce',
200                'name'        => 'WooCommerce',
201                'path'        => 'woocommerce/woocommerce.php',
202                'url'         => 'https://wordpress.org/plugins/woocommerce/',
203                'description' => 'This plugin is essential for adding the product grid to this pattern!',
204            ),
205        ),
206        'header-26'  => array(
207            array(
208                'slug'        => 'woocommerce',
209                'name'        => 'WooCommerce',
210                'path'        => 'woocommerce/woocommerce.php',
211                'url'         => 'https://wordpress.org/plugins/woocommerce/',
212                'description' => 'This plugin is essential for adding the product grid to this pattern!',
213            ),
214        ),
215        'header-28'  => array(
216            array(
217                'slug'        => 'woocommerce',
218                'name'        => 'WooCommerce',
219                'path'        => 'woocommerce/woocommerce.php',
220                'url'         => 'https://wordpress.org/plugins/woocommerce/',
221                'description' => 'This plugin is essential for adding the product grid to this pattern!',
222            ),
223        ),
224        'header-31'  => array(
225            array(
226                'slug'        => 'woocommerce',
227                'name'        => 'WooCommerce',
228                'path'        => 'woocommerce/woocommerce.php',
229                'url'         => 'https://wordpress.org/plugins/woocommerce/',
230                'description' => 'This plugin is essential for adding the product grid to this pattern!',
231            ),
232        ),
233    );
234
235    /**
236     * Retrieves plugin requirements for a specific type.
237     *
238     * @param string $type The type of requirements to retrieve. Defaults to 'patterns'.
239     *
240     * @return array The array of plugin requirements for the specified type.
241     */
242    public static function get( $type = 'patterns' ) {
243
244        if ( 'patterns' === $type ) {
245            return self::get_patterns_requirements();
246        }
247
248        return array();
249    }
250
251    /**
252     * Retrieves the plugin requirements for patterns.
253     *
254     * @return array The array of plugin requirements for patterns.
255     */
256    private static function get_patterns_requirements() {
257
258        $patterns = apply_filters( 'nfd_wb_patterns_plugin_requirements', self::$patterns );
259
260        $patterns = array_map(
261            function ( $pattern ) {
262                $pattern = self::enhance_requirements( $pattern );
263                $pattern = self::add_logo( $pattern );
264                return $pattern;
265            },
266            $patterns
267        );
268
269        return $patterns;
270    }
271
272    /**
273     * Adds a logo identifier to each plugin requirement in a pattern.
274     *
275     * @param array $pattern An array of plugin requirements for a specific pattern.
276     *               Each requirement is an associative array containing plugin details.
277     *
278     * @return array The pattern with logo identifiers added to each plugin requirement.
279     */
280    private static function add_logo( $pattern ) {
281        foreach ( $pattern as $key => $requirement ) {
282            $slug     = isset( $requirement['slug'] ) ? $requirement['slug'] : '';
283            $pls_slug = isset( $requirement['plsSlug'] ) ? $requirement['plsSlug'] : '';
284
285            $pattern[ $key ]['logo'] = self::get_logo_for_plugin( $slug, $pls_slug );
286        }
287
288        return $pattern;
289    }
290
291    /**
292     * Get the logo identifier for a plugin based on its slug.
293     *
294     * @param string $slug    The plugin slug.
295     * @param string $pls_slug Optional PLS slug.
296     *
297     * @return string The logo identifier or empty string if no match.
298     */
299    private static function get_logo_for_plugin( $slug, $pls_slug = '' ) {
300        $known_logos = array(
301            'yoast'       => array( 'yoast' ),
302            'yith'        => array( 'yith' ),
303            'jetpack'     => array( 'jetpack' ),
304            'woocommerce' => array( 'woocommerce', 'wc-' ),
305        );
306
307        foreach ( $known_logos as $logo => $patterns ) {
308            foreach ( $patterns as $pattern ) {
309                if ( strpos( $slug, $pattern ) !== false || ( $pls_slug && strpos( $pls_slug, $pattern ) !== false ) ) {
310                    return $logo;
311                }
312            }
313        }
314
315        return '';
316    }
317
318    /**
319     * Enhances the plugin requirements with additional information.
320     *
321     * @param array $requirements The original plugin requirements.
322     *
323     * @return array The enhanced plugin requirements.
324     */
325    private static function enhance_requirements( $requirements ) {
326
327        $solutions = get_transient( 'newfold_solutions' );
328
329        if ( ! $solutions || empty( $solutions['entitlements'] ) ) {
330            return $requirements;
331        }
332
333        $entitlements = isset( $solutions['entitlements'] ) ? $solutions['entitlements'] : array();
334
335        foreach ( $requirements as $key => $requirement ) {
336
337            $slug = isset( $requirement['slug'] ) ? $requirement['slug'] : '';
338
339            if ( ! $slug ) {
340                continue;
341            }
342
343            if ( ! isset( $requirement['isPremium'] ) || ( isset( $requirement['isPremium'] ) && ! $requirement['isPremium'] ) ) {
344                continue;
345            }
346
347            if ( ! empty( $entitlements ) ) {
348                foreach ( $entitlements as $entitlement ) {
349                    $entitlement_slug     = isset( $entitlement['slug'] ) ? $entitlement['slug'] : '';
350                    $entitlement_pls_slug = isset( $entitlement['plsSlug'] ) ? $entitlement['plsSlug'] : '';
351
352                    if ( $slug === $entitlement_slug || $slug === $entitlement_pls_slug ) {
353                        $requirements[ $key ] = array_merge(
354                            $requirement,
355                            $entitlement,
356                            array(
357                                'isPremium'  => $requirement['isPremium'] ?? false,
358                                'isEntitled' => true,
359                                'ctbId'      => '',
360                            )
361                        );
362                    }
363                }
364            }
365        }
366
367        return $requirements;
368    }
369}