companion.php (6337B)
1 <?php 2 3 namespace Materialis; 4 5 class Companion_Plugin { 6 public static $plugin_state; 7 public static $config = array(); 8 private static $instance = false; 9 private static $slug; 10 11 public function __construct( $config ) { 12 self::$config = $config; 13 self::$slug = $config['slug']; 14 add_action( 'tgmpa_register', array( __CLASS__, 'tgma_register' ) ); 15 add_action( 'wp_ajax_companion_disable_popup', array( __CLASS__, 'companion_disable_popup' ) ); 16 17 if ( get_template() === get_stylesheet() ) { 18 19 if ( ! get_option( 'materialis_companion_disable_popup', false ) ) { 20 if ( ! apply_filters( 'materialis_is_companion_installed', false ) ) { 21 global $pagenow; 22 if ( $pagenow !== "update.php" ) { 23 add_action( 'admin_notices', array( __CLASS__, 'plugin_notice' ) ); 24 add_action( 'admin_head', function () { 25 wp_enqueue_style( 'materialis_customizer.css', 26 get_template_directory_uri() . '/customizer/css/companion-install.css' ); 27 } ); 28 } 29 } 30 } 31 } 32 33 } 34 35 public static function plugin_notice() { 36 ?> 37 <div class="notice notice-success is-dismissible materialis-welcome-notice"> 38 <div class="notice-content-wrapper"> 39 <?php materialis_require( "/customizer/start-with-frontpage.php" ); ?> 40 </div> 41 </div> 42 <?php 43 } 44 45 public static function companion_disable_popup() { 46 $nonce = isset( $_POST['companion_disable_popup_wpnonce'] ) ? $_POST['companion_disable_popup_wpnonce'] : ''; 47 48 $nonce = wp_unslash( $nonce ); 49 50 if ( ! wp_verify_nonce( $nonce, "companion_disable_popup" ) ) { 51 die( "wrong nonce" ); 52 } 53 54 $value = isset( $_POST['value'] ) ? $_POST['value'] : false; 55 $value = wp_unslash( $value ); 56 $value = intval( $value ); 57 $option = isset( $_POST['option'] ) ? wp_unslash( $_POST['option'] ) : "materialis_companion_disable_popup"; 58 59 update_option( $option, $value ); 60 } 61 62 public static function tgma_register() { 63 self::$plugin_state = self::get_plugin_state( self::$slug ); 64 } 65 66 public static function get_plugin_state( $plugin_slug ) { 67 $tgmpa = \TGM_Plugin_Activation::get_instance(); 68 $installed = $tgmpa->is_plugin_installed( $plugin_slug ); 69 70 return array( 71 'installed' => $installed, 72 'active' => $installed && $tgmpa->is_plugin_active( $plugin_slug ), 73 ); 74 } 75 76 public static function output_companion_message() { 77 wp_enqueue_style( 'materialis_customizer_css', 78 get_template_directory_uri() . '/customizer/css/companion-install.css' ); 79 wp_enqueue_script( 'materialis_customizer_js', 80 get_template_directory_uri() . '/customizer/js/companion-install.js', array( 'jquery' ), false, true ); 81 ?> 82 <div id="extend-themes-companion-popover" style="display:none"> 83 <div class="extend-themes-companion-popover-close dashicons dashicons-no-alt"></div> 84 <div class="extend-themes-companion-popover-wrapper"> 85 <p class="extend-themes-companion-popover-message"> 86 <?php esc_html_e( 'Please Install the Materialis Companion Plugin to Enable All the Theme Features', 87 'materialis' ) ?> 88 </p> 89 <div class="extend-themes-companion-popover-actions"> 90 <?php 91 if ( \Materialis\Companion_Plugin::$plugin_state['installed'] ) { 92 $link = \Materialis\Companion_Plugin::get_activate_link(); 93 $label = esc_html__( 'Activate now', 'materialis' ); 94 } else { 95 $link = \Materialis\Companion_Plugin::get_install_link(); 96 $label = esc_html__( 'Install now', 'materialis' ); 97 } 98 printf( '<a class="install-now button button-large button-orange" href="%1$s">%2$s</a>', 99 esc_url( $link ), $label ); 100 ?> 101 </div> 102 </div> 103 </div> 104 <?php 105 } 106 107 public static function get_activate_link( $slug = false ) { 108 if ( ! $slug ) { 109 $slug = self::$slug; 110 } 111 $tgmpa = \TGM_Plugin_Activation::get_instance(); 112 $path = $tgmpa->plugins[ $slug ]['file_path']; 113 114 return add_query_arg( array( 115 'action' => 'activate', 116 'plugin' => rawurlencode( $path ), 117 'plugin_status' => 'all', 118 'paged' => '1', 119 '_wpnonce' => wp_create_nonce( 'activate-plugin_' . $path ), 120 ), network_admin_url( 'plugins.php' ) ); 121 } 122 123 public static function get_install_link( $slug = false ) { 124 if ( ! $slug ) { 125 $slug = self::$slug; 126 } 127 128 return add_query_arg( 129 array( 130 'action' => 'install-plugin', 131 'plugin' => $slug, 132 '_wpnonce' => wp_create_nonce( 'install-plugin_' . $slug ), 133 ), 134 network_admin_url( 'update.php' ) 135 ); 136 } 137 138 public static function check_companion( $wp_customize ) { 139 $plugin_state = self::$plugin_state; 140 141 if ( ! $plugin_state ) { 142 return; 143 } 144 145 if ( ! $plugin_state['installed'] || ! $plugin_state['active'] ) { 146 $wp_customize->add_setting( 'companion_install', array( 147 'default' => '', 148 'sanitize_callback' => 'esc_attr', 149 ) ); 150 151 152 if ( ! $plugin_state['installed'] ) { 153 $wp_customize->add_control( 154 new Install_Companion_Control( 155 $wp_customize, 156 'materialis_page_content', 157 array( 158 'section' => 'page_content', 159 'settings' => 'companion_install', 160 'label' => self::$config['install_label'], 161 'msg' => self::$config['install_msg'], 162 'plugin_state' => $plugin_state, 163 'slug' => self::$slug, 164 ) 165 ) 166 ); 167 } else { 168 $wp_customize->add_control( 169 new Activate_Companion_Control( 170 $wp_customize, 171 'materialis_page_content', 172 array( 173 'section' => 'page_content', 174 'settings' => 'companion_install', 175 'label' => self::$config['activate_label'], 176 'msg' => self::$config['activate_msg'], 177 'plugin_state' => $plugin_state, 178 'slug' => self::$slug, 179 ) 180 ) 181 ); 182 } 183 184 Companion_Plugin::show_companion_popup( $plugin_state ); 185 } 186 } 187 188 public static function show_companion_popup() { 189 190 add_action( 'customize_controls_print_footer_scripts', 191 array( '\Materialis\Companion_Plugin', 'output_companion_message' ) ); 192 } 193 194 // static functions 195 196 public static function init( $config ) { 197 Companion_Plugin::getInstance( $config ); 198 } 199 200 public static function getInstance( $config ) { 201 if ( ! self::$instance ) { 202 self::$instance = new Companion_Plugin( $config ); 203 } 204 205 return self::$instance; 206 } 207 }