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