class-supported-plugins.php (4791B)
1 <?php // phpcs:ignore WordPress.Files.FileName 2 3 /** 4 * Detect supported plugins with the given instance. 5 * 6 * @since 4.0.0 7 * @package Redux Framework 8 */ 9 10 namespace ReduxTemplates; 11 12 if ( ! defined( 'ABSPATH' ) ) { 13 exit; // Exit if accessed directly. 14 } 15 16 /** 17 * Redux Templates Supported_Plugins Class 18 * 19 * @since 4.0.0 20 */ 21 class Supported_Plugins { 22 23 /** 24 * List of all supported plugins from the library. 25 * 26 * @var array|null 27 */ 28 protected static $plugins = array(); 29 /** 30 * List of all supported plugins from the library. 31 * 32 * @var Supported_Plugins|null 33 */ 34 protected static $instance = null; 35 36 /** 37 * Return or generate instance, singleton. 38 * 39 * @return object Instance 40 * @since 4.0.0 41 */ 42 public static function instance() { 43 if ( is_null( self::$instance ) ) { 44 self::$instance = new self(); 45 } 46 47 return self::$instance; 48 } 49 50 /** 51 * Include the template 52 * 53 * @param array|null $plugins List of all possible plugins from the library. 54 * 55 * @return void 56 * @since 4.0.0 57 */ 58 public function init( $plugins = array() ) { 59 self::$plugins = $plugins; 60 self::detect_versions(); 61 } 62 63 /** 64 * Detect all versions of plugins had in library and installed locally. 65 * 66 * @return void 67 * @since 4.0.0 68 */ 69 private static function detect_versions() { 70 $all_plugins = get_plugins(); 71 72 $active_plugins = get_option( 'active_plugins' ); 73 74 $data = array(); 75 foreach ( $active_plugins as $plugin ) { 76 $slug = explode( '/', $plugin )[0]; 77 if ( ! isset( $all_plugins[ $plugin ] ) ) { 78 $all_plugins[ $plugin ] = array(); 79 } 80 $data[ $slug ] = $all_plugins[ $plugin ]; 81 } 82 83 foreach ( self::$plugins as $key => $plugin ) { 84 $selector = false; 85 if ( isset( $data[ $key ] ) ) { 86 $selector = $key; 87 } else { 88 if ( isset( $plugin['slug'] ) && isset( $data[ $plugin['slug'] ] ) ) { 89 $selector = $plugin['slug']; 90 } 91 } 92 if ( isset( $plugin['detect'] ) ) { 93 if ( isset( $plugin['detect']['freemius'] ) && $plugin['detect']['freemius'] ) { 94 // Freemius Version Detection. 95 if ( isset( $GLOBALS[ $plugin['detect']['freemius'] ] ) && ! empty( $GLOBALS[ $plugin['detect']['freemius'] ] ) ) { 96 $freemius = $GLOBALS[ $plugin['detect']['freemius'] ]; 97 $selector = $freemius->get_plugin_basename(); 98 $true_slug = explode( '/', $selector )[0]; 99 if ( $true_slug !== $key ) { 100 self::$plugins[ $key ]['true_slug'] = $true_slug; 101 } 102 if ( isset( self::$plugins[ $key ]['free_slug'] ) ) { 103 continue; // Let's only store the info on the free version. 104 } 105 $plugin_info = $freemius->get_plugin_data(); 106 if ( $selector && ! isset( $data[ $selector ]['Version'] ) ) { 107 self::$plugins[ $key ]['version'] = $plugin_info['Version']; 108 } 109 if ( $freemius->can_use_premium_code() ) { 110 self::$plugins[ $key ]['is_pro'] = true; 111 } 112 unset( self::$plugins[ $key ]['detect']['freemius'] ); 113 } 114 } 115 if ( isset( $plugin['detect']['defined'] ) ) { 116 if ( ! empty( $plugin['detect']['defined'] ) ) { 117 foreach ( $plugin['detect']['defined'] as $key_name => $defined_name ) { 118 if ( defined( $defined_name ) && ! empty( constant( $defined_name ) ) ) { 119 self::$plugins[ $key ][ $key_name ] = constant( $defined_name ); 120 } 121 } 122 } 123 unset( self::$plugins[ $key ]['detect']['defined'] ); 124 } 125 if ( empty( self::$plugins[ $key ]['detect'] ) ) { 126 unset( self::$plugins[ $key ]['detect'] ); 127 } 128 } else { 129 if ( isset( $data[ $key ] ) ) { 130 if ( isset( $data[ $key ]['Version'] ) ) { 131 self::$plugins[ $key ]['version'] = $data[ $key ]['Version']; 132 } 133 } 134 } 135 } 136 foreach ( self::$plugins as $key => $plugin ) { 137 if ( ! isset( $plugin['url'] ) || ( isset( $plugin['url'] ) && empty( $plugin['url'] ) ) ) { 138 if ( isset( $plugin['free_slug'] ) ) { 139 if ( isset( $plugin['free_slug'] ) ) { 140 $free_plugin = self::$plugins[ $plugin['free_slug'] ]; 141 if ( isset( $free_plugin['url'] ) ) { 142 self::$plugins[ $key ]['url'] = $free_plugin['url']; 143 } else { 144 self::$plugins[ $key ]['url'] = "https://wordpress.org/plugins/{$plugin['free_slug']}/"; 145 } 146 } 147 } else { 148 self::$plugins[ $key ]['url'] = "https://wordpress.org/plugins/{$key}/"; 149 } 150 } 151 } 152 self::$plugins['redux-framework']['plugin'] = defined( 'REDUX_PLUGIN_FILE' ); 153 if ( isset( self::$plugins['redux-pro'] ) ) { 154 self::$plugins['redux-pro']['redux_pro'] = true; 155 } 156 } 157 158 /** 159 * Helper function to return all installed plugins. 160 * 161 * @return array Array of plugins and which are installed. 162 * @since 4.0.0 163 */ 164 public static function get_plugins() { 165 $instance = self::instance(); 166 if ( empty( $instance::$plugins ) ) { 167 $instance->init(); 168 } 169 170 return $instance::$plugins; 171 } 172 173 }