PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.3.2
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.3.2
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / vendor / wp-media / plugin-family / src / Model / PluginFamily.php

PluginFamily.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 2.3.2, at vendor/wp-media/plugin-family/src/Model/PluginFamily.php

172 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Imagify\Dependencies\WPMedia\PluginFamily\Model;
4
5 /**
6 * Handles the data to be passed to the frontend.
7 */
8 class PluginFamily {
9
10 /**
11 * An array of referrers for wp rocket.
12 *
13 * @var array
14 */
15 protected $wp_rocket_referrer = [
16 'imagify' => 'imagify',
17 'seo-by-rank-math' => '',
18 'backwpup' => '',
19 'uk-cookie-consent' => '',
20 ];
21
22 /**
23 * Get filtered plugins.
24 *
25 * @param string $main_plugin Main plugin installed.
26 *
27 * @return array
28 */
29 public function get_filtered_plugins( string $main_plugin ): array {
30 $plugins = require_once 'wp_media_plugins.php';
31
32 return $this->filter_plugins_by_activation( $plugins, $main_plugin );
33 }
34
35 /**
36 * Filter plugins family data by activation status and returns both categorized and uncategorized format.
37 *
38 * @param array $plugins Array of family plugins.
39 * @param string $main_plugin Main plugin installed.
40 *
41 * @return array
42 */
43 public function filter_plugins_by_activation( array $plugins, string $main_plugin ): array {
44 if ( empty( $plugins ) ) {
45 return [];
46 }
47
48 list( $active_plugins, $inactive_plugins ) = [ [], [] ];
49
50 foreach ( $plugins as $cat => $cat_data ) {
51 foreach ( $cat_data['plugins'] as $plugin => $data ) {
52
53 $plugin_path = $plugin . '.php';
54 $plugin_slug = dirname( $plugin );
55 $main_plugin_slug = dirname( $main_plugin );
56 $wpr_referrer = 'wp-rocket' !== $main_plugin_slug ? $this->wp_rocket_referrer[ $main_plugin_slug ] : '';
57
58 /**
59 * Check for activated plugins and pop them out of the array
60 * to re-add them back using array_merge to be displayed after
61 * plugins that are not installed or not activated.
62 */
63 if ( is_plugin_active( $plugin_path ) && $main_plugin . '.php' !== $plugin_path ) {
64 // set cta data of active plugins.
65 $plugins[ $cat ]['plugins'][ $plugin ]['cta'] = [
66 'text' => __( 'Activated', 'imagify' ),
67 'url' => '#',
68 ];
69
70 // Send active plugin to new array.
71 $active_plugins[ $plugin ] = $plugins[ $cat ]['plugins'][ $plugin ];
72
73 // Remove active plugin from current category.
74 $active_plugin = $plugins[ $cat ]['plugins'][ $plugin ];
75 unset( $plugins[ $cat ]['plugins'][ $plugin ] );
76
77 // Send active plugin to the end of array in current category.
78 $plugins[ $cat ]['plugins'][ $plugin ] = $active_plugin;
79
80 // Remove category with active plugin from current array.
81 $active_cat = $plugins[ $cat ];
82 unset( $plugins[ $cat ] );
83
84 // Send category with active plugins to the end of array.
85 $plugins[ $cat ] = $active_cat;
86 continue;
87 }
88
89 $install_activate_url = admin_url( 'admin-post.php' );
90
91 $args = [
92 'action' => 'plugin_family_install_' . $plugin_slug,
93 '_wpnonce' => wp_create_nonce( 'plugin_family_install_' . $plugin_slug ),
94 'plugin_to_install' => rawurlencode( $plugin ),
95 ];
96
97 if ( 'imagify' === $plugin_slug ) {
98 $args = [
99 'action' => 'install_imagify_from_partner_' . $main_plugin_slug,
100 '_wpnonce' => wp_create_nonce( 'install_imagify_from_partner' ),
101 '_wp_http_referer' => rawurlencode( $this->get_current_url() ),
102 ];
103 }
104
105 $install_activate_url = add_query_arg( $args, $install_activate_url );
106
107 // Set Installation link.
108 $plugins[ $cat ]['plugins'][ $plugin ]['cta'] = [
109 'text' => __( 'Install', 'imagify' ),
110 'url' => $install_activate_url,
111 ];
112
113 // Create unique CTA data for WP Rocket.
114 if ( 'wp-rocket/wp-rocket' === $plugin ) {
115 $url = 'https://wp-rocket.me/?utm_source=' . $wpr_referrer . '-coupon&utm_medium=plugin&utm_campaign=' . $wpr_referrer;
116
117 $plugins[ $cat ]['plugins'][ $plugin ]['cta'] = [
118 'text' => __( 'Get it Now', 'imagify' ),
119 'url' => $url,
120 ];
121
122 $plugins[ $cat ]['plugins'][ $plugin ]['link'] = $url;
123 }
124
125 // Set activation text.
126 if ( file_exists( WP_PLUGIN_DIR . '/' . $plugin_path ) ) {
127 $plugins[ $cat ]['plugins'][ $plugin ]['cta']['text'] = __( 'Activate', 'imagify' );
128
129 if ( 'wp-rocket/wp-rocket' === $plugin ) {
130 $plugins[ $cat ]['plugins'][ $plugin ]['cta']['url'] = $install_activate_url;
131 }
132 }
133
134 // Send inactive plugins to new array.
135 $inactive_plugins[ $plugin ] = $plugins[ $cat ]['plugins'][ $plugin ];
136 }
137
138 // Remove main plugin from categorized array.
139 if ( isset( $plugins[ $cat ]['plugins'][ $main_plugin ] ) ) {
140 unset( $plugins[ $cat ]['plugins'][ $main_plugin ] );
141 }
142 }
143
144 $uncategorized = array_merge( $inactive_plugins, $active_plugins );
145 // Remove main plugin from uncategorized array.
146 unset( $uncategorized[ $main_plugin ] );
147
148 return [
149 'categorized' => $plugins,
150 'uncategorized' => $uncategorized,
151 ];
152 }
153
154 /**
155 * Get the current URL.
156 * Gotten from Imagify_Partner Package.
157 *
158 * @return string
159 */
160 protected function get_current_url(): string {
161 if ( ! isset( $_SERVER['SERVER_PORT'], $_SERVER['HTTP_HOST'] ) ) {
162 return '';
163 }
164
165 $port = (int) wp_unslash( $_SERVER['SERVER_PORT'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
166 $port = 80 !== $port && 443 !== $port ? ( ':' . $port ) : '';
167 $url = ! empty( $GLOBALS['HTTP_SERVER_VARS']['REQUEST_URI'] ) ? $GLOBALS['HTTP_SERVER_VARS']['REQUEST_URI'] : ( ! empty( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : '' ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
168
169 return 'http' . ( is_ssl() ? 's' : '' ) . '://' . $_SERVER['HTTP_HOST'] . $port . $url; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
170 }
171 }
172