Preview
3 days ago
views
3 days ago
Editor.php
3 weeks ago
Iframe.php
2 months ago
TheFrontend.php
2 months ago
TheFrontendHooks.php
2 months ago
Editor.php
275 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Editor controller |
| 4 | * |
| 5 | * @package kirki |
| 6 | */ |
| 7 | |
| 8 | namespace Kirki\Frontend; |
| 9 | |
| 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 11 | exit; // Exit if accessed directly. |
| 12 | } |
| 13 | |
| 14 | use Kirki\Ajax\WpAdmin; |
| 15 | use Kirki\HelperFunctions; |
| 16 | use Kirki\Staging; |
| 17 | |
| 18 | /** |
| 19 | * Editor class for controlling editor page template and hooks |
| 20 | */ |
| 21 | class Editor { |
| 22 | |
| 23 | |
| 24 | /** |
| 25 | * Initialize the class |
| 26 | * |
| 27 | * @return void |
| 28 | */ |
| 29 | public function __construct() { |
| 30 | $this->render_editor(); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Render the editor page |
| 35 | * |
| 36 | * @return void |
| 37 | */ |
| 38 | public function render_editor() { |
| 39 | if ( ! $this->has_editor_access() ) { |
| 40 | //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped,WordPress.WP.I18n.NonSingularStringLiteralDomain |
| 41 | wp_die( __( 'Sorry you are not allowed to access this page', 'kirki' ), 403 ); |
| 42 | } |
| 43 | add_filter( 'template_include', array( $this, 'load_my_editor_view' ), PHP_INT_MAX ); |
| 44 | |
| 45 | add_action( 'wp_enqueue_scripts', array( $this, 'load_assets' ), 1 ); |
| 46 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts_for_iframe' ), PHP_INT_MAX ); |
| 47 | add_action( 'wp_enqueue_scripts', array( $this, 'load_script_text_domain' ), 1 ); |
| 48 | |
| 49 | // add_action( 'wp_enqueue_scripts', array( new HelperFunctions(), 'remove_theme_style' ), 100 ); |
| 50 | add_action( 'wp_enqueue_scripts', array( new HelperFunctions(), 'dequeue_all_except_my_plugin' ), 100 ); // Low priority to run late |
| 51 | |
| 52 | add_action( 'wp_footer', array( $this, 'add_before_body_tag_end' ) ); |
| 53 | |
| 54 | add_action( 'wp_default_scripts', array( $this, 'remove_jquery_migrate' ) ); |
| 55 | add_action( 'init', array( $this, 'disable_wp_emojicons' ) ); |
| 56 | |
| 57 | } |
| 58 | |
| 59 | public function disable_wp_emojicons() { |
| 60 | // Remove emoji script and styles from front-end and admin |
| 61 | remove_action( 'wp_head', 'print_emoji_detection_script', 7 ); |
| 62 | remove_action( 'admin_print_scripts', 'print_emoji_detection_script' ); |
| 63 | remove_action( 'wp_print_styles', 'print_emoji_styles' ); |
| 64 | remove_action( 'admin_print_styles', 'print_emoji_styles' ); |
| 65 | |
| 66 | // Remove from RSS feeds |
| 67 | remove_filter( 'the_content_feed', 'wp_staticize_emoji' ); |
| 68 | remove_filter( 'comment_text_rss', 'wp_staticize_emoji' ); |
| 69 | |
| 70 | // Remove from emails |
| 71 | remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' ); |
| 72 | |
| 73 | // Remove TinyMCE emoji plugin |
| 74 | add_filter( |
| 75 | 'tiny_mce_plugins', |
| 76 | function ( $plugins ) { |
| 77 | return is_array( $plugins ) ? array_diff( $plugins, array( 'wpemoji' ) ) : array(); |
| 78 | } |
| 79 | ); |
| 80 | |
| 81 | // Disable DNS prefetch |
| 82 | add_filter( 'emoji_svg_url', '__return_false' ); |
| 83 | } |
| 84 | |
| 85 | public function remove_jquery_migrate( $scripts ) { |
| 86 | if ( isset( $scripts->registered['jquery'] ) ) { |
| 87 | $script = $scripts->registered['jquery']; |
| 88 | if ( $script->deps ) { |
| 89 | $script->deps = array_diff( $script->deps, array( 'jquery-migrate' ) ); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Load script text domain |
| 96 | * |
| 97 | * @return void |
| 98 | */ |
| 99 | public function load_script_text_domain() { |
| 100 | HelperFunctions::load_script_text_domain('kirki-editor' ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Render Editor View from kirki custom page template. |
| 105 | * |
| 106 | * @param string $template template name. |
| 107 | * @return string template location. |
| 108 | */ |
| 109 | public function load_my_editor_view( $template ) { |
| 110 | $template = dirname( __FILE__ ) . '/views/editor.php'; |
| 111 | return $template; |
| 112 | } |
| 113 | |
| 114 | public function enqueue_scripts_for_iframe() { |
| 115 | global $wp_scripts, $wp_styles; |
| 116 | |
| 117 | $scripts = []; |
| 118 | foreach ($wp_scripts->registered as $handle => $script) { |
| 119 | if (!empty($script->src) && strpos($script->src, '/themes/') !== false) { |
| 120 | $scripts[] = $script->src; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | $styles = []; |
| 125 | foreach ($wp_styles->registered as $handle => $style) { |
| 126 | if (!empty($style->src) && strpos($style->src, '/themes/') !== false) { |
| 127 | $styles[] = $style->src; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | |
| 132 | wp_localize_script( |
| 133 | 'kirki-editor', |
| 134 | 'wp_assets', |
| 135 | [ |
| 136 | 'scripts' => $scripts, |
| 137 | 'styles' => $styles, |
| 138 | ] |
| 139 | ); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Load Assets |
| 144 | * |
| 145 | * @return void |
| 146 | */ |
| 147 | public function load_assets() { |
| 148 | HelperFunctions::remove_wp_assets(); |
| 149 | $version = KIRKI_VERSION; |
| 150 | |
| 151 | $screen = HelperFunctions::sanitize_text( isset( $_GET['screen'] ) ? $_GET['screen'] : '' ); |
| 152 | $is_current_screen_dashboard = $screen === 'dashboard' ? true : false; |
| 153 | |
| 154 | wp_enqueue_media(); |
| 155 | |
| 156 | wp_enqueue_script('kirki-editor', apply_filters('kirki_editor_script_url', KIRKI_ASSETS_URL . 'js/kirki-editor.min.js'), array( 'wp-i18n' ), $version, true ); |
| 157 | wp_enqueue_script( 'kirki', KIRKI_ASSETS_URL . 'js/kirki.min.js', array( 'wp-i18n' ), $version, true ); |
| 158 | |
| 159 | $post_id = HelperFunctions::get_post_id_if_possible_from_url(); |
| 160 | |
| 161 | if($is_current_screen_dashboard && !$post_id){ |
| 162 | // get first post id from current post type |
| 163 | $post_id = get_posts( |
| 164 | array( |
| 165 | 'post_type' => 'any', |
| 166 | 'numberposts' => 1, |
| 167 | 'fields' => 'ids', |
| 168 | 'post_status' => 'any', |
| 169 | ) |
| 170 | ); |
| 171 | $post_id = $post_id[0]; |
| 172 | } |
| 173 | |
| 174 | $post = get_post( $post_id ); |
| 175 | if ( ! $post ) { |
| 176 | return; |
| 177 | } |
| 178 | $post_type = $post->post_type; |
| 179 | |
| 180 | $staging_version = isset( $_GET['staging_version'] ) ? intval( HelperFunctions::sanitize_text( $_GET['staging_version'] ) ) : false; |
| 181 | if ( ! $staging_version ) { |
| 182 | $staging_version = Staging::get_most_recent_stage_version( $post_id, false ); |
| 183 | } |
| 184 | |
| 185 | $post_url_arr = HelperFunctions::get_post_url_arr_from_post_id( |
| 186 | $post_id, |
| 187 | array( |
| 188 | 'ajax_url' => true, |
| 189 | 'rest_url' => true, |
| 190 | 'nonce' => true, |
| 191 | 'site_url' => true, |
| 192 | 'admin_url' => true, |
| 193 | 'post_id' => true, |
| 194 | 'core_plugin_url' => true, |
| 195 | 'preview_url' => true, |
| 196 | 'editor_preview_token' => true, |
| 197 | ) |
| 198 | ); |
| 199 | |
| 200 | $staging_nonce = false; |
| 201 | if ( $staging_version ) { |
| 202 | $staging_nonce = wp_create_nonce( 'kirki_preview_staging_nonce' ); |
| 203 | } |
| 204 | |
| 205 | $wp_kirki = array( |
| 206 | 'ajaxUrl' => $post_url_arr['ajax_url'], |
| 207 | 'restUrl' => $post_url_arr['rest_url'], |
| 208 | 'nonce' => $post_url_arr['nonce'], |
| 209 | 'siteUrl' => $post_url_arr['site_url'], |
| 210 | 'adminUrl' => $post_url_arr['admin_url'], |
| 211 | 'postId' => $post_url_arr['post_id'], |
| 212 | 'corePluginUrl' => $post_url_arr['core_plugin_url'], |
| 213 | 'postPreviewUrl' => $post_url_arr['preview_url'], |
| 214 | 'editor_preview_token' => $post_url_arr['editor_preview_token'], |
| 215 | 'uploadBaseUrl' => wp_upload_dir()['baseurl'], |
| 216 | 'postTitle' => get_the_title( $post_id ), |
| 217 | 'postType' => $post_type, |
| 218 | 'kirkiWPDashboard' => $post_url_arr['site_url'] . '/wp-admin', |
| 219 | 'postWpEditUrl' => get_edit_post_link( $post_id ), |
| 220 | 'version' => KIRKI_VERSION, |
| 221 | 'default_menu_id' => $this->get_defalut_menu_id(), // TODO: need to check this menu related code |
| 222 | 'current_user_id' => get_current_user_id(), |
| 223 | 'current_user_avatar_url' => get_avatar_url( get_current_user_id() ), |
| 224 | 'current_user_name' => get_the_author_meta( 'display_name', get_current_user_id() ), |
| 225 | 'staging_version' => $staging_version, |
| 226 | 'staging_nonce' => $staging_nonce, |
| 227 | 'current_screen' => $is_current_screen_dashboard ? 'dashboard' : 'editor', |
| 228 | 'assetsUrlBase' => KIRKI_ASSETS_URL, |
| 229 | ); |
| 230 | wp_localize_script('kirki-editor', 'wp_kirki', $wp_kirki ); |
| 231 | wp_enqueue_style('kirki-kirki', KIRKI_ASSETS_URL . 'css/kirki.min.css', null, $version ); |
| 232 | wp_enqueue_style('kirki-editor', KIRKI_ASSETS_URL . 'css/kirki-editor.min.css', null, $version ); |
| 233 | |
| 234 | // Store the handles of your plugin's scripts and styles |
| 235 | $editor_assets = array( |
| 236 | 'scripts' => array( 'kirki','kirki-editor' ), // here we add kirki.min.js 'kirki' too. cause some theme prevent this script from loading. |
| 237 | 'styles' => array('kirki-kirki','kirki-editor' ), |
| 238 | ); |
| 239 | |
| 240 | if ( defined( 'TDE_APP_PREFIX' ) ) { |
| 241 | // tutor support |
| 242 | $editor_assets['scripts'][] = TDE_APP_PREFIX . '-tutor-kirki-elements'; |
| 243 | $editor_assets['styles'][] = TDE_APP_PREFIX . '-tutor-kirki-elements'; |
| 244 | } |
| 245 | |
| 246 | // Make the handles accessible globally |
| 247 | global $kirki_editor_assets; |
| 248 | $kirki_editor_assets = $editor_assets; |
| 249 | } |
| 250 | |
| 251 | public function add_before_body_tag_end() { |
| 252 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 253 | $s = ''; |
| 254 | $s .= HelperFunctions::get_view_port_lists(); |
| 255 | // $s contains a pre-rendered <script> tag from HelperFunctions::get_view_port_lists(). |
| 256 | echo $s; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 257 | } |
| 258 | private function get_defalut_menu_id() { |
| 259 | $menus = wp_get_nav_menus(); |
| 260 | $first_menu_id = count( $menus ) > 0 ? $menus[0]->term_id : 0; |
| 261 | $nav_locations = get_nav_menu_locations(); |
| 262 | $primary_menu_id = $nav_locations && isset( $nav_locations['primary'] ) ? $nav_locations['primary'] : 0; |
| 263 | $default_menu_id = $primary_menu_id == 0 ? $first_menu_id : $primary_menu_id; |
| 264 | return $default_menu_id; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Check the editor access |
| 269 | * |
| 270 | * @return boolean |
| 271 | */ |
| 272 | public function has_editor_access() { |
| 273 | return HelperFunctions::user_has_editor_access(); |
| 274 | } |
| 275 | } |