diff --git a/functions.php b/functions.php index f0b28f6..c266f6e 100644 --- a/functions.php +++ b/functions.php @@ -255,6 +255,9 @@ function cenote_scripts() { add_action( 'wp_enqueue_scripts', 'cenote_scripts' ); +$cenote_theme = wp_get_theme(); +define( 'CENOTE_THEME_VERSION', $cenote_theme->get( 'Version' ) ); + /** * Implement the Custom Header feature. */ @@ -340,6 +343,7 @@ function cenote_scripts() { */ if ( is_admin() ) { require get_template_directory() . '/inc/admin/class-cenote-theme-review-notice.php'; + require get_template_directory() . '/inc/admin/class-cenote-admin.php'; } /** diff --git a/inc/admin/class-cenote-admin.php b/inc/admin/class-cenote-admin.php new file mode 100644 index 0000000..76c16a8 --- /dev/null +++ b/inc/admin/class-cenote-admin.php @@ -0,0 +1,209 @@ + esc_url( admin_url( '/themes.php?page=demo-importer&browse=all&cenote-hide-notice=welcome' ) ), + 'btn_text' => esc_html__( 'Processing...', 'cenote' ), + 'nonce' => wp_create_nonce( 'cenote_demo_import_nonce' ), + ); + + wp_localize_script( 'cenote-plugin-install-helper', 'cenote_redirect_demo_page', $translation_array ); + + } + + /** + * Handle the AJAX process while import or get started button clicked. + */ + public function tg_ajax_import_button_handler() { + + check_ajax_referer( 'cenote_demo_import_nonce', 'security' ); + + $state = ''; + + if ( class_exists( 'themegrill_demo_importer' ) ) { + $state = 'activated'; + } elseif ( file_exists( WP_PLUGIN_DIR . '/themegrill-demo-importer/themegrill-demo-importer.php' ) ) { + $state = 'installed'; + } + + if ( 'activated' === $state ) { + $response['redirect'] = admin_url( '/themes.php?page=demo-importer&browse=all&cenote-hide-notice=welcome' ); + } elseif ( 'installed' === $state ) { + $response['redirect'] = admin_url( '/themes.php?page=demo-importer&browse=all&cenote-hide-notice=welcome' ); + if ( current_user_can( 'activate_plugin' ) ) { + $result = activate_plugin( 'themegrill-demo-importer/themegrill-demo-importer.php' ); + + if ( is_wp_error( $result ) ) { + $response['errorCode'] = $result->get_error_code(); + $response['errorMessage'] = $result->get_error_message(); + } + } + } else { + wp_enqueue_style( 'plugin-install' ); + wp_enqueue_script( 'plugin-install' ); + $response['redirect'] = admin_url( '/themes.php?page=demo-importer&browse=all&cenote-hide-notice=welcome' ); + /** + * Install Plugin. + */ + include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; + include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; + + $api = plugins_api( 'plugin_information', array( + 'slug' => sanitize_key( wp_unslash( 'themegrill-demo-importer' ) ), + 'fields' => array( + 'sections' => false, + ), + ) ); + + $skin = new WP_Ajax_Upgrader_Skin(); + $upgrader = new Plugin_Upgrader( $skin ); + $result = $upgrader->install( $api->download_link ); + if ( $result ) { + $response['installed'] = 'succeed'; + } else { + $response['installed'] = 'failed'; + } + + // Activate plugin. + if ( current_user_can( 'activate_plugin' ) ) { + $result = activate_plugin( 'themegrill-demo-importer/themegrill-demo-importer.php' ); + + if ( is_wp_error( $result ) ) { + $response['errorCode'] = $result->get_error_code(); + $response['errorMessage'] = $result->get_error_message(); + } + } + } + + wp_send_json( $response ); + + exit(); + + } + + /** + * Add admin notice. + */ + public function admin_notice() { + + wp_enqueue_style( 'cenote-message', get_template_directory_uri() . '/inc/admin/css/message.css', array(), CENOTE_THEME_VERSION ); + + // Let's bail on theme activation. + $notice_nag = get_option( 'cenote_admin_notice_welcome' ); + if ( ! $notice_nag ) { + add_action( 'admin_notices', array( $this, 'welcome_notice' ) ); + } + + } + + /** + * Hide a notice if the GET variable is set. + */ + public static function hide_notices() { + + if ( isset( $_GET['cenote-hide-notice'] ) && isset( $_GET['_cenote_notice_nonce'] ) ) { // WPCS: input var ok. + if ( ! wp_verify_nonce( wp_unslash( $_GET['_cenote_notice_nonce'] ), 'cenote_hide_notices_nonce' ) ) { // phpcs:ignore WordPress.VIP.ValidatedSanitizedInput.InputNotSanitized + wp_die( __( 'Action failed. Please refresh the page and retry.', 'cenote' ) ); // WPCS: xss ok. + } + + if ( ! current_user_can( 'manage_options' ) ) { + wp_die( __( 'Cheatin’ huh?', 'cenote' ) ); // WPCS: xss ok. + } + + $hide_notice = sanitize_text_field( wp_unslash( $_GET['cenote-hide-notice'] ) ); + update_option( 'cenote_admin_notice_' . $hide_notice, 1 ); + + // Hide. + if ( 'welcome' === $_GET['cenote-hide-notice'] ) { + update_option( 'cenote_admin_notice_' . $hide_notice, 1 ); + } else { // Show. + delete_option( 'cenote_admin_notice_' . $hide_notice ); + } + } + + } + + /** + * Return or echo `Get started/Import button` HTML. + * + * @param bool $return Return or echo. + * @param string $slug PLugin slug to install. + * @param string $text Text string for button. + * @param string $css_class CSS class list for button link. + * + */ + public static function import_button_html( $return = false, $slug = 'themegrill-demo-importer', $text, $css_class = 'btn-get-started button button-primary button-hero' ) { + + if ( true === $return ) { + return '' . esc_html( $text ) . ''; + } else { + echo '' . esc_html( $text ) . ''; + } + + } + + /** + * Show welcome notice. + */ + public function welcome_notice() { + ?> + +
+ + + +
+ <?php esc_html_e( 'Cenote', 'cenote' ); ?> + +
+

+ ', '' ); + ?> +

+ +

+ + +
+
+
+ import button while processing. + if ( jQuery( this ).parents( '.theme-actions' ).length ) { + jQuery( this ).parents( '.theme-actions' ).css( 'opacity', '1' ); + } + + // Show updating gif icon. + jQuery( this ).addClass( 'updating-message' ); + + // Change button text. + jQuery( this ).text( cenote_redirect_demo_page.btn_text ); + + // Assign `TG demo importer` plugin state for processing from PHP. + if ( $( this ).hasClass( 'tdi-activated' ) ) { // Installed and activated. + state = 'activated'; + } else if ( $( this ).hasClass( 'tdi-installed' ) ) { // Installed but not activated. + state = 'installed'; + } else { // Not installed. + state = ''; + } + + var data = { + action : 'import_button', + security : cenote_redirect_demo_page.nonce, + state : state + }; + + $.ajax( { + type : "POST", + url : ajaxurl, // URL to "wp-admin/admin-ajax.php" + data : data, + success : function ( response ) { + extra_uri = ''; + if ( jQuery( '.cenote-message-close' ).length ) { + dismiss_nonce = jQuery( '.cenote-message-close' ).attr( 'href' ).split( '_cenote_notice_nonce=' )[ 1 ]; + extra_uri = '&_cenote_notice_nonce=' + dismiss_nonce; + } + + redirect_uri = response.redirect + extra_uri; + window.location.href = redirect_uri; + }, + error : function ( xhr, ajaxOptions, thrownError ) { + console.log( thrownError ); + } + } ); + + } ); +} );