diff --git a/core/app/class-orbit-fox-admin.php b/core/app/class-orbit-fox-admin.php index 009ceb62..010ed8e6 100644 --- a/core/app/class-orbit-fox-admin.php +++ b/core/app/class-orbit-fox-admin.php @@ -107,6 +107,22 @@ public function enqueue_scripts() { } if ( in_array( $screen->id, array( 'toplevel_page_obfx_companion' ), true ) ) { wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . '../assets/js/orbit-fox-admin.js', array( 'jquery' ), $this->version, false ); + + wp_register_script( 'obfx-plugin-install', plugin_dir_url( __FILE__ ) . '../assets/js/plugin-install.js', array( 'jquery' ), $this->version, true ); + + wp_localize_script( + 'obfx-plugin-install', + 'obfxPluginInstall', + array( + 'activating' => esc_html__( 'Activating ', 'themeisle-companion' ), + 'installing' => esc_html__( 'Installing ... ', 'themeisle-companion' ), + ) + ); + + wp_enqueue_script( 'plugin-install' ); + wp_enqueue_script( 'updates' ); + wp_enqueue_script( 'obfx-plugin-install' ); + } do_action( 'obfx_admin_enqueue_scripts' ); } @@ -259,97 +275,43 @@ public function load_recommended_plugins() { 'wordpress-seo', ]; shuffle( $plugins ); - add_thickbox(); echo sprintf( '', 'Orbit Fox recommends' ); + + $install_instance = new Orbit_Fox_Plugin_Install(); + foreach ( $plugins as $plugin ) { - $current_plugin = $this->call_plugin_api( $plugin ); + $current_plugin = $install_instance->call_plugin_api( $plugin ); if ( ! isset( $current_plugin->name ) ) { continue; } - $image = $current_plugin->icons['1x']; - $name = $current_plugin->name; - $desc = $current_plugin->short_description; - $url = add_query_arg( - array( - 'tab' => 'plugin-information', - 'plugin' => $plugin, - 'TB_iframe' => true, - 'width' => 800, - 'height' => 800, - ), - network_admin_url( 'plugin-install.php' ) - ); - echo sprintf( - '', - esc_url( $image ), - esc_attr( $name ), - esc_attr( $desc ), - esc_url( $url ), - esc_attr__( 'Install', 'themeisle-companion' ) - ); + $image = $install_instance->check_for_icon( $current_plugin->icons ); + $name = $current_plugin->name; + $desc = $current_plugin->short_description; + $button = $install_instance->get_button_html( $plugin ); + + echo ''; } } - /** - * Get info from wporg api. - * - * @param string $slug Plugin slug. - * - * @return array|mixed|object|WP_Error - */ - public function call_plugin_api( $slug ) { - include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; - - $call_api = get_transient( 'ti_plugin_info_' . $slug ); - - if ( false === $call_api ) { - $call_api = plugins_api( - 'plugin_information', - array( - 'slug' => $slug, - 'fields' => array( - 'downloaded' => false, - 'rating' => false, - 'description' => false, - 'short_description' => true, - 'donate_link' => false, - 'tags' => false, - 'sections' => true, - 'homepage' => true, - 'added' => false, - 'last_updated' => false, - 'compatibility' => false, - 'tested' => false, - 'requires' => false, - 'downloadlink' => false, - 'icons' => true, - 'banners' => true, - ), - ) - ); - set_transient( 'ti_plugin_info_' . $slug, $call_api, 1 * DAY_IN_SECONDS ); - } - - return $call_api; - } + /** * This method is called via AJAX and processes the diff --git a/core/app/class-orbit-fox-plugin-install.php b/core/app/class-orbit-fox-plugin-install.php new file mode 100644 index 00000000..2afb1f7f --- /dev/null +++ b/core/app/class-orbit-fox-plugin-install.php @@ -0,0 +1,190 @@ + $slug, + 'fields' => array( + 'downloaded' => false, + 'rating' => false, + 'description' => false, + 'short_description' => true, + 'donate_link' => false, + 'tags' => false, + 'sections' => true, + 'homepage' => true, + 'added' => false, + 'last_updated' => false, + 'compatibility' => false, + 'tested' => false, + 'requires' => false, + 'downloadlink' => false, + 'icons' => true, + 'banners' => true, + ), + ) + ); + set_transient( 'ti_plugin_info_' . $slug, $call_api, 1 * DAY_IN_SECONDS ); + } + + return $call_api; + } + + /** + * Get plugin icon. + * + * @param array $arr Icon sizes. + * + * @return string + */ + public function check_for_icon( $arr ) { + if ( ! empty( $arr['svg'] ) ) { + $plugin_icon_url = $arr['svg']; + } elseif ( ! empty( $arr['2x'] ) ) { + $plugin_icon_url = $arr['2x']; + } elseif ( ! empty( $arr['1x'] ) ) { + $plugin_icon_url = $arr['1x']; + } else { + $plugin_icon_url = $arr['default']; + } + + return $plugin_icon_url; + } + + /** + * Check plugin state. + * + * @param string $slug plugin slug. + * + * @return bool + */ + public function check_plugin_state( $slug ) { + + $plugin_link_suffix = self::get_plugin_path( $slug ); + + if ( file_exists( ABSPATH . 'wp-content/plugins/' . $plugin_link_suffix ) ) { + return is_plugin_active( $plugin_link_suffix ) ? 'deactivate' : 'activate'; + } + + return 'install'; + } + + + /** + * Get plugin path based on plugin slug. + * + * @param string $slug Plugin slug. + * + * @return string + */ + public static function get_plugin_path( $slug ) { + + switch ( $slug ) { + case 'wpforms-lite': + return $slug . '/wpforms.php'; + break; + case 'translatepress-multilingual': + return $slug . '/index.php'; + break; + case 'feedzy-rss-feeds': + return $slug . '/feedzy-rss-feed.php'; + break; + case 'wordpress-seo': + return $slug . '/wp-seo.php'; + default: + return $slug . '/' . $slug . '.php'; + } + } + + /** + * Generate action button html. + * + * @param string $slug plugin slug. + * @param array $settings button settings. + * + * @return string + */ + public function get_button_html( $slug, $settings = array() ) { + $button = ''; + $redirect = ''; + if ( ! empty( $settings ) && array_key_exists( 'redirect', $settings ) ) { + $redirect = $settings['redirect']; + } + $state = $this->check_plugin_state( $slug ); + if ( empty( $slug ) ) { + return ''; + } + + $additional = ''; + + if ( $state === 'deactivate' ) { + $additional = ' action_button active'; + } + + $button .= '
'; + + $plugin_link_suffix = self::get_plugin_path( $slug ); + + $nonce = add_query_arg( + array( + 'action' => 'activate', + 'plugin' => rawurlencode( $plugin_link_suffix ), + 'plugin_status' => 'all', + 'paged' => '1', + '_wpnonce' => wp_create_nonce( 'activate-plugin_' . $plugin_link_suffix ), + ), + esc_url( network_admin_url( 'plugins.php' ) ) + ); + switch ( $state ) { + case 'install': + $button .= '' . __( 'Install and activate', 'themeisle-companion' ) . ''; + break; + + case 'activate': + $button .= '' . esc_html__( 'Activate', 'themeisle-companion' ) . ''; + break; + + case 'deactivate': + $nonce = add_query_arg( + array( + 'action' => 'deactivate', + 'plugin' => rawurlencode( $plugin_link_suffix ), + 'plugin_status' => 'all', + 'paged' => '1', + '_wpnonce' => wp_create_nonce( 'deactivate-plugin_' . $plugin_link_suffix ), + ), + esc_url( network_admin_url( 'plugins.php' ) ) + ); + + $button .= '' . esc_html__( 'Deactivate', 'themeisle-companion' ) . ''; + break; + + default: + break; + }// End switch(). + $button .= '
'; + + return $button; + } +} diff --git a/core/assets/css/orbit-fox-admin.css b/core/assets/css/orbit-fox-admin.css index 967b54e0..9edbd2c2 100644 --- a/core/assets/css/orbit-fox-admin.css +++ b/core/assets/css/orbit-fox-admin.css @@ -3878,9 +3878,12 @@ ul.obfx-menu-tabs{ line-height: 15px; box-shadow: none; text-shadow: none; + display: inline; } -.obfx-recommended .button-primary:hover { +.obfx-recommended .button-primary:hover, +.obfx-recommended .button-primary:active, +.obfx-recommended .button-primary:focus { background: #fff; border-color: #5764c6; color: #5764c6; @@ -3889,6 +3892,28 @@ ul.obfx-menu-tabs{ transition: 0.3s; } +.obfx-recommended .button-primary.updating-message:before { + color: #fff; + margin: 0; + vertical-align: middle; + margin-right: 5px; + padding: 0; +} + +.obfx-recommended .button-primary span.dashicons { + vertical-align: middle; + margin-right: 5px; +} + +.obfx-recommended .button-primary:hover span.dashicons, +.obfx-recommended .button-primary.updating-message:hover:before, +.obfx-recommended .button-primary:active span.dashicons, +.obfx-recommended .button-primary.updating-message:active:before, +.obfx-recommended .button-primary:focus span.dashicons, +.obfx-recommended .button-primary.updating-message:focus:before{ + color: #5764c6; +} + .wp-core-ui .button-primary:hover { text-shadow: none; } diff --git a/core/assets/js/plugin-install.js b/core/assets/js/plugin-install.js new file mode 100644 index 00000000..46e38214 --- /dev/null +++ b/core/assets/js/plugin-install.js @@ -0,0 +1,97 @@ +/** + * Remove activate button and replace with activation in progress button. + */ + +/* global obfxPluginInstall */ +/* global console */ + +jQuery( document ).ready( + function ( $ ) { + $.pluginInstall = { + 'init': function () { + this.handleInstall(); + this.handleActivate(); + }, + + 'handleInstall': function () { + var self = this; + $( 'body' ).on( 'click', '.obfx-install-plugin', function ( e ) { + e.preventDefault(); + var button = $( this ); + if ( button.hasClass('no-click') ){ + return false; + } + $('.obfx-install-plugin').addClass( 'no-click' ); + var slug = button.attr( 'data-slug' ); + var url = button.attr( 'href' ); + var redirect = $( button ).attr( 'data-redirect' ); + button.text( obfxPluginInstall.installing ); + button.addClass( 'updating-message' ); + wp.updates.installPlugin( + { + slug: slug, + success: function () { + button.text( obfxPluginInstall.activating + '...' ); + self.activatePlugin( url, redirect ); + } + } + ); + } ); + }, + + 'activatePlugin': function ( url, redirect ) { + if ( typeof url === 'undefined' || !url ) { + return; + } + jQuery.ajax( + { + async: true, + type: 'GET', + url: url, + success: function () { + // Reload the page. + if ( typeof(redirect) !== 'undefined' && redirect !== '' ) { + window.location.replace( redirect ); + } else { + location.reload(); + } + }, + error: function ( jqXHR, exception ) { + var msg = ''; + if ( jqXHR.status === 0 ) { + msg = 'Not connect.\n Verify Network.'; + } else if ( jqXHR.status === 404 ) { + msg = 'Requested page not found. [404]'; + } else if ( jqXHR.status === 500 ) { + msg = 'Internal Server Error [500].'; + } else if ( exception === 'parsererror' ) { + msg = 'Requested JSON parse failed.'; + } else if ( exception === 'timeout' ) { + msg = 'Time out error.'; + } else if ( exception === 'abort' ) { + msg = 'Ajax request aborted.'; + } else { + msg = 'Uncaught Error.\n' + jqXHR.responseText; + } + console.log( msg ); + }, + } + ); + }, + + 'handleActivate': function () { + var self = this; + $( 'body' ).on( 'click', '.activate-now', function ( e ) { + e.preventDefault(); + var button = $( this ); + var url = button.attr( 'href' ); + var redirect = button.attr( 'data-redirect' ); + button.addClass( 'updating-message' ); + button.text( obfxPluginInstall.activating + '...' ); + self.activatePlugin( url, redirect ); + } ); + }, + }; + $.pluginInstall.init(); + } +); diff --git a/obfx_modules/google-analytics/init.php b/obfx_modules/google-analytics/init.php index 09044e4b..61e12f3f 100644 --- a/obfx_modules/google-analytics/init.php +++ b/obfx_modules/google-analytics/init.php @@ -94,11 +94,11 @@ public function register_endpoints() { '/obfx-analytics', array( array( - 'methods' => WP_REST_Server::CREATABLE, - 'callback' => array( $this, 'refresh_tracking_links' ), - 'permission_callback' => function () { - return current_user_can( 'manage_options' ); - }, + 'methods' => WP_REST_Server::CREATABLE, + 'callback' => array( $this, 'refresh_tracking_links' ), + 'permission_callback' => function () { + return current_user_can( 'manage_options' ); + }, ), ) );