class-tour-controller.php
169 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SuperbAddons\Tours\Controllers; |
| 4 | |
| 5 | use SuperbAddons\Admin\Controllers\TroubleshootingController; |
| 6 | use SuperbAddons\Components\Admin\Modal; |
| 7 | use SuperbAddons\Data\Controllers\RestController; |
| 8 | use SuperbAddons\Data\Utils\ScriptTranslations; |
| 9 | |
| 10 | defined('ABSPATH') || exit(); |
| 11 | |
| 12 | class TourController |
| 13 | { |
| 14 | const TOUR_GUTENBERG = 'superb-tour-gutenberg'; |
| 15 | const GUTENBERG_TOUR_BLOCKS = 'blocks'; |
| 16 | const GUTENBERG_TOUR_PATTERNS = 'patterns'; |
| 17 | const TOUR_ELEMENTOR = 'superb-tour-elementor'; |
| 18 | |
| 19 | const TOUR_ELEMENTOR_PAGE_ID_OPTION = 'superbaddons_elementor_tour_id'; |
| 20 | |
| 21 | const TOUR_NONCE_ACTION = 'superbaddons-tour'; |
| 22 | const TOUR_NONCE_PARAM = 'superbaddons-tour-nonce'; |
| 23 | |
| 24 | public function __construct() |
| 25 | { |
| 26 | add_action('enqueue_block_editor_assets', array($this, 'MaybeLoadGutenbergTour'), 0); |
| 27 | add_action('elementor/editor/before_enqueue_scripts', array($this, 'MaybeLoadElementorTour'), 0); |
| 28 | } |
| 29 | |
| 30 | public function MaybeLoadGutenbergTour() |
| 31 | { |
| 32 | if (!isset($_GET[self::TOUR_GUTENBERG]) || !isset($_GET[self::TOUR_NONCE_PARAM])) return; |
| 33 | if (!wp_verify_nonce(sanitize_text_field(wp_unslash($_GET[self::TOUR_NONCE_PARAM])), self::TOUR_NONCE_ACTION)) return; |
| 34 | global $pagenow; |
| 35 | if ('post-new.php' !== $pagenow) { |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | $tour_param = sanitize_text_field(wp_unslash($_GET[self::TOUR_GUTENBERG])); |
| 40 | $this->GutenbergTourAssets($tour_param); |
| 41 | } |
| 42 | |
| 43 | public function MaybeLoadElementorTour() |
| 44 | { |
| 45 | if (!isset($_GET[self::TOUR_ELEMENTOR])) return; |
| 46 | global $pagenow; |
| 47 | if ('post.php' !== $pagenow) { |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | if (!isset($_GET['action']) || $_GET['action'] !== 'elementor' || !isset($_GET['post'])) return; |
| 52 | |
| 53 | $tour_page_id = get_option(self::TOUR_ELEMENTOR_PAGE_ID_OPTION); |
| 54 | if (!$tour_page_id || $tour_page_id !== $_GET['post']) return; |
| 55 | |
| 56 | $tour_param = sanitize_text_field(wp_unslash($_GET[self::TOUR_ELEMENTOR])); |
| 57 | if (!wp_verify_nonce($tour_param, 'superbaddons-tour-' . $tour_page_id)) { |
| 58 | return; |
| 59 | } |
| 60 | $this->ElementorTourAssets($tour_param); |
| 61 | } |
| 62 | |
| 63 | private function GutenbergTourAssets($tour_param) |
| 64 | { |
| 65 | $this->TourAssets(); |
| 66 | add_action('admin_footer', function () { |
| 67 | new Modal(); |
| 68 | }); |
| 69 | |
| 70 | if ($tour_param === self::GUTENBERG_TOUR_BLOCKS) { |
| 71 | wp_enqueue_script( |
| 72 | 'superb-addons-guided-tours', |
| 73 | SUPERBADDONS_ASSETS_PATH . '/js/guided-tours/gutenberg-blocks.js', |
| 74 | array('wp-i18n', 'jquery'), |
| 75 | SUPERBADDONS_VERSION, |
| 76 | true |
| 77 | ); |
| 78 | ScriptTranslations::Set('superb-addons-guided-tours'); |
| 79 | } else |
| 80 | if ($tour_param === self::GUTENBERG_TOUR_PATTERNS) { |
| 81 | wp_enqueue_script( |
| 82 | 'superb-addons-guided-tours', |
| 83 | SUPERBADDONS_ASSETS_PATH . '/js/guided-tours/gutenberg-patterns.js', |
| 84 | array('wp-i18n', 'jquery'), |
| 85 | SUPERBADDONS_VERSION, |
| 86 | true |
| 87 | ); |
| 88 | ScriptTranslations::Set('superb-addons-guided-tours'); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | private function ElementorTourAssets($tour_param) |
| 93 | { |
| 94 | $this->TourAssets(); |
| 95 | add_action('elementor/editor/footer', function () { |
| 96 | new Modal(); |
| 97 | }); |
| 98 | wp_enqueue_script( |
| 99 | 'superb-addons-guided-tours', |
| 100 | SUPERBADDONS_ASSETS_PATH . '/js/guided-tours/elementor-sections.js', |
| 101 | array('wp-i18n', 'jquery'), |
| 102 | SUPERBADDONS_VERSION, |
| 103 | true |
| 104 | ); |
| 105 | ScriptTranslations::Set('superb-addons-guided-tours'); |
| 106 | wp_localize_script('superb-addons-guided-tours', 'superbaddonstroubleshooting_g', array( |
| 107 | "rest" => array( |
| 108 | "base" => \get_rest_url(), |
| 109 | "namespace" => RestController::NAMESPACE, |
| 110 | "nonce" => wp_create_nonce("wp_rest"), |
| 111 | "tour_nonce" => isset($tour_param) ? esc_html($tour_param) : false, |
| 112 | "routes" => array( |
| 113 | "tutorial" => TroubleshootingController::TUTORIAL_ROUTE, |
| 114 | ) |
| 115 | ), |
| 116 | )); |
| 117 | } |
| 118 | |
| 119 | private function TourAssets() |
| 120 | { |
| 121 | wp_enqueue_style( |
| 122 | 'superbaddons-driver', |
| 123 | SUPERBADDONS_ASSETS_PATH . '/lib/driver.css', |
| 124 | array(), |
| 125 | SUPERBADDONS_VERSION |
| 126 | ); |
| 127 | wp_enqueue_style( |
| 128 | 'superb-addons-admin-modal', |
| 129 | SUPERBADDONS_ASSETS_PATH . '/css/admin-modal.min.css', |
| 130 | array(), |
| 131 | SUPERBADDONS_VERSION |
| 132 | ); |
| 133 | } |
| 134 | |
| 135 | public static function GetElementorTourURL() |
| 136 | { |
| 137 | $tour_page_id = get_option(self::TOUR_ELEMENTOR_PAGE_ID_OPTION); |
| 138 | $post_status = $tour_page_id ? get_post_status($tour_page_id) : false; |
| 139 | if (!$tour_page_id || $post_status !== 'draft') { |
| 140 | $tour_page_id = wp_insert_post(array( |
| 141 | 'post_title' => __('Superb Addons Tutorial', "superb-blocks"), |
| 142 | 'post_type' => 'page', |
| 143 | 'post_status' => 'draft' |
| 144 | )); |
| 145 | update_option(self::TOUR_ELEMENTOR_PAGE_ID_OPTION, $tour_page_id, false); |
| 146 | } else { |
| 147 | } |
| 148 | |
| 149 | $nonce = wp_create_nonce('superbaddons-tour-' . $tour_page_id); |
| 150 | $edit_link = get_edit_post_link($tour_page_id); |
| 151 | return add_query_arg(array(self::TOUR_ELEMENTOR => $nonce, "action" => "elementor"), $edit_link); |
| 152 | } |
| 153 | |
| 154 | public static function CleanUpTourPage($nonce) |
| 155 | { |
| 156 | $tour_page_id = get_option(self::TOUR_ELEMENTOR_PAGE_ID_OPTION); |
| 157 | if (!$tour_page_id) { |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | if (!wp_verify_nonce($nonce, 'superbaddons-tour-' . $tour_page_id)) { |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | wp_delete_post($tour_page_id, true); |
| 166 | delete_option(self::TOUR_ELEMENTOR_PAGE_ID_OPTION); |
| 167 | } |
| 168 | } |
| 169 |