Admin
2 weeks ago
Ajax
2 weeks ago
Asset
2 weeks ago
Context
2 weeks ago
Customizer
2 weeks ago
Debug_Bar
2 weeks ago
Dialog
2 weeks ago
Documentation
2 weeks ago
Duplicate
2 weeks ago
Editor
2 weeks ago
Image
2 weeks ago
JSON_LD
2 weeks ago
Languages
2 weeks ago
Log
2 weeks ago
Meta
2 weeks ago
Models
2 weeks ago
PUE
2 weeks ago
Process
2 weeks ago
Promoter
2 weeks ago
REST
2 weeks ago
Repository
2 weeks ago
Service_Providers
2 weeks ago
Shortcode
2 weeks ago
Support
2 weeks ago
Tabbed_View
2 weeks ago
Tooltip
2 weeks ago
Traits
2 weeks ago
Utils
2 weeks ago
Validator
2 weeks ago
Widget
2 weeks ago
Abstract_Deactivation.php
2 weeks ago
Abstract_Plugin_Register.php
2 weeks ago
App_Shop.php
2 weeks ago
Assets.php
2 weeks ago
Assets_Pipeline.php
2 weeks ago
Autoloader.php
2 weeks ago
Cache.php
2 weeks ago
Cache_Listener.php
2 weeks ago
Changelog_Reader.php
2 weeks ago
Container.php
2 weeks ago
Context.php
2 weeks ago
Cost_Utils.php
2 weeks ago
Credits.php
2 weeks ago
Customizer.php
2 weeks ago
DB_Lock.php
2 weeks ago
Data.php
2 weeks ago
Date_Utils.php
2 weeks ago
Db.php
2 weeks ago
Debug.php
2 weeks ago
Dependency.php
2 weeks ago
Deprecation.php
2 weeks ago
Editor.php
2 weeks ago
Error.php
2 weeks ago
Exception.php
2 weeks ago
Extension.php
2 weeks ago
Extension_Loader.php
2 weeks ago
Feature_Detection.php
2 weeks ago
Field.php
2 weeks ago
Field_Conditional.php
2 weeks ago
Freemius.php
2 weeks ago
Log.php
2 weeks ago
Main.php
2 weeks ago
Notices.php
2 weeks ago
Plugin_Meta_Links.php
2 weeks ago
Plugins.php
2 weeks ago
Plugins_API.php
2 weeks ago
Post_History.php
2 weeks ago
Post_Transient.php
2 weeks ago
Promise.php
2 weeks ago
Repository.php
2 weeks ago
Rewrite.php
2 weeks ago
Settings.php
2 weeks ago
Settings_Manager.php
2 weeks ago
Settings_Tab.php
2 weeks ago
Simple_Table.php
2 weeks ago
Support.php
2 weeks ago
Tabbed_View.php
2 weeks ago
Template.php
2 weeks ago
Template_Factory.php
2 weeks ago
Template_Part_Cache.php
2 weeks ago
Templates.php
2 weeks ago
Terms.php
2 weeks ago
Timezones.php
2 weeks ago
Tracker.php
2 weeks ago
Updater.php
2 weeks ago
Validate.php
2 weeks ago
View_Helpers.php
2 weeks ago
Editor.php
259 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Initialize Gutenberg editor blocks |
| 5 | * |
| 6 | * @since 4.8 |
| 7 | */ |
| 8 | class Tribe__Editor { |
| 9 | |
| 10 | /** |
| 11 | * Meta key for flaging if a post is from classic editor |
| 12 | * |
| 13 | * @since 4.8 |
| 14 | * |
| 15 | * @var string |
| 16 | */ |
| 17 | public $key_flag_classic_editor = '_tribe_is_classic_editor'; |
| 18 | |
| 19 | /** |
| 20 | * Utility function to check if we should load the blocks or not based on two assumptions |
| 21 | * |
| 22 | * a) Is gutenberg active? |
| 23 | * b) Is the blocks editor active? |
| 24 | * |
| 25 | * @since 4.8 |
| 26 | * |
| 27 | * @return bool |
| 28 | */ |
| 29 | public function should_load_blocks() { |
| 30 | $gutenberg = $this->is_gutenberg_active() || $this->is_wp_version(); |
| 31 | $blocks = $this->is_blocks_editor_active(); |
| 32 | $classic = $this->is_classic_plugin_active() || $this->is_classic_option_active(); |
| 33 | |
| 34 | $should_load_blocks = $gutenberg && $blocks && ! $classic; |
| 35 | |
| 36 | /** |
| 37 | * Filters whether the Blocks Editor should be activated or not. |
| 38 | * |
| 39 | * @since 4.12.0 |
| 40 | * |
| 41 | * @param bool $should_load_blocks Whether the blocks editor should be activated or not. |
| 42 | */ |
| 43 | $should_load_blocks = (bool) apply_filters( 'tribe_editor_should_load_blocks', $should_load_blocks ); |
| 44 | |
| 45 | return $should_load_blocks; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Checks if we are on version 5.0-alpha or higher where we no longer have |
| 50 | * Gutenberg Project, but the Blocks Editor |
| 51 | * |
| 52 | * @since 4.8 |
| 53 | * |
| 54 | * @return boolean |
| 55 | */ |
| 56 | public function is_wp_version() { |
| 57 | global $wp_version; |
| 58 | |
| 59 | return version_compare( $wp_version, '5.0-alpha', '>=' ); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Checks if we have Gutenberg Project online, only useful while |
| 64 | * its a external plugin |
| 65 | * |
| 66 | * @since 4.8 |
| 67 | * |
| 68 | * @return boolean |
| 69 | */ |
| 70 | public function is_gutenberg_active() { |
| 71 | return function_exists( 'the_gutenberg_project' ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Checks if we have Editor Block active |
| 76 | * |
| 77 | * @since 4.8 |
| 78 | * |
| 79 | * @return boolean |
| 80 | */ |
| 81 | public function is_blocks_editor_active() { |
| 82 | return function_exists( 'register_block_type' ) && function_exists( 'unregister_block_type' ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Adds the required fields into the Events Post Type so that we can use Block Editor |
| 87 | * |
| 88 | * @since 4.8 |
| 89 | * |
| 90 | * @param array $args Arguments used to setup the Post Type |
| 91 | * |
| 92 | * @return array |
| 93 | */ |
| 94 | public function add_support( $args = [] ) { |
| 95 | // Make sure we have the Support argument and it's an array |
| 96 | if ( ! isset( $args['supports'] ) || ! is_array( $args['supports'] ) ) { |
| 97 | $args['supports'] = []; |
| 98 | } |
| 99 | |
| 100 | // Add Editor Support |
| 101 | if ( ! in_array( 'editor', $args['supports'] ) ) { |
| 102 | $args['supports'][] = 'editor'; |
| 103 | } |
| 104 | |
| 105 | return $args; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Adds the required fields into the Post Type so that we can the Rest API to update it |
| 110 | * |
| 111 | * @since 4.8 |
| 112 | * |
| 113 | * @param array $args Arguments used to setup the Post Type |
| 114 | * |
| 115 | * @return array |
| 116 | */ |
| 117 | public function add_rest_support( $args = [] ) { |
| 118 | // Blocks Editor requires REST support |
| 119 | $args['show_in_rest'] = true; |
| 120 | |
| 121 | // Make sure we have the Support argument and it's an array |
| 122 | if ( ! isset( $args['supports'] ) || ! is_array( $args['supports'] ) ) { |
| 123 | $args['supports'] = []; |
| 124 | } |
| 125 | |
| 126 | if ( ! in_array( 'revisions', $args['supports'] ) ) { |
| 127 | $args['supports'][] = 'revisions'; |
| 128 | } |
| 129 | |
| 130 | // Add Custom Fields (meta) Support |
| 131 | if ( ! in_array( 'custom-fields', $args['supports'] ) ) { |
| 132 | $args['supports'][] = 'custom-fields'; |
| 133 | } |
| 134 | |
| 135 | // Add Post Title Support |
| 136 | if ( ! in_array( 'title', $args['supports'] ) ) { |
| 137 | $args['supports'][] = 'title'; |
| 138 | } |
| 139 | |
| 140 | // Add Post Excerpt Support |
| 141 | if ( ! in_array( 'excerpt', $args['supports'] ) ) { |
| 142 | $args['supports'][] = 'excerpt'; |
| 143 | } |
| 144 | |
| 145 | // Add Post Content Support |
| 146 | if ( ! in_array( 'editor', $args['supports'] ) ) { |
| 147 | $args['supports'][] = 'editor'; |
| 148 | } |
| 149 | |
| 150 | // Add Post Author Support |
| 151 | if ( ! in_array( 'author', $args['supports'] ) ) { |
| 152 | $args['supports'][] = 'author'; |
| 153 | } |
| 154 | |
| 155 | // Add Thumbnail Support |
| 156 | if ( ! in_array( 'thumbnail', $args['supports'] ) ) { |
| 157 | $args['supports'][] = 'thumbnail'; |
| 158 | } |
| 159 | |
| 160 | return $args; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * classic_editor_replace is function that is created by the plugin: |
| 165 | * |
| 166 | * @see https://wordpress.org/plugins/classic-editor/ |
| 167 | * |
| 168 | * prior 1.3 version the classic editor plugin was bundle inside of a unique function: |
| 169 | * `classic_editor_replace` now all is bundled inside of a class `Classic_Editor` |
| 170 | * |
| 171 | * @since 4.8 |
| 172 | * |
| 173 | * @return bool |
| 174 | */ |
| 175 | public function is_classic_plugin_active() { |
| 176 | $is_plugin_active = function_exists( 'classic_editor_replace' ) || class_exists( 'Classic_Editor' ); |
| 177 | /** |
| 178 | * Filter to change the output of calling: `is_classic_plugin_active` |
| 179 | * |
| 180 | * @since 4.9.12 |
| 181 | * |
| 182 | * @param $is_plugin_active bool Value that indicates if the plugin is active or not. |
| 183 | */ |
| 184 | return apply_filters( 'tribe_is_classic_editor_plugin_active', $is_plugin_active ); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Check if the setting `'classic-editor-replace'` is set to `replace` that option means to |
| 189 | * replace the gutenberg editor with the classic editor. |
| 190 | * |
| 191 | * Prior to 1.3 on classic editor plugin the value to identify if is on classic the value |
| 192 | * was `replace`, now the value is `classic` |
| 193 | * |
| 194 | * @since 4.8 |
| 195 | * |
| 196 | * @return bool |
| 197 | */ |
| 198 | public function is_classic_option_active() { |
| 199 | $valid_values = [ 'replace', 'classic' ]; |
| 200 | |
| 201 | return in_array( (string) get_option( 'classic-editor-replace' ), $valid_values, true ); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Detect if the classic editor is force-activated via plugin or if it comes from a request. |
| 206 | * |
| 207 | * @since 4.8 |
| 208 | * |
| 209 | * @return bool |
| 210 | */ |
| 211 | public function is_classic_editor() { |
| 212 | $disabled_by_plugin = $this->is_classic_plugin_active() && $this->is_classic_option_active(); |
| 213 | |
| 214 | /** |
| 215 | * Allow other addons to disable classic editor based on options. |
| 216 | * |
| 217 | * @since 4.8.5 |
| 218 | * |
| 219 | * @param bool $classic_is_active Whether the classic editor should be used. |
| 220 | */ |
| 221 | $disabled_by_filter = apply_filters( 'tribe_editor_classic_is_active', false ); |
| 222 | |
| 223 | $is_classic_editor_request = tribe_get_request_var( 'classic-editor', null ); |
| 224 | |
| 225 | return $is_classic_editor_request || $disabled_by_plugin || $disabled_by_filter; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Whether the events are being served using Blocks or the Classical Editor. |
| 230 | * |
| 231 | * @since 4.12.0 |
| 232 | * |
| 233 | * @return bool True if using Blocks. False if using the Classical Editor. |
| 234 | */ |
| 235 | public function is_events_using_blocks() { |
| 236 | /** |
| 237 | * Whether the event is being served through blocks |
| 238 | * or the classical editor. |
| 239 | * |
| 240 | * @since 4.12.0 |
| 241 | * |
| 242 | * @param bool $is_using_blocks True if using blocks. False if using the classical editor. |
| 243 | */ |
| 244 | $is_using_blocks = apply_filters( 'tribe_is_using_blocks', null ); |
| 245 | |
| 246 | // Early bail: The filter was overridden to return either true or false. |
| 247 | if ( null !== $is_using_blocks ) { |
| 248 | return $is_using_blocks; |
| 249 | } |
| 250 | |
| 251 | // Early bail: The site itself is not using blocks. |
| 252 | if ( ! $this->should_load_blocks() ) { |
| 253 | return false; |
| 254 | } |
| 255 | |
| 256 | return tribe_is_truthy( tribe_get_option( 'toggle_blocks_editor' ) ); |
| 257 | } |
| 258 | } |
| 259 |