| @@ -1,13 +1,14 @@ | ||
| 1 | 1 | <?php |
| 2 | + | |
| 2 | 3 | namespace Photonic_Plugin\Admin; |
| 3 | 4 | |
| 4 | 5 | use Photonic_Plugin\Admin\Wizard\Wizard; |
| 5 | 6 | use Photonic_Plugin\Core\Photonic; |
| 6 | -use Photonic_Plugin\Modules\SmugMug; | |
| 7 | +use Photonic_Plugin\Platforms\SmugMug; | |
| 7 | 8 | |
| 8 | 9 | class Admin { |
| 9 | - function __construct() { | |
| 10 | + public function __construct() { | |
| 10 | 11 | global $photonic_disable_flow_editor_global; |
| 11 | 12 | |
| 12 | 13 | // General |
| 13 | 14 | add_action('admin_head', [$this, 'admin_head']); |
| @@ -19,26 +20,20 @@ | ||
| 19 | 20 | |
| 20 | 21 | add_action('print_media_templates', [&$this, 'edit_gallery']); |
| 21 | 22 | |
| 22 | 23 | // Gutenberg |
| 23 | - add_action('admin_notices', [&$this, 'admin_notices']); | |
| 24 | 24 | add_action('enqueue_block_editor_assets', [&$this, 'enqueue_gutenberg_assets']); |
| 25 | + add_action('enqueue_block_assets', [&$this, 'enqueue_fse_assets']); // Site Editor; without this, the block doesn't work on the FSE Site Editor | |
| 25 | 26 | |
| 26 | 27 | if (empty($photonic_disable_flow_editor_global)) { |
| 27 | 28 | add_action('media_buttons', [&$this, 'add_photonic_button']); |
| 28 | 29 | add_action('admin_action_photonic_wizard', [&$this, 'open_wizard']); |
| 29 | 30 | add_action('wp_ajax_photonic_wizard_next_screen', [&$this, 'wizard_next_screen']); |
| 30 | - add_action('wp_ajax_nopriv_photonic_wizard_next_screen', [&$this, 'wizard_next_screen']); | |
| 31 | 31 | add_action('wp_ajax_photonic_wizard_more', [&$this, 'flow_more']); |
| 32 | - add_action('wp_ajax_nopriv_photonic_wizard_more', [&$this, 'flow_more']); | |
| 33 | 32 | } |
| 34 | - | |
| 35 | - if (class_exists('\FLBuilderModel') && \FLBuilderModel::is_builder_active()) { | |
| 36 | - $this->enqueue_widget_scripts(); | |
| 37 | - } | |
| 38 | 33 | } |
| 39 | 34 | |
| 40 | - function admin_head() { | |
| 35 | + public function admin_head() { | |
| 41 | 36 | // check user permissions |
| 42 | 37 | if (!current_user_can('edit_posts') && !current_user_can('edit_pages')) { |
| 43 | 38 | return; |
| 44 | 39 | } |
| @@ -43,102 +38,208 @@ | ||
| 43 | 38 | return; |
| 44 | 39 | } |
| 45 | 40 | |
| 46 | 41 | global $photonic_disable_editor, $photonic_disable_editor_post_type; |
| 47 | - $disabled_types = explode(',', $photonic_disable_editor_post_type); | |
| 42 | + $disabled_types = explode(',', esc_attr($photonic_disable_editor_post_type)); | |
| 48 | 43 | $screen = get_current_screen(); |
| 49 | - $post_type = empty($_REQUEST['post_type']) ? 'post' : $_REQUEST['post_type']; | |
| 44 | + | |
| 50 | 45 | // check if WYSIWYG is enabled |
| 51 | - if ('true' == get_user_option('rich_editing') && empty($photonic_disable_editor) && !in_array($post_type, $disabled_types) && $screen->base == 'post') { | |
| 46 | + if (user_can_richedit() && empty($photonic_disable_editor) && !in_array($_REQUEST['post_type'] ?? 'post', $disabled_types, true) && 'post' === $screen->base) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 52 | 47 | $this->prepare_mce_data(); |
| 53 | - add_filter('mce_external_plugins', [$this ,'mce_photonic'], 5); | |
| 54 | - add_filter('mce_buttons', [$this ,'mce_flow_button'], 5); | |
| 48 | + add_filter('mce_external_plugins', [$this, 'mce_photonic'], 5); | |
| 49 | + add_filter('mce_buttons', [$this, 'mce_flow_button'], 5); | |
| 55 | 50 | } |
| 56 | 51 | } |
| 57 | 52 | |
| 58 | - function add_photonic_button() { | |
| 53 | + public function add_photonic_button() { | |
| 59 | 54 | add_thickbox(); |
| 60 | - $url = $this->get_wizard_thickbox_url(); | |
| 55 | + $url = $this->get_wizard_modal_url(); | |
| 61 | 56 | |
| 62 | - printf('<a href="%1$s" class="button photonic-button thickbox" id="photonic-add-gallery" title="Photonic Gallery"><img class="wp-media-buttons-icon" src="'.PHOTONIC_URL.'include/images/Photonic-20.png'.'" alt="%2$s" /> %2$s</a>', | |
| 63 | - $url, esc_html__( 'Add / Edit Photonic Gallery', 'photonic')); | |
| 57 | + printf( | |
| 58 | + '<a href="%1$s" class="button photonic-button thickbox" id="photonic-add-gallery" title="Photonic Gallery"><img class="wp-media-buttons-icon" src="' . esc_url(PHOTONIC_URL) . 'include/images/Photonic-20.png" alt="%2$s" /> %2$s</a>', | |
| 59 | + esc_url($url), | |
| 60 | + esc_html__('Add / Edit Photonic Gallery', 'photonic') | |
| 61 | + ); | |
| 64 | 62 | } |
| 65 | 63 | |
| 66 | - function open_wizard() { | |
| 67 | - define( 'IFRAME_REQUEST', true ); | |
| 64 | + public function open_wizard() { | |
| 65 | + define('IFRAME_REQUEST', true); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals | |
| 68 | 66 | $this->enqueue_wizard_scripts(); |
| 69 | 67 | iframe_header(esc_html__('Add / Edit Photonic Gallery', 'photonic')); |
| 70 | - require_once(PHOTONIC_PATH.'/Admin/Wizard/Screen_Flow.php'); | |
| 68 | + require_once PHOTONIC_PATH . '/Admin/Wizard/Screen_Flow.php'; | |
| 71 | 69 | iframe_footer(); |
| 72 | 70 | exit; |
| 73 | 71 | } |
| 74 | 72 | |
| 75 | - function enqueue_wizard_scripts() { | |
| 76 | - global $photonic_alternative_shortcode; | |
| 77 | - $wizard_js = [ | |
| 78 | - 'ajaxurl' => admin_url('admin-ajax.php'), | |
| 79 | - 'shortcode' => empty($photonic_alternative_shortcode) ? 'gallery' : $photonic_alternative_shortcode, | |
| 80 | - 'insert_gallery' => esc_html__('Insert Gallery', 'photonic'), | |
| 81 | - 'update_gallery' => esc_html__('Update Gallery', 'photonic'), | |
| 82 | - 'error_mandatory' => esc_html__('Please fill the mandatory fields. Mandatory fields are marked with a red "*".', 'photonic'), | |
| 83 | - 'media_library_title' => esc_html__('Select from WordPress Media Library', 'photonic'), | |
| 84 | - 'media_library_button' => esc_html__('Select', 'photonic'), | |
| 85 | - 'info_editor_not_shortcode' => esc_html__('The text selected in the editor is not a Photonic shortcode. Creating a new shortcode.', 'photonic'), | |
| 86 | - 'info_editor_block_select' => sprintf(esc_html__('%1$sHint:%2$s To edit an existing Photonic block simply click on the block.', 'photonic'), '<strong>', '</strong>'), | |
| 87 | - ]; | |
| 88 | - if (!empty($_REQUEST['shortcode'])) { | |
| 89 | - $wizard_js['shortcode'] = $_REQUEST['shortcode']; | |
| 73 | + public function enqueue_wizard_scripts() { | |
| 74 | + if (current_user_can('edit_posts') && isset($_REQUEST['nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['nonce'])), 'photonic-wizard-' . get_current_user_id())) { | |
| 75 | + global $photonic_alternative_shortcode, $photonic_debug_on; | |
| 76 | + $safe_origin = ''; | |
| 77 | + $url_parts = wp_parse_url(home_url()); | |
| 78 | + $safe_origin_scheme = $url_parts['scheme'] ?? ''; | |
| 79 | + $safe_origin_separator = empty($safe_origin_scheme) ? '' : '://'; | |
| 80 | + $safe_origin_port = empty($url_parts['port']) ? '' : ':' . $url_parts['port']; | |
| 81 | + $safe_origin .= $safe_origin_scheme . $safe_origin_separator . $url_parts['host'] . $safe_origin_port; | |
| 82 | + $wizard_js = [ | |
| 83 | + 'ajaxurl' => admin_url('admin-ajax.php'), | |
| 84 | + 'shortcode' => sanitize_text_field($photonic_alternative_shortcode ?: 'gallery'), | |
| 85 | + 'insert_gallery' => esc_html__('Insert Gallery', 'photonic'), | |
| 86 | + 'update_gallery' => esc_html__('Update Gallery', 'photonic'), | |
| 87 | + 'error_mandatory' => esc_html__('Please fill the mandatory fields. Mandatory fields are marked with a red "*".', 'photonic'), | |
| 88 | + 'media_library_title' => esc_html__('Select from WordPress Media Library', 'photonic'), | |
| 89 | + 'media_library_button' => esc_html__('Select', 'photonic'), | |
| 90 | + 'info_editor_not_shortcode' => esc_html__('The text selected in the editor is not a Photonic shortcode. Creating a new shortcode.', 'photonic'), | |
| 91 | + 'info_editor_google_shortcode' => esc_html__('The text selected in the editor is Photonic shortcode for Google Photos. With effect from April 2025, Google Photos is no longer supported. Creating a new shortcode.', 'photonic'), | |
| 92 | + /* Translators: 1: Open HTML tag 2: Close HTML tag */ | |
| 93 | + 'info_editor_block_select' => sprintf(esc_html__('%1$sHint:%2$s To edit an existing Photonic block simply click on the block.', 'photonic'), '<strong>', '</strong>'), | |
| 94 | + 'safe_origin' => $safe_origin, | |
| 95 | + 'debug_on' => !empty($photonic_debug_on), | |
| 96 | + ]; | |
| 97 | + if (!empty($_REQUEST['shortcode'])) { | |
| 98 | + $wizard_js['shortcode'] = sanitize_text_field($_REQUEST['shortcode']); | |
| 99 | + } | |
| 100 | + wp_enqueue_style('photonic-flow', PHOTONIC_URL . 'include/css/admin/admin-flow.css', [], Photonic::get_version(PHOTONIC_PATH . '/include/css/admin/admin-flow.css')); | |
| 101 | + wp_enqueue_script('photonic-flow-js', PHOTONIC_URL . 'include/js/admin/wizard.js', ['jquery'], Photonic::get_version(PHOTONIC_PATH . '/include/js/admin/wizard.js'), false); | |
| 102 | + wp_localize_script('photonic-flow-js', 'Photonic_Wizard_JS', $wizard_js); | |
| 90 | 103 | } |
| 91 | - wp_enqueue_style('photonic-flow', PHOTONIC_URL.'include/css/admin/admin-flow.css', [], Photonic::get_version(PHOTONIC_PATH.'/include/css/admin/admin-flow.css')); | |
| 92 | - wp_enqueue_script('photonic-flow-js', PHOTONIC_URL.'include/scripts/admin/flow.js', ['jquery'], Photonic::get_version(PHOTONIC_PATH.'/include/scripts/admin/flow.js')); | |
| 93 | - wp_localize_script('photonic-flow-js', 'Photonic_Wizard_JS', $wizard_js); | |
| 94 | 104 | } |
| 95 | 105 | |
| 96 | - function wizard_next_screen() { | |
| 97 | - require_once(PHOTONIC_PATH."/Admin/Wizard/Wizard.php"); | |
| 98 | - if (isset($_POST['provider'])) { | |
| 99 | - $wizard = new Wizard(); | |
| 100 | - echo $wizard->get_screen(); | |
| 106 | + public function wizard_next_screen() { | |
| 107 | + if (check_ajax_referer('photonic-wizard-next-' . get_current_user_id())) { | |
| 108 | + require_once PHOTONIC_PATH . '/Admin/Wizard/Wizard.php'; | |
| 109 | + if (isset($_POST['provider'])) { | |
| 110 | + $safe_tags = array_merge_recursive( | |
| 111 | + [ | |
| 112 | + 'form' => [ | |
| 113 | + 'id' => true, | |
| 114 | + 'class' => true, | |
| 115 | + 'name' => true, | |
| 116 | + 'data-photonic-submission' => true, | |
| 117 | + 'data-photonic-submission-pending' => true, | |
| 118 | + ], | |
| 119 | + | |
| 120 | + 'input' => [ | |
| 121 | + 'type' => true, | |
| 122 | + 'id' => true, | |
| 123 | + 'name' => true, | |
| 124 | + 'value' => true, | |
| 125 | + 'checked' => true, | |
| 126 | + 'class' => true, | |
| 127 | + 'data-photonic-option-condition' => true, | |
| 128 | + ], | |
| 129 | + | |
| 130 | + 'label' => [ | |
| 131 | + 'class' => true, | |
| 132 | + 'for' => true, | |
| 133 | + ], | |
| 134 | + | |
| 135 | + 'select' => [ | |
| 136 | + 'name' => true, | |
| 137 | + 'id' => true, | |
| 138 | + 'class' => true, | |
| 139 | + 'aria-describedby' => true, | |
| 140 | + ], | |
| 141 | + | |
| 142 | + 'option' => [ | |
| 143 | + 'value' => true, | |
| 144 | + 'selected' => true, | |
| 145 | + 'data-photonic-option-condition' => true, | |
| 146 | + ], | |
| 147 | + | |
| 148 | + // Custom data attributes | |
| 149 | + 'div' => [ | |
| 150 | + 'data-current-screen' => true, | |
| 151 | + | |
| 152 | + 'data-photonic-flow-selector-for' => true, | |
| 153 | + 'data-photonic-flow-selector-mode' => true, | |
| 154 | + | |
| 155 | + 'data-photonic-condition' => true, | |
| 156 | + 'data-photonic-flow-sequence' => true, | |
| 157 | + 'data-photonic-flow-sequence-group' => true, | |
| 158 | + | |
| 159 | + 'data-photonic-flow-selection-id' => true, | |
| 160 | + 'data-photonic-selection-alt-id' => true, | |
| 161 | + 'data-photonic-selection-alt-id-2' => true, | |
| 162 | + ], | |
| 163 | + | |
| 164 | + 'span' => [ | |
| 165 | + 'data-photonic-selection-id' => true, | |
| 166 | + ], | |
| 167 | + | |
| 168 | + 'ol' => [ | |
| 169 | + 'data-photonic-date-filter' => true, | |
| 170 | + 'data-photonic-filter-count' => true, | |
| 171 | + 'data-photonic-date-range-filter' => true, | |
| 172 | + ], | |
| 173 | + | |
| 174 | + 'a' => [ | |
| 175 | + 'data-photonic-add-date' => true, | |
| 176 | + 'data-photonic-mark-for' => true, | |
| 177 | + 'data-photonic-more-link' => true, | |
| 178 | + 'data-photonic-display-type' => true, | |
| 179 | + 'data-photonic-provider' => true, | |
| 180 | + 'data-photonic-nonce' => true, | |
| 181 | + ] | |
| 182 | + ], | |
| 183 | + wp_kses_allowed_html('post') | |
| 184 | + ); | |
| 185 | + | |
| 186 | + $wizard = new Wizard(); | |
| 187 | + // echo $wizard->get_screen(); // Cannot escape ... has form and JS markup; individual parts are escaped as appropriate. | |
| 188 | + echo wp_kses($wizard->get_screen(), $safe_tags); // Cannot escape ... has form and JS markup; individual parts are escaped as appropriate. | |
| 189 | + } | |
| 101 | 190 | } |
| 102 | 191 | die(); |
| 103 | 192 | } |
| 104 | 193 | |
| 105 | - function flow_more() { | |
| 106 | - require_once(PHOTONIC_PATH."/Admin/Wizard/Wizard.php"); | |
| 107 | - if (isset($_POST['url']) && isset($_POST['provider']) && isset($_POST['display_type'])) { | |
| 108 | - $url = base64_decode(sanitize_text_field($_POST['url'])); | |
| 194 | + public function flow_more() { | |
| 195 | + if (check_ajax_referer('photonic-wizard-more-' . get_current_user_id())) { | |
| 196 | + require_once PHOTONIC_PATH . '/Admin/Wizard/Wizard.php'; | |
| 197 | + if (isset($_POST['url']) && isset($_POST['provider']) && isset($_POST['display_type'])) { | |
| 198 | + $url = base64_decode(sanitize_text_field($_POST['url'])); // The `url` for fetching additional results is base64-encoded in the wizard in wizard.js using `btoa` | |
| 109 | 199 | |
| 110 | - $provider = sanitize_text_field($_POST['provider']); | |
| 111 | - $display_type = sanitize_text_field($_POST['display_type']); | |
| 112 | - $args = ['sslverify' => PHOTONIC_SSL_VERIFY]; | |
| 113 | - if ($provider == 'smugmug') { | |
| 114 | - $gallery = SmugMug::get_instance(); | |
| 200 | + $provider = sanitize_text_field($_POST['provider']); | |
| 201 | + $display_type = sanitize_text_field($_POST['display_type']); | |
| 202 | + $existing = []; | |
| 203 | + if (!empty($_POST['filter'])) { | |
| 204 | + $existing['selected_data'] = sanitize_text_field($_POST['filter']); | |
| 205 | + } | |
| 206 | + $args = ['sslverify' => PHOTONIC_SSL_VERIFY]; | |
| 207 | + if ('smugmug' === $provider) { | |
| 208 | + require_once PHOTONIC_PATH . '/Platforms/SmugMug.php'; | |
| 209 | + $gallery = SmugMug::get_instance(); | |
| 115 | 210 | |
| 116 | - $body = [ | |
| 117 | - 'APIKey' => $gallery->api_key, | |
| 118 | - '_accept' => 'application/json', | |
| 119 | - '_expandmethod' => 'inline', | |
| 120 | - '_verbosity' => '1', | |
| 121 | - ]; | |
| 211 | + $body = [ | |
| 212 | + 'APIKey' => $gallery->api_key, | |
| 213 | + '_accept' => 'application/json', | |
| 214 | + '_expandmethod' => 'inline', | |
| 215 | + '_verbosity' => '1', | |
| 216 | + ]; | |
| 122 | 217 | |
| 123 | - if ($display_type == 'album-photo' || $display_type == 'multi-album') { | |
| 124 | - $body['_expand'] = 'HighlightImage.ImageSizes'; | |
| 218 | + if ('album-photo' === $display_type || 'multi-album' === $display_type) { | |
| 219 | + $body['_expand'] = 'HighlightImage.ImageSizes'; | |
| 220 | + } | |
| 221 | + | |
| 222 | + $args['body'] = $body; | |
| 125 | 223 | } |
| 126 | 224 | |
| 127 | - $args['body'] = $body; | |
| 128 | - } | |
| 225 | + // URL has come via base64, so we will check that it is directing to a whitelist of hosts... | |
| 226 | + $url_parts = wp_parse_url($url); | |
| 227 | + if (in_array($url_parts['host'], Wizard::base_apis(), true)) { | |
| 228 | + $response = wp_remote_request($url, $args); | |
| 229 | + $wizard = new Wizard(); | |
| 230 | + $objects = $wizard->process_response($response, $provider, $display_type, [], $existing, $url, true); | |
| 129 | 231 | |
| 130 | -// $response = wp_remote_request($url, ['sslverify' => PHOTONIC_SSL_VERIFY]); | |
| 131 | - $response = wp_remote_request($url, $args); | |
| 132 | - $wizard = new Wizard(); | |
| 133 | - $objects = $wizard->process_response($response, $provider, $display_type, [], [], $url, true); | |
| 134 | - | |
| 135 | - if (!empty($objects['success'])) { | |
| 136 | - echo $objects['success']; | |
| 232 | + if (!empty($objects['success'])) { | |
| 233 | + echo wp_kses_post($objects['success']); | |
| 234 | + } | |
| 235 | + elseif (!empty($objects['error'])) { | |
| 236 | + if ($objects['error'] !== $wizard->error_no_data_returned) { // Typically happens if there is a "next token" without corresponding data | |
| 237 | + echo wp_kses_post($objects['error']); | |
| 238 | + } | |
| 239 | + } | |
| 240 | + } | |
| 137 | 241 | } |
| 138 | - else if (!empty($objects['error'])) { | |
| 139 | - echo $objects['error']; | |
| 140 | - } | |
| 141 | 242 | } |
| 142 | 243 | die(); |
| 143 | 244 | } |
| 144 | 245 | |
| @@ -147,69 +248,84 @@ | ||
| 147 | 248 | * |
| 148 | 249 | * @param $hook |
| 149 | 250 | * @return void |
| 150 | 251 | */ |
| 151 | - function add_admin_scripts($hook) { | |
| 152 | - if ('media-upload-popup' == $hook) { | |
| 252 | + public function add_admin_scripts($hook) { | |
| 253 | + if ('media-upload-popup' === $hook) { | |
| 153 | 254 | wp_enqueue_script('jquery'); |
| 154 | - wp_enqueue_style('photonic-upload', PHOTONIC_URL.'include/css/admin/admin-form.css', [], Photonic::get_version(PHOTONIC_PATH.'/include/css/admin/admin-form.css')); | |
| 255 | + wp_enqueue_style('photonic-upload', PHOTONIC_URL . 'include/css/admin/admin-form.css', [], Photonic::get_version(PHOTONIC_PATH . '/include/css/admin/admin-form.css')); | |
| 155 | 256 | } |
| 156 | - else if ('post-new.php' == $hook || 'post.php' == $hook) { | |
| 157 | - global $photonic_disable_editor, $photonic_disable_editor_post_type; | |
| 257 | + elseif ('post-new.php' === $hook || 'post.php' === $hook) { | |
| 258 | + global $photonic_disable_editor, $photonic_disable_editor_post_type, $photonic_alternative_shortcode; | |
| 158 | 259 | $disabled_types = explode(',', $photonic_disable_editor_post_type); |
| 159 | - $post_type = empty($_REQUEST['post_type']) ? 'post' : $_REQUEST['post_type']; | |
| 160 | - wp_enqueue_style('photonic-upload', PHOTONIC_URL.'include/css/admin/admin-form.css', [], Photonic::get_version(PHOTONIC_PATH.'/include/css/admin/admin-form.css')); | |
| 161 | - if (empty($photonic_disable_editor) && !in_array($post_type, $disabled_types)) { | |
| 260 | + wp_enqueue_style('photonic-upload', PHOTONIC_URL . 'include/css/admin/admin-form.css', [], Photonic::get_version(PHOTONIC_PATH . '/include/css/admin/admin-form.css')); | |
| 261 | + wp_enqueue_script('photonic-native-ui', PHOTONIC_URL . 'include/js/admin/native-ui.js', ['shortcode', 'thickbox'], Photonic::get_version(PHOTONIC_PATH . '/include/js/admin/native-ui.js'), false); | |
| 262 | + wp_enqueue_script('photonic-editor', PHOTONIC_URL . 'include/js/admin/editor.js', ['photonic-native-ui'], Photonic::get_version(PHOTONIC_PATH . '/include/js/admin/editor.js'), false); | |
| 263 | + $editor_js = [ | |
| 264 | + 'shortcode' => sanitize_text_field($photonic_alternative_shortcode ?: 'gallery'), | |
| 265 | + ]; | |
| 266 | + wp_localize_script('photonic-editor', 'Photonic_Editor_JS', $editor_js); | |
| 267 | + | |
| 268 | + if (empty($photonic_disable_editor) && !in_array($_REQUEST['post_type'] ?? 'post', $disabled_types, true)) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 162 | 269 | $this->prepare_mce_data(); |
| 163 | 270 | |
| 164 | - add_editor_style(PHOTONIC_URL.'include/css/admin/admin-editor.css?'.Photonic::get_version(PHOTONIC_PATH.'/include/css/admin/admin-editor.css')); | |
| 271 | + add_editor_style(PHOTONIC_URL . 'include/css/admin/admin-editor.css?' . Photonic::get_version(PHOTONIC_PATH . '/include/css/admin/admin-editor.css')); | |
| 165 | 272 | } |
| 166 | 273 | } |
| 167 | - else if ('widgets.php' == $hook) { | |
| 274 | + elseif ('widgets.php' === $hook) { | |
| 168 | 275 | Photonic::enqueue_widget_scripts(); |
| 169 | 276 | } |
| 170 | 277 | } |
| 171 | 278 | |
| 172 | - function prepare_mce_data() { | |
| 173 | - $url = $this->get_wizard_thickbox_url(); | |
| 279 | + public function prepare_mce_data() { | |
| 280 | + $url = $this->get_wizard_modal_url(); | |
| 174 | 281 | $js_array = $this->get_wizard_js_parameters($url); |
| 175 | 282 | |
| 176 | - wp_enqueue_script('photonic-admin-js', PHOTONIC_URL.'include/scripts/admin/gallery-settings.js', ['jquery', 'media-views', 'media-upload'], Photonic::get_version(PHOTONIC_PATH.'/include/scripts/admin/gallery-settings.js')); | |
| 283 | + wp_enqueue_script('photonic-admin-js', PHOTONIC_URL . 'include/js/admin/gallery-settings.js', ['jquery', 'media-views', 'media-upload'], Photonic::get_version(PHOTONIC_PATH . '/include/js/admin/gallery-settings.js'), false); | |
| 177 | 284 | wp_localize_script('photonic-admin-js', 'Photonic_Admin_JS', $js_array); |
| 178 | 285 | } |
| 179 | 286 | |
| 180 | - function mce_photonic($plugin_array) { | |
| 181 | - $plugin_array['photonic'] = PHOTONIC_URL.'include/scripts/admin/mce.js?'.Photonic::get_version(PHOTONIC_PATH.'/include/scripts/admin/mce.js'); | |
| 287 | + public function mce_photonic($plugin_array) { | |
| 288 | + $plugin_array['photonic'] = PHOTONIC_URL . 'include/js/admin/mce.js?' . Photonic::get_version(PHOTONIC_PATH . '/include/js/admin/mce.js'); | |
| 182 | 289 | return $plugin_array; |
| 183 | 290 | } |
| 184 | 291 | |
| 185 | - function mce_flow_button($buttons) { | |
| 292 | + public function mce_flow_button($buttons) { | |
| 186 | 293 | array_push($buttons, 'photonic_wizard'); |
| 187 | 294 | return $buttons; |
| 188 | 295 | } |
| 189 | 296 | |
| 190 | - function enqueue_gutenberg_assets() { | |
| 191 | - if (function_exists('register_block_type')) { | |
| 192 | - wp_enqueue_script('photonic-gutenberg', | |
| 193 | - PHOTONIC_URL.'include/scripts/admin/block.js', | |
| 194 | - ['jquery', 'wp-blocks', 'wp-i18n', 'wp-element', 'shortcode', 'thickbox'], | |
| 195 | - Photonic::get_version(PHOTONIC_PATH.'/include/scripts/admin/block.js') | |
| 196 | - ); | |
| 297 | + public function enqueue_gutenberg_assets() { | |
| 298 | + wp_enqueue_script('photonic-native-ui', PHOTONIC_URL . 'include/js/admin/native-ui.js', ['shortcode', 'thickbox'], Photonic::get_version(PHOTONIC_PATH . '/include/js/admin/native-ui.js'), false); | |
| 299 | + wp_enqueue_script( | |
| 300 | + 'photonic-gutenberg', | |
| 301 | + PHOTONIC_URL . 'include/js/admin/block.js', | |
| 302 | + ['wp-blocks', 'wp-i18n', 'wp-element', 'shortcode', 'thickbox', 'photonic-native-ui'], | |
| 303 | + Photonic::get_version(PHOTONIC_PATH . '/include/js/admin/block.js'), | |
| 304 | + false | |
| 305 | + ); | |
| 197 | 306 | |
| 198 | - if (function_exists( 'gutenberg_get_jed_locale_data')) { | |
| 199 | - $locale = gutenberg_get_jed_locale_data('photonic'); | |
| 200 | - $content = 'wp.i18n.setLocaleData('.json_encode($locale).', "photonic");'; | |
| 201 | - wp_script_add_data( 'photonic-gutenberg', 'data', $content ); | |
| 202 | - } | |
| 307 | + wp_set_script_translations('photonic-gutenberg', 'photonic'); | |
| 203 | 308 | |
| 204 | - $url = $this->get_wizard_thickbox_url(); | |
| 205 | - $js_array = $this->get_wizard_js_parameters($url); | |
| 206 | - wp_localize_script('photonic-gutenberg', 'Photonic_Gutenberg_JS', $js_array); | |
| 309 | + $url = $this->get_wizard_modal_url(); | |
| 310 | + $js_array = $this->get_wizard_js_parameters($url); | |
| 311 | + wp_localize_script('photonic-gutenberg', 'Photonic_Gutenberg_JS', $js_array); | |
| 207 | 312 | |
| 208 | - wp_enqueue_style('photonic-gutenberg', | |
| 209 | - PHOTONIC_URL.'include/css/admin/admin-block.css', | |
| 313 | + wp_enqueue_style( | |
| 314 | + 'photonic-gutenberg', | |
| 315 | + PHOTONIC_URL . 'include/css/admin/admin-block.css', | |
| 316 | + ['thickbox'], | |
| 317 | + Photonic::get_version(PHOTONIC_PATH . '/include/css/admin/admin-block.css') | |
| 318 | + ); | |
| 319 | + } | |
| 320 | + | |
| 321 | + public function enqueue_fse_assets() { | |
| 322 | + if (is_admin()) { | |
| 323 | + wp_enqueue_style( | |
| 324 | + 'photonic-gutenberg', | |
| 325 | + PHOTONIC_URL . 'include/css/admin/admin-block.css', | |
| 210 | 326 | ['thickbox'], |
| 211 | - Photonic::get_version(PHOTONIC_PATH.'/include/css/admin/admin-block.css') | |
| 327 | + Photonic::get_version(PHOTONIC_PATH . '/include/css/admin/admin-block.css') | |
| 212 | 328 | ); |
| 213 | 329 | } |
| 214 | 330 | } |
| 215 | 331 | |
| @@ -218,10 +334,11 @@ | ||
| 218 | 334 | * |
| 219 | 335 | * @param $tabs |
| 220 | 336 | * @return array |
| 221 | 337 | */ |
| 222 | - function media_upload_tabs($tabs) { | |
| 223 | - if (!function_exists('is_gutenberg_page') || (function_exists('is_gutenberg_page') && !is_gutenberg_page())) { | |
| 338 | + public function media_upload_tabs($tabs): array { | |
| 339 | + $current_screen = get_current_screen(); | |
| 340 | + if (!empty($current_screen) && $current_screen->is_block_editor()) { | |
| 224 | 341 | $tabs['photonic'] = 'Photonic'; |
| 225 | 342 | } |
| 226 | 343 | return $tabs; |
| 227 | 344 | } |
| @@ -230,9 +347,9 @@ | ||
| 230 | 347 | * Invokes the form to display the photonic insertion screen in the "Add Media" panel. The call to wp_iframe ensures that the right CSS and JS are called. |
| 231 | 348 | * |
| 232 | 349 | * @return void |
| 233 | 350 | */ |
| 234 | - function media_upload_photonic() { | |
| 351 | + public function media_upload_photonic() { | |
| 235 | 352 | wp_iframe([&$this, 'media_upload_photonic_form']); |
| 236 | 353 | } |
| 237 | 354 | |
| 238 | 355 | /** |
| @@ -239,20 +356,19 @@ | ||
| 239 | 356 | * First prints the standard buttons for media upload, then shows the UI for Photonic. |
| 240 | 357 | * |
| 241 | 358 | * @return void |
| 242 | 359 | */ |
| 243 | - function media_upload_photonic_form() { | |
| 360 | + public function media_upload_photonic_form() { | |
| 244 | 361 | media_upload_header(); |
| 245 | - require_once(PHOTONIC_PATH."/Admin/Forms/Add_Gallery.php"); | |
| 362 | + require_once PHOTONIC_PATH . '/Admin/Forms/Add_Gallery.php'; | |
| 246 | 363 | } |
| 247 | 364 | |
| 248 | - function edit_gallery() { | |
| 365 | + public function edit_gallery() { | |
| 249 | 366 | global $photonic_disable_editor, $photonic_disable_editor_post_type; |
| 250 | 367 | $disabled_types = explode(',', $photonic_disable_editor_post_type); |
| 251 | - $post_type = empty($_REQUEST['post_type']) ? 'post' : $_REQUEST['post_type']; | |
| 252 | 368 | // check if WYSIWYG is enabled |
| 253 | - if ('true' == get_user_option('rich_editing') && empty($photonic_disable_editor) && !in_array($post_type, $disabled_types)) { | |
| 254 | - require_once(PHOTONIC_PATH."/Admin/Forms/Edit_Gallery_Templates.php"); | |
| 369 | + if (user_can_richedit() && empty($photonic_disable_editor) && !in_array($_REQUEST['post_type'] ?? 'post', $disabled_types, true)) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 370 | + require_once PHOTONIC_PATH . '/Admin/Forms/Edit_Gallery_Templates.php'; | |
| 255 | 371 | } |
| 256 | 372 | } |
| 257 | 373 | |
| 258 | 374 | /** |
| @@ -257,18 +373,28 @@ | ||
| 257 | 373 | |
| 258 | 374 | /** |
| 259 | 375 | * @return string |
| 260 | 376 | */ |
| 261 | - private function get_wizard_thickbox_url() { | |
| 262 | - $url = add_query_arg([ | |
| 263 | - 'action' => 'photonic_wizard', | |
| 264 | - 'class' => 'photonic-flow', | |
| 265 | - 'post_id' => empty($_REQUEST['post']) ? '' : $_REQUEST['post'], | |
| 266 | - 'width' => '1000', | |
| 267 | - 'height' => '600', | |
| 268 | - 'TB_iframe' => 'true', | |
| 269 | - ], admin_url('admin.php')); | |
| 270 | - return $url; | |
| 377 | + private function get_wizard_modal_url(): string { | |
| 378 | + $user = get_current_user_id(); | |
| 379 | + if (0 === $user) { | |
| 380 | + $user = wp_rand(1); | |
| 381 | + } | |
| 382 | + | |
| 383 | + $post = empty($_REQUEST['post']) ? '' : sanitize_text_field($_REQUEST['post']); // phpcs:ignore WordPress.Security.NonceVerification | |
| 384 | + | |
| 385 | + return add_query_arg( | |
| 386 | + [ | |
| 387 | + 'action' => 'photonic_wizard', | |
| 388 | + 'class' => 'photonic-flow', | |
| 389 | + 'post_id' => $post, | |
| 390 | + 'nonce' => wp_create_nonce('photonic-wizard-' . $user), | |
| 391 | + 'width' => '1000', | |
| 392 | + 'height' => '600', | |
| 393 | + 'TB_iframe' => 'true', | |
| 394 | + ], | |
| 395 | + admin_url('admin.php') | |
| 396 | + ); | |
| 271 | 397 | } |
| 272 | 398 | |
| 273 | 399 | /** |
| 274 | 400 | * @param string $url |
| @@ -273,34 +399,18 @@ | ||
| 273 | 399 | /** |
| 274 | 400 | * @param string $url |
| 275 | 401 | * @return array |
| 276 | 402 | */ |
| 277 | - private function get_wizard_js_parameters(string $url) { | |
| 403 | + private function get_wizard_js_parameters($url): array { | |
| 278 | 404 | global $photonic_alternative_shortcode, $photonic_disable_flow_editor, $photonic_disable_flow_editor_global; |
| 279 | - $js_array = [ | |
| 280 | - 'flow_url' => $url, | |
| 281 | - 'ajaxurl' => admin_url('admin-ajax.php'), | |
| 282 | - 'shortcode' => empty($photonic_alternative_shortcode) ? 'gallery' : $photonic_alternative_shortcode, | |
| 283 | - 'disable_flow' => !empty($photonic_disable_flow_editor) || !empty($photonic_disable_flow_editor_global), | |
| 405 | + return [ | |
| 406 | + 'flow_url' => $url, | |
| 407 | + 'ajaxurl' => admin_url('admin-ajax.php'), | |
| 408 | + 'shortcode' => sanitize_text_field($photonic_alternative_shortcode ?: 'gallery'), | |
| 409 | + 'disable_flow' => !empty($photonic_disable_flow_editor) || !empty($photonic_disable_flow_editor_global), | |
| 284 | 410 | 'default_gallery_type' => 'default', |
| 285 | - 'plugin_dir' => plugin_dir_url(__FILE__), | |
| 411 | + 'plugin_dir' => plugin_dir_url(__FILE__), | |
| 286 | 412 | ]; |
| 287 | - return $js_array; | |
| 288 | 413 | } |
| 289 | - | |
| 290 | - function admin_notices() { | |
| 291 | - if (!empty($_REQUEST['page']) && in_array($_REQUEST['page'], ['photonic-options-manager', 'photonic-getting-started', 'photonic-auth', 'photonic-helpers'])){ | |
| 292 | - if (function_exists('register_block_type')) { | |
| 293 | - $dismissed = get_user_meta(get_current_user_id(), 'photonic_gutenberg'); | |
| 294 | - if (!$dismissed) { | |
| 295 | - echo "<div class='notice notice-warning'>\n<p>\n"; | |
| 296 | - echo sprintf(esc_html__('It looks like you are using the Gutenberg editor. Please ensure that you have followed the instructions under %s.', 'photonic'), '<em><a href="'.admin_url('admin.php?page=photonic-gutenberg').'">Photonic → Switch to Gutenberg</a></em>'); | |
| 297 | - echo "</p>\n"; | |
| 298 | - echo "<button type='button' data-photonic-dismissible='gutenberg' class='photonic-notice-dismiss'><span class='screen-reader-text'>Dismiss this notice.</span></button>"; | |
| 299 | - echo "</div>\n"; | |
| 300 | - } | |
| 301 | - } | |
| 302 | - } | |
| 303 | - } | |
| 304 | 414 | } |
| 305 | 415 | |
| 306 | -new Admin(); | |
| 416 | +new Admin(); | |