| @@ -1,88 +1,56 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * Plugin Admin Page |
| 4 | - * | |
| 4 | + * @package GutSliderBlocks | |
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | - if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly | |
| 7 | +if (!defined('ABSPATH')) exit; | |
| 8 | 8 | |
| 9 | - if( ! class_exists( 'GutSlider_Admin' ) ) { | |
| 10 | - | |
| 9 | +if (!class_exists('GutSlider_Admin')) { | |
| 11 | 10 | class GutSlider_Admin { |
| 11 | + /** | |
| 12 | + * Slider block settings | |
| 13 | + */ | |
| 14 | + private const BLOCK_SETTINGS = [ | |
| 15 | + 'gut_fixed_content_slider', | |
| 16 | + 'gut_any_content_slider', | |
| 17 | + 'gut_testimonial_slider', | |
| 18 | + 'gut_post_slider', | |
| 19 | + 'gut_photo_carousel', | |
| 20 | + 'gut_logo_carousel', | |
| 21 | + 'gut_before_after_slider', | |
| 22 | + 'gut_videos_carousel' | |
| 23 | + ]; | |
| 12 | 24 | |
| 13 | 25 | /** |
| 14 | 26 | * Constructor |
| 15 | - * return void | |
| 16 | 27 | */ |
| 17 | 28 | public function __construct() { |
| 18 | - // include admin notice | |
| 19 | - $this->include_admin_notice(); | |
| 20 | - | |
| 21 | - add_action( 'admin_menu', [ $this, 'admin_menu' ] ); | |
| 22 | - add_action( 'admin_enqueue_scripts', [ $this, 'admin_assests' ] ); | |
| 23 | - | |
| 24 | - // register settings | |
| 25 | - add_action('admin_init', [ $this, 'custom_register_settings' ]); | |
| 26 | - add_action( 'rest_api_init', [ $this, 'custom_register_settings' ] ); | |
| 27 | - | |
| 28 | - // include admin data SDk | |
| 29 | - add_action( 'plugins_loaded', [ $this, 'dci_plugin_gutslider' ] ); | |
| 29 | + add_action('admin_menu', [$this, 'admin_menu'], 20); | |
| 30 | + add_action('admin_enqueue_scripts', [$this, 'admin_assets']); | |
| 31 | + add_action('admin_init', [$this, 'initialize_admin']); | |
| 32 | + add_action('rest_api_init', [$this, 'register_settings']); | |
| 30 | 33 | } |
| 31 | 34 | |
| 32 | 35 | /** |
| 33 | - * Include admin notice | |
| 36 | + * Initialize admin functionality | |
| 34 | 37 | */ |
| 35 | - public function include_admin_notice() { | |
| 36 | - require_once trailingslashit( GUTSLIDER_DIR ) . '/admin/notice/notice.php'; | |
| 38 | + public function initialize_admin() { | |
| 39 | + $this->register_settings(); | |
| 40 | + $this->include_data_sdk(); | |
| 37 | 41 | } |
| 38 | - | |
| 39 | - /** | |
| 40 | - * Include data SDK | |
| 41 | - */ | |
| 42 | - public function dci_plugin_gutslider() { | |
| 43 | - // Include DCI SDK. | |
| 44 | - require_once dirname( __FILE__ ) . '/dci/start.php'; | |
| 45 | - dci_dynamic_init( array( | |
| 46 | - 'sdk_version' => '1.0.0', | |
| 47 | - 'plugin_title' => 'GutSlider', | |
| 48 | - 'product_id' => 3, | |
| 49 | - 'api_endpoint' => 'https://dashboard.gutenbergkits.com/wp-json/dci/v1/data-insights', | |
| 50 | - 'slug' => 'slider-blocks', | |
| 51 | - 'public_key' => 'pk_KxU4qcYXPyqvBDwsyyBkbCfY9Gulc1z5', | |
| 52 | - 'is_premium' => false, | |
| 53 | - 'menu' => array( | |
| 54 | - 'slug' => 'gutslider-blocks', | |
| 55 | - ), | |
| 56 | - ) ); | |
| 57 | - } | |
| 58 | 42 | |
| 59 | 43 | /** |
| 60 | - * Enqueue admin scripts | |
| 44 | + * Enqueue admin scripts and styles | |
| 45 | + * @param string $screen Current admin screen | |
| 61 | 46 | */ |
| 62 | - public function admin_assests( $screen ) { | |
| 47 | + public function admin_assets($screen) { | |
| 48 | + if ($screen !== 'toplevel_page_gutslider-blocks') { | |
| 49 | + return; | |
| 50 | + } | |
| 63 | 51 | |
| 64 | - if( $screen === 'toplevel_page_gutslider-blocks' ){ | |
| 65 | - | |
| 66 | - $dependencyFile = trailingslashit( GUTSLIDER_DIR ) . '/build/admin/admin.asset.php'; | |
| 67 | - $dependencies = file_exists( $dependencyFile ) ? require_once $dependencyFile : []; | |
| 68 | - | |
| 69 | - wp_enqueue_style( 'gutslider-admin-style', trailingslashit( GUTSLIDER_URL ) . 'build/admin/style-admin.css', [], GUTSLIDER_VERSION ); | |
| 70 | - wp_enqueue_script( 'gutslider-admin-script', trailingslashit( GUTSLIDER_URL ) . 'build/admin/admin.js', $dependencies[ 'dependencies' ] , GUTSLIDER_VERSION, true ); | |
| 71 | - | |
| 72 | - // enqueue wp-style | |
| 73 | - wp_enqueue_style( 'wp-components' ); | |
| 74 | - | |
| 75 | - // localize script | |
| 76 | - wp_localize_script( | |
| 77 | - 'gutslider-admin-script', | |
| 78 | - 'gutslider', | |
| 79 | - [ | |
| 80 | - 'version' => GUTSLIDER_VERSION, | |
| 81 | - ] | |
| 82 | - ); | |
| 83 | - | |
| 84 | - } | |
| 52 | + $this->enqueue_admin_assets(); | |
| 85 | 53 | } |
| 86 | 54 | |
| 87 | 55 | /** |
| 88 | 56 | * Add admin menu |
| @@ -87,83 +55,115 @@ | ||
| 87 | 55 | /** |
| 88 | 56 | * Add admin menu |
| 89 | 57 | */ |
| 90 | 58 | public function admin_menu() { |
| 91 | - add_menu_page( __( 'GutSlider', 'slider-blocks' ), __( 'GutSlider', 'slider-blocks' ), 'manage_options', 'gutslider-blocks', [ $this, 'gutslider_admin_page' ], 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjQiIGhlaWdodD0iMjQiIHZpZXdCb3g9IjAgMCAyNCAyNCIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHJlY3Qgd2lkdGg9IjI0IiBoZWlnaHQ9IjI0IiByeD0iMTIiIGZpbGw9IiNEOUQ5RDkiLz4KPHBhdGggZD0iTTE1IDdIMTEuODIzNUg5VjE3SDE1VjEyLjQxNjdIMTIuODgyNEgxMS44MjM1IiBzdHJva2U9IiMxRDIzMjciIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIi8+Cjwvc3ZnPgo=', 100 ); | |
| 59 | + add_menu_page( | |
| 60 | + __('GutSlider', 'slider-blocks'), | |
| 61 | + __('GutSlider', 'slider-blocks'), | |
| 62 | + 'manage_options', | |
| 63 | + 'gutslider-blocks', | |
| 64 | + [$this, 'render_admin_page'], | |
| 65 | + 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjQiIGhlaWdodD0iMjQiIHZpZXdCb3g9IjAgMCAyNCAyNCIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHJlY3Qgd2lkdGg9IjI0IiBoZWlnaHQ9IjI0IiByeD0iMTIiIGZpbGw9IiNEOUQ5RDkiLz4KPHBhdGggZD0iTTE1IDdIMTEuODIzNUg5VjE3SDE1VjEyLjQxNjdIMTIuODgyNEgxMS44MjM1IiBzdHJva2U9IiMxRDIzMjciIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIi8+Cjwvc3ZnPgo=', | |
| 66 | + 100 | |
| 67 | + ); | |
| 68 | + | |
| 69 | + add_submenu_page( | |
| 70 | + 'gutslider-blocks', | |
| 71 | + __( 'Welcome', 'slider-blocks' ), | |
| 72 | + __( 'Welcome', 'slider-blocks' ), | |
| 73 | + 'manage_options', | |
| 74 | + 'gutslider-blocks', | |
| 75 | + array( $this, 'render_admin_page' ) | |
| 76 | + ); | |
| 92 | 77 | } |
| 93 | 78 | |
| 94 | 79 | /** |
| 95 | - * Admin page | |
| 80 | + * Render admin page | |
| 96 | 81 | */ |
| 97 | - public function gutslider_admin_page() { | |
| 98 | - ?> | |
| 99 | - <div id="gutslider"></div> | |
| 100 | - <?php | |
| 82 | + public function render_admin_page() { | |
| 83 | + echo '<div id="gutslider"></div>'; | |
| 101 | 84 | } |
| 102 | 85 | |
| 103 | 86 | /** |
| 104 | - * Register settings | |
| 105 | - * return void | |
| 87 | + * Register block settings | |
| 106 | 88 | */ |
| 107 | - public function custom_register_settings() { | |
| 89 | + public function register_settings() { | |
| 90 | + foreach (self::BLOCK_SETTINGS as $setting) { | |
| 91 | + register_setting('rest-api-settings', $setting, [ | |
| 92 | + 'type' => 'boolean', | |
| 93 | + 'default' => true, | |
| 94 | + 'show_in_rest' => true, | |
| 95 | + 'sanitize_callback' => 'rest_sanitize_boolean', | |
| 96 | + ]); | |
| 97 | + } | |
| 98 | + } | |
| 108 | 99 | |
| 109 | - // fixed content slider | |
| 110 | - register_setting('rest-api-settings', 'gut_fixed_content_slider', array( | |
| 111 | - 'type' => 'boolean', | |
| 112 | - 'default' => true, | |
| 113 | - 'show_in_rest' => true, | |
| 114 | - 'sanitize_callback' => 'rest_sanitize_boolean', | |
| 115 | - )); | |
| 100 | + /** | |
| 101 | + * Include data SDK | |
| 102 | + */ | |
| 103 | + private function include_data_sdk() { | |
| 104 | + require_once dirname(__FILE__) . '/dci/start.php'; | |
| 105 | + | |
| 106 | + dci_dynamic_init([ | |
| 107 | + 'sdk_version' => '1.2.1', | |
| 108 | + 'product_id' => 3, | |
| 109 | + 'plugin_name' => 'GutSlider', | |
| 110 | + 'plugin_title' => 'GutSlider', | |
| 111 | + 'api_endpoint' => 'https://dashboard.codedivo.com/wp-json/dci/v1/data-insights', | |
| 112 | + 'slug' => 'gutslider-blocks', | |
| 113 | + 'core_file' => false, | |
| 114 | + 'plugin_deactivate_id' => false, | |
| 115 | + 'menu' => ['slug' => 'gutslider-blocks'], | |
| 116 | + 'public_key' => 'pk_KxU4qcYXPyqvBDwsyyBkbCfY9Gulc1z5', | |
| 117 | + 'is_premium' => false, | |
| 118 | + 'popup_notice' => false, | |
| 119 | + 'deactivate_feedback' => true, | |
| 120 | + 'delay_time' => ['time' => 3 * DAY_IN_SECONDS], | |
| 121 | + 'text_domain' => 'slider-blocks', | |
| 122 | + 'plugin_msg' => '<p>Thank you for using GutSlider! 🎉</p><p>We collect some non-sensitive data to improve our product and decide which features to build next.</p>', | |
| 123 | + ]); | |
| 124 | + } | |
| 116 | 125 | |
| 117 | - // any content | |
| 118 | - register_setting('rest-api-settings', 'gut_any_content_slider', array( | |
| 119 | - 'type' => 'boolean', | |
| 120 | - 'default' => true, | |
| 121 | - 'show_in_rest' => true, | |
| 122 | - 'sanitize_callback' => 'rest_sanitize_boolean', | |
| 123 | - )); | |
| 126 | + /** | |
| 127 | + * Get GutSlider changelogs | |
| 128 | + * @return array|false Changelog data or false on failure | |
| 129 | + */ | |
| 130 | + private function get_change_logs() { | |
| 131 | + $changelog_file = GUTSLIDER_DIR . '/changelogs.json'; | |
| 132 | + | |
| 133 | + if (!file_exists($changelog_file)) { | |
| 134 | + return false; | |
| 135 | + } | |
| 136 | + | |
| 137 | + global $wp_filesystem; | |
| 138 | + if (empty($wp_filesystem)) { | |
| 139 | + require_once(ABSPATH . '/wp-admin/includes/file.php'); | |
| 140 | + WP_Filesystem(); | |
| 141 | + } | |
| 142 | + | |
| 143 | + $changelogs = $wp_filesystem->get_contents($changelog_file); | |
| 144 | + return json_decode($changelogs, true); | |
| 145 | + } | |
| 124 | 146 | |
| 125 | - // testimonial slider | |
| 126 | - register_setting('rest-api-settings', 'gut_testimonial_slider', array( | |
| 127 | - 'type' => 'boolean', | |
| 128 | - 'default' => true, | |
| 129 | - 'show_in_rest' => true, | |
| 130 | - 'sanitize_callback' => 'rest_sanitize_boolean', | |
| 131 | - )); | |
| 147 | + /** | |
| 148 | + * Enqueue admin assets | |
| 149 | + */ | |
| 150 | + private function enqueue_admin_assets() { | |
| 151 | + $dependency_file = GUTSLIDER_DIR . '/build/admin/admin.asset.php'; | |
| 152 | + $dependencies = file_exists($dependency_file) ? require_once($dependency_file) : []; | |
| 132 | 153 | |
| 133 | - // post slider | |
| 134 | - register_setting('rest-api-settings', 'gut_post_slider', array( | |
| 135 | - 'type' => 'boolean', | |
| 136 | - 'default' => true, | |
| 137 | - 'show_in_rest' => true, | |
| 138 | - 'sanitize_callback' => 'rest_sanitize_boolean', | |
| 139 | - )); | |
| 154 | + wp_enqueue_style('gutslider-admin-style', GUTSLIDER_URL . 'build/admin/style-admin.css', [], GUTSLIDER_VERSION); | |
| 155 | + wp_enqueue_script('gutslider-admin-script', GUTSLIDER_URL . 'build/admin/admin.js', $dependencies['dependencies'], GUTSLIDER_VERSION, true); | |
| 156 | + wp_enqueue_style('wp-components'); | |
| 140 | 157 | |
| 141 | - // photo carousel | |
| 142 | - register_setting('rest-api-settings', 'gut_photo_carousel', array( | |
| 143 | - 'type' => 'boolean', | |
| 144 | - 'default' => true, | |
| 145 | - 'show_in_rest' => true, | |
| 146 | - 'sanitize_callback' => 'rest_sanitize_boolean', | |
| 147 | - )); | |
| 148 | - | |
| 149 | - // logo carousel | |
| 150 | - register_setting('rest-api-settings', 'gut_logo_carousel', array( | |
| 151 | - 'type' => 'boolean', | |
| 152 | - 'default' => true, | |
| 153 | - 'show_in_rest' => true, | |
| 154 | - 'sanitize_callback' => 'rest_sanitize_boolean', | |
| 155 | - )); | |
| 156 | - | |
| 157 | - // before after | |
| 158 | - register_setting('rest-api-settings', 'gut_before_after_slider', array( | |
| 159 | - 'type' => 'boolean', | |
| 160 | - 'default' => true, | |
| 161 | - 'show_in_rest' => true, | |
| 162 | - 'sanitize_callback' => 'rest_sanitize_boolean', | |
| 163 | - )); | |
| 158 | + wp_localize_script('gutslider-admin-script', 'gutslider', [ | |
| 159 | + 'version' => GUTSLIDER_VERSION, | |
| 160 | + 'changeLogs' => $this->get_change_logs(), | |
| 161 | + 'nonce' => wp_create_nonce('gutslider_nonce'), | |
| 162 | + 'isPro' => defined('GUTSLIDER_PRO_VERSION'), | |
| 163 | + 'proVersion' => defined('GUTSLIDER_PRO_VERSION') ? GUTSLIDER_PRO_VERSION : '', | |
| 164 | + ]); | |
| 164 | 165 | } |
| 165 | 166 | } |
| 166 | - } | |
| 167 | +} | |
| 167 | 168 | |
| 168 | - | |
| 169 | -new GutSlider_Admin(); // initialize the class | |
| 169 | +new GutSlider_Admin(); | |