config = $config; $this->prepare_class(); /*admin menu*/ add_action( 'admin_menu', array( $this, 'kt_admin_menu' ) ); /* enqueue script and style for about page */ add_action( 'admin_enqueue_scripts', array( $this, 'style_and_scripts' ) ); /* ajax callback for dismissable required actions */ add_action( 'wp_ajax_kt_theme_info_update_recommended_action', array( $this, 'update_recommended_action_callback' ) ); } /** * Prepare and setup class properties. */ public function prepare_class() { $theme = wp_get_theme(); if ( is_child_theme() ) { $this->theme_name = $theme->parent()->get( 'Name' ); } else { $this->theme_name = $theme->get( 'Name' ); } $this->theme_slug = $theme->get_template(); $this->theme_version = $theme->get( 'Version' ); $this->page_title = isset( $this->config['page_title'] ) ? $this->config['page_title'] : esc_html__('Info','blogto'). $this->theme_name; $this->menu_title = isset( $this->config['menu_title'] ) ? $this->config['menu_title'] : esc_html__('Info','blogto') . $this->theme_name; $this->tabs = isset( $this->config['tabs'] ) ? $this->config['tabs'] : array(); } /** * Return the valid array of recommended actions. * @return array The valid array of recommended actions. */ /** * Dismiss required actions */ public function update_recommended_action_callback() { /*getting for provided array*/ $recommended_actions = isset( $this->config['recommended_actions'] ) ? $this->config['recommended_actions'] : array(); /*from js action*/ $action_id = esc_attr( ( isset( $_GET['id'] ) ) ? $_GET['id'] : 0 ); $todo = esc_attr( ( isset( $_GET['todo'] ) ) ? $_GET['todo'] : '' ); /*getting saved actions*/ $saved_actions = get_option( $this->theme_slug . '_recommended_actions' ); echo esc_html( wp_unslash( $action_id ) ); /* this is needed and it's the id of the dismissable required action */ if ( ! empty( $action_id ) ) { if( 'reset' == $todo ){ $saved_actions_new = array(); if ( ! empty( $recommended_actions ) ) { foreach ( $recommended_actions as $recommended_action ) { $saved_actions[ $recommended_action['id'] ] = true; } update_option( $this->theme_slug . '_recommended_actions', $saved_actions_new ); } } /* if the option exists, update the record for the specified id */ elseif ( !empty( $saved_actions) and is_array( $saved_actions ) ) { switch ( esc_html( $todo ) ) { case 'add'; $saved_actions[ $action_id ] = true; break; case 'dismiss'; $saved_actions[ $action_id ] = false; break; } update_option( $this->theme_slug . '_recommended_actions', $saved_actions ); /* create the new option,with false for the specified id */ } else { $saved_actions_new = array(); if ( ! empty( $recommended_actions ) ) { foreach ( $recommended_actions as $recommended_action ) { echo $recommended_action['id']; echo " ".$todo; if ( $recommended_action['id'] == $action_id ) { switch ( esc_html( $todo ) ) { case 'add'; $saved_actions_new[ $action_id ] = true; break; case 'dismiss'; $saved_actions_new[ $action_id ] = false; break; } } } } update_option( $this->theme_slug . '_recommended_actions', $saved_actions_new ); } } exit; } private function get_recommended_actions() { $saved_actions = get_option( $this->theme_slug . '_recommended_actions' ); if ( ! is_array( $saved_actions ) ) { $saved_actions = array(); } $recommended_actions = isset( $this->config['recommended_actions'] ) ? $this->config['recommended_actions'] : array(); $valid = array(); if( isset( $recommended_actions ) && is_array( $recommended_actions ) ){ foreach ( $recommended_actions as $recommended_action ) { if ( ( ! isset( $recommended_action['check'] ) || ( isset( $recommended_action['check'] ) && ( $recommended_action['check'] == false ) ) ) && ( ! isset( $saved_actions[ $recommended_action['id'] ] ) || ( isset( $saved_actions[ $recommended_action['id']] ) && ($saved_actions[ $recommended_action['id']] == true ) ) ) ) { $valid[] = $recommended_action; } } } return $valid; } private function count_recommended_actions() { $count = 0; $actions_count = $this->get_recommended_actions(); if ( ! empty( $actions_count ) ) { $count = count( $actions_count ); } return $count; } /** * Adding Theme Info Menu under Appearance. */ function kt_admin_menu() { if ( ! empty( $this->page_title ) && ! empty( $this->menu_title ) ) { $count = $this->count_recommended_actions(); $menu_title = $count > 0 ? $this->menu_title . '' . esc_html( $count ) . '' : $this->menu_title; /* Example * add_theme_page('My Plugin Theme', 'My Plugin', 'edit_theme_options', 'my-unique-identifier', 'my_plugin_function'); * */ add_theme_page( $this->page_title, $menu_title, 'edit_theme_options', $this->theme_slug . '-info', array( $this, 'kt_theme_info_screen', ) ); } } /** * Render the info content screen. */ public function kt_theme_info_screen() { if ( ! empty( $this->config['info_title'] ) ) { $welcome_title = $this->config['info_title']; } if ( ! empty( $this->config['info_content'] ) ) { $welcome_content = $this->config['info_content']; } if ( ! empty( $this->config['quick_links'] ) ) { $quick_links = $this->config['quick_links']; } if ( ! empty( $welcome_title ) || ! empty( $welcome_content ) || ! empty( $quick_links ) || ! empty( $this->tabs ) ) { echo '
'; if ( ! empty( $welcome_title ) ) { echo '

'; echo esc_html( $welcome_title ); if ( ! empty( $this->theme_version ) ) { echo esc_html( $this->theme_version ) . ' '; } echo '

'; } if ( ! empty( $welcome_content ) ) { echo '
' . wp_kses_post( $welcome_content ) . '
'; } echo ''; /*quick links*/ if( !empty( $quick_links ) && is_array( $quick_links ) ){ echo '"; } /* Display tabs */ if ( ! empty( $this->tabs ) ) { $current_tab = isset( $_GET['tab'] ) ? wp_unslash( $_GET['tab'] ) : 'getting_started'; echo ''; /* Display content for current tab, dynamic method according to key provided*/ if ( method_exists( $this, $current_tab ) ) { echo "
"; $this->$current_tab(); echo "
"; } } echo '
'; } } /** * Getting started tab */ public function getting_started() { if ( ! empty( $this->config['getting_started'] ) ) { $getting_started = $this->config['getting_started']; if ( ! empty( $getting_started ) ) { /*defaults values for getting_started array */ $defaults = array( 'title' => '', 'desc' => '', 'recommended_actions'=> '', 'link_title' => '', 'link_url' => '', 'is_button' => false, 'is_new_tab' => false ); echo '
'; foreach ( $getting_started as $getting_started_item ) { /*allowed 6 value in array */ $instance = wp_parse_args( (array) $getting_started_item, $defaults ); /*default values*/ $title = esc_html( $instance[ 'title' ] ); $desc = wp_kses_post( $instance[ 'desc' ] ); $link_title = esc_html( $instance[ 'link_title' ] ); $link_url = esc_url( $instance[ 'link_url' ] ); $is_button = $instance[ 'is_button' ]; $is_new_tab = $instance[ 'is_new_tab' ]; echo '
'; if ( ! empty( $title ) ) { echo '

' . $title . '

'; } if ( ! empty( $desc ) ) { echo '

' . $desc . '

'; } if ( ! empty( $link_title ) && ! empty( $link_url ) ) { echo '

'; $button_class = ''; if ( $is_button ) { $button_class = 'button button-primary'; } $count = $this->count_recommended_actions(); if ( $getting_started_item['recommended_actions'] && isset( $count ) ) { if ( $count == 0 ) { echo ''; } else { echo ''; } } $button_new_tab = '_self'; if ( $is_new_tab ) { $button_new_tab = '_blank'; } echo '' . $getting_started_item['link_title'] . ''; echo '

'; } echo '
'; } echo '
'; } } } /** * Recommended Actions tab */ public function check_plugin_status( $slug ) { $path = WPMU_PLUGIN_DIR . '/' . $slug . '/' . $slug . '.php'; if ( ! file_exists( $path ) ) { $path = WP_PLUGIN_DIR . '/' . $slug . '/' . $slug . '.php'; if ( ! file_exists( $path ) ) { $path = false; } } if ( file_exists( $path ) ) { include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); $needs = is_plugin_active( $slug . '/' . $slug . '.php' ) ? 'deactivate' : 'activate'; return array( 'status' => is_plugin_active( $slug . '/' . $slug . '.php' ), 'needs' => $needs ); } return array( 'status' => false, 'needs' => 'install' ); } public function create_action_link( $state, $slug ) { switch ( $state ) { case 'install': return wp_nonce_url( add_query_arg( array( 'action' => 'install-plugin', 'plugin' => $slug ), network_admin_url( 'update.php' ) ), 'install-plugin_' . $slug ); break; case 'deactivate': return add_query_arg( array( 'action' => 'deactivate', 'plugin' => rawurlencode( $slug . '/' . $slug . '.php' ), 'plugin_status' => 'all', 'paged' => '1', '_wpnonce' => wp_create_nonce( 'deactivate-plugin_' . $slug . '/' . $slug . '.php' ) ), network_admin_url( 'plugins.php' ) ); break; case 'activate': return add_query_arg( array( 'action' => 'activate', 'plugin' => rawurlencode( $slug . '/' . $slug . '.php' ), 'plugin_status' => 'all', 'paged' => '1', '_wpnonce' => wp_create_nonce( 'activate-plugin_' . $slug . '/' . $slug . '.php' ) ), network_admin_url( 'plugins.php' ) ); break; } } public function recommended_actions() { $recommended_actions = isset( $this->config['recommended_actions'] ) ? $this->config['recommended_actions'] : array(); $hooray = true; if ( ! empty( $recommended_actions ) ) { echo '