PluginActiveEvents.php
3 weeks ago
PluginDeactivateEvents.php
3 weeks ago
PluginInitEvents.php
1 month ago
PluginLoadedEvents.php
3 weeks ago
PluginShortcode.php
2 months ago
TemplateRedirection.php
1 month ago
PluginInitEvents.php
196 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Plugin init events handler |
| 4 | * |
| 5 | * @package kirki |
| 6 | */ |
| 7 | |
| 8 | namespace Kirki\Manager; |
| 9 | |
| 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 11 | exit; // Exit if accessed directly. |
| 12 | } |
| 13 | |
| 14 | use Kirki\Ajax\Media; |
| 15 | use Kirki\Ajax\Page; |
| 16 | use Kirki\Ajax\WpAdmin; |
| 17 | use Kirki\Frontend\Preview\Preview; |
| 18 | use Kirki\HelperFunctions; |
| 19 | |
| 20 | /** |
| 21 | * Do some task during plugin activation |
| 22 | */ |
| 23 | class PluginInitEvents { |
| 24 | |
| 25 | |
| 26 | /** |
| 27 | * Initilize the class |
| 28 | * |
| 29 | * @return void |
| 30 | */ |
| 31 | public function __construct() { |
| 32 | add_filter( 'document_title_parts', array( $this, 'change_document_title' ) ); |
| 33 | add_action( 'init', array( $this, 'plugins_initial_tasks' ) ); |
| 34 | add_filter( 'upload_mimes', array( $this, 'cc_mime_types' ) ); |
| 35 | add_filter( 'wp_check_filetype_and_ext', array( $this, 'fix_filetype_check' ), 10, 5 ); |
| 36 | add_filter( 'big_image_size_threshold', '__return_false' ); |
| 37 | add_theme_support( 'post-thumbnails' ); |
| 38 | add_filter( 'rewrite_rules_array', array( $this, 'kirki_utility_pages_rewrite_rules' ) ); |
| 39 | |
| 40 | add_filter( 'wp_handle_upload_prefilter', array( new Media(), 'kirki_handle_upload_prefilter' ) ); |
| 41 | add_filter( 'wp_generate_attachment_metadata', array( new Media(), 'kirki_convert_sizes_to_webp' ) ); |
| 42 | add_filter( 'kirki_html_generator', array( $this, 'kirki_html_generator_wrap' ), 10, 2 ); |
| 43 | add_filter( 'kirki_template_finder', array( new HelperFunctions(), 'find_template_for_this_context' ), 10, 2 ); |
| 44 | } |
| 45 | |
| 46 | |
| 47 | public function kirki_html_generator_wrap( $s, $post_id ) { |
| 48 | $staging_version = isset( $_GET['staging_version'] ) ? intval( HelperFunctions::sanitize_text( $_GET['staging_version'] ) ) : false; |
| 49 | $nonce = HelperFunctions::sanitize_text( isset( $_GET['nonce'] ) ? $_GET['nonce'] : 'false' ); |
| 50 | $preview_staging_version = ( $staging_version && wp_verify_nonce( $nonce, 'kirki_preview_staging_nonce' ) ) ? $staging_version : false; |
| 51 | return HelperFunctions::kirki_html_generator( $s, $post_id, $preview_staging_version ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Filters the document title parts based on custom SEO settings or context-specific templates. |
| 56 | * |
| 57 | * @param array $title_parts_array The original title parts array. |
| 58 | * @return array Modified title parts array. |
| 59 | */ |
| 60 | public function change_document_title( $title_parts_array ) { |
| 61 | $post_id = HelperFunctions::get_post_id_if_possible_from_url(); |
| 62 | |
| 63 | $context = HelperFunctions::get_current_page_context(); |
| 64 | if ( $context && isset( $context['type'] ) ) { |
| 65 | switch ( $context['type'] ) { |
| 66 | case '404': { |
| 67 | $template = HelperFunctions::find_utility_page_for_this_context( '404', 'type' ); |
| 68 | if ( $template && isset( $template['id'] ) ) { |
| 69 | $post_id = $template['id']; |
| 70 | } |
| 71 | break; |
| 72 | } |
| 73 | case 'kirki_utility': { |
| 74 | $post_id = $context['kirki_utility_page_id']; |
| 75 | break; |
| 76 | } |
| 77 | default: { |
| 78 | break; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | if ( $post_id ) { |
| 84 | |
| 85 | $template_data = HelperFunctions::get_template_data_if_current_page_is_kirki_template(); |
| 86 | if ( $template_data ) { |
| 87 | $seo_settings = get_post_meta( $template_data['template_id'], KIRKI_PAGE_SEO_SETTINGS_META_KEY, true ); |
| 88 | } else { |
| 89 | $seo_settings = get_post_meta( $post_id, KIRKI_PAGE_SEO_SETTINGS_META_KEY, true ); |
| 90 | } |
| 91 | |
| 92 | if ( $seo_settings && isset( $seo_settings['seoSettings'], $seo_settings['seoSettings']['seoTitleTag'], $seo_settings['seoSettings']['seoTitleTag']['value'] ) ) { |
| 93 | $seo_title = Preview::getSeoValue( $post_id, $seo_settings['seoSettings']['seoTitleTag']['value'] ); |
| 94 | $title_parts_array['title'] = $seo_title; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return $title_parts_array; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Register custom post type , mime types and theme page templates |
| 103 | * |
| 104 | * @return void |
| 105 | */ |
| 106 | public function plugins_initial_tasks() { |
| 107 | // register_post_type( 'kirki_page', array( 'public' => true ) ); |
| 108 | add_filter( 'theme_page_templates', array( $this, 'add_page_template_to_dropdown' ) ); |
| 109 | load_plugin_textdomain( 'kirki', false, KIRKI_PLUGIN_REL_URL . '/languages' ); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Filter hook upload_mimes. |
| 114 | * |
| 115 | * @param array $mimes wp mimes. |
| 116 | * @return array |
| 117 | */ |
| 118 | public function cc_mime_types( $mimes ) { |
| 119 | $data = WpAdmin::get_common_data( true ); |
| 120 | if ( isset( $data['json_upload'] ) && $data['json_upload'] === true ) { |
| 121 | $mimes['json'] = 'application/json'; |
| 122 | } |
| 123 | if ( isset( $data['svg_upload'] ) && $data['svg_upload'] === true ) { |
| 124 | $mimes['svg'] = 'image/svg+xml'; |
| 125 | } |
| 126 | |
| 127 | return $mimes; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Fix JSON and SVG file type check for WordPress MIME sniffing. |
| 132 | * |
| 133 | * WordPress uses finfo to sniff the real MIME type of uploaded files. |
| 134 | * JSON files are detected as 'text/plain' by finfo, which causes WordPress |
| 135 | * to reject them even when 'application/json' is in the allowed mimes list. |
| 136 | * This filter overrides the sniffed type for .json files when json_upload is enabled. |
| 137 | * |
| 138 | * @param array $wp_check_filetype_and_ext Array with ext, type, proper_filename keys. |
| 139 | * @param string $file Full path to the file. |
| 140 | * @param string $filename The name of the file. |
| 141 | * @param string[] $mimes Array of mime types keyed by extension. |
| 142 | * @param string|false $real_mime The actual mime type or false if finfo is unavailable. |
| 143 | * @return array |
| 144 | */ |
| 145 | public function fix_filetype_check( $wp_check_filetype_and_ext, $file, $filename, $mimes, $real_mime ) { |
| 146 | $data = WpAdmin::get_common_data( true ); |
| 147 | $ext = strtolower( pathinfo( $filename, PATHINFO_EXTENSION ) ); |
| 148 | |
| 149 | if ( 'json' === $ext && isset( $data['json_upload'] ) && $data['json_upload'] === true ) { |
| 150 | $wp_check_filetype_and_ext['ext'] = 'json'; |
| 151 | $wp_check_filetype_and_ext['type'] = 'application/json'; |
| 152 | } |
| 153 | |
| 154 | if ( 'svg' === $ext && isset( $data['svg_upload'] ) && $data['svg_upload'] === true ) { |
| 155 | $wp_check_filetype_and_ext['ext'] = 'svg'; |
| 156 | $wp_check_filetype_and_ext['type'] = 'image/svg+xml'; |
| 157 | } |
| 158 | |
| 159 | return $wp_check_filetype_and_ext; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Add rewrite rules for Kirki utility pages. |
| 164 | * |
| 165 | * @param array $rules The list of rewrite rules. |
| 166 | * |
| 167 | * @return array $rules The modified list of rewrite rules. |
| 168 | */ |
| 169 | public function kirki_utility_pages_rewrite_rules( $rules ) { |
| 170 | $utility_pages = Page::fetch_list('kirki_utility', true, array( 'publish' ) ); |
| 171 | $new_rules = array(); |
| 172 | foreach ( $utility_pages as $key => $page ) { |
| 173 | $slug = ! empty( $page['slug'] ) ? preg_quote( $page['slug'], '/' ) : ''; |
| 174 | if ( empty( $slug ) ) { |
| 175 | continue; |
| 176 | } |
| 177 | if ( $page['utility_page_type'] !== '404' ) { |
| 178 | $new_rules[ "^$slug$" ] = "index.php?kirki_utility_page_type={$page['utility_page_type']}&kirki_utility_page_id={$page['id']}"; |
| 179 | } |
| 180 | } |
| 181 | return $new_rules + ( is_array( $rules ) ? $rules : array() ); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Add page templates. |
| 186 | * |
| 187 | * @param array $templates The list of page templates. |
| 188 | * |
| 189 | * @return array $templates The modified list of page templates. |
| 190 | */ |
| 191 | public function add_page_template_to_dropdown( $templates ) { |
| 192 | $templates[ HelperFunctions::get_kirki_full_canvas_template_path() ] = 'Kirki Full Canvas'; |
| 193 | return $templates; |
| 194 | } |
| 195 | } |
| 196 |