| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* The file that defines the core plugin class |
| 9 |
* |
| 10 |
* A class definition that includes attributes and functions used across both the |
| 11 |
* public-facing side of the site and the admin area. |
| 12 |
* |
| 13 |
* @link https://pixelgrade.com |
| 14 |
* @since 1.0.0 |
| 15 |
* |
| 16 |
* @package Gridable |
| 17 |
* @subpackage Gridable/includes |
| 18 |
*/ |
| 19 |
|
| 20 |
/** |
| 21 |
* The core plugin class. |
| 22 |
* |
| 23 |
* This is used to define internationalization, admin-specific hooks, and |
| 24 |
* public-facing site hooks. |
| 25 |
* |
| 26 |
* Also maintains the unique identifier of this plugin as well as the current |
| 27 |
* version of the plugin. |
| 28 |
* |
| 29 |
* @since 1.0.0 |
| 30 |
* @package Gridable |
| 31 |
* @subpackage Gridable/includes |
| 32 |
* @author Pixelgrade <contact@pixelgrade.com> |
| 33 |
*/ |
| 34 |
class Gridable { |
| 35 |
|
| 36 |
/** |
| 37 |
* The loader that's responsible for maintaining and registering all hooks that power |
| 38 |
* the plugin. |
| 39 |
* |
| 40 |
* @since 1.0.0 |
| 41 |
* @access protected |
| 42 |
* @var Gridable_Loader $loader Maintains and registers all hooks for the plugin. |
| 43 |
*/ |
| 44 |
protected $loader; |
| 45 |
|
| 46 |
/** |
| 47 |
* The unique identifier of this plugin. |
| 48 |
* |
| 49 |
* @since 1.0.0 |
| 50 |
* @access protected |
| 51 |
* @var string $gridable The string used to uniquely identify this plugin. |
| 52 |
*/ |
| 53 |
protected $gridable; |
| 54 |
|
| 55 |
/** |
| 56 |
* The current version of the plugin. |
| 57 |
* |
| 58 |
* @since 1.0.0 |
| 59 |
* @access protected |
| 60 |
* @var string $version The current version of the plugin. |
| 61 |
*/ |
| 62 |
protected $version; |
| 63 |
|
| 64 |
/** |
| 65 |
* Define the core functionality of the plugin. |
| 66 |
* |
| 67 |
* Set the plugin name and the plugin version that can be used throughout the plugin. |
| 68 |
* Load the dependencies, define the locale, and set the hooks for the admin area and |
| 69 |
* the public-facing side of the site. |
| 70 |
* |
| 71 |
* @param string $version The current plugin version. |
| 72 |
* |
| 73 |
* @since 1.0.0 |
| 74 |
*/ |
| 75 |
public function __construct( $version = '1.0.0' ) { |
| 76 |
|
| 77 |
$this->gridable = 'gridable'; |
| 78 |
$this->version = $version; |
| 79 |
|
| 80 |
$this->load_dependencies(); |
| 81 |
$this->set_locale(); |
| 82 |
|
| 83 |
// We need to define the admin hooks (and their conditions) later to allow the theme to have its say (like in the case of the Classic Editor bundled with Pixelgrade Care). |
| 84 |
add_action( 'after_setup_theme', array( $this, 'define_admin_hooks' ), 3 ); |
| 85 |
$this->define_public_hooks(); |
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Load the required dependencies for this plugin. |
| 91 |
* |
| 92 |
* Include the following files that make up the plugin: |
| 93 |
* |
| 94 |
* - Gridable_Loader. Orchestrates the hooks of the plugin. |
| 95 |
* - Gridable_i18n. Defines internationalization functionality. |
| 96 |
* - Gridable_Admin. Defines all hooks for the admin area. |
| 97 |
* - Gridable_Public. Defines all hooks for the public side of the site. |
| 98 |
* |
| 99 |
* Create an instance of the loader which will be used to register the hooks |
| 100 |
* with WordPress. |
| 101 |
* |
| 102 |
* @since 1.0.0 |
| 103 |
* @access private |
| 104 |
*/ |
| 105 |
private function load_dependencies() { |
| 106 |
|
| 107 |
/** |
| 108 |
* The class responsible for orchestrating the actions and filters of the |
| 109 |
* core plugin. |
| 110 |
*/ |
| 111 |
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-gridable-loader.php'; |
| 112 |
|
| 113 |
/** |
| 114 |
* The class responsible for defining internationalization functionality |
| 115 |
* of the plugin. |
| 116 |
*/ |
| 117 |
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-gridable-i18n.php'; |
| 118 |
|
| 119 |
/** |
| 120 |
* The class responsible for defining all actions that occur in the admin area. |
| 121 |
*/ |
| 122 |
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-gridable-admin.php'; |
| 123 |
|
| 124 |
/** |
| 125 |
* The class responsible for defining all actions that occur in the public-facing |
| 126 |
* side of the site. |
| 127 |
*/ |
| 128 |
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/class-gridable-public.php'; |
| 129 |
|
| 130 |
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/addons/attributes.php'; |
| 131 |
|
| 132 |
$this->loader = new Gridable_Loader(); |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Check if Gutenberg is active. |
| 138 |
* Must be used not earlier than plugins_loaded action fired. |
| 139 |
* |
| 140 |
* @return bool |
| 141 |
*/ |
| 142 |
protected function is_gutenberg_active() { |
| 143 |
// Gutenberg plugin is installed and activated. |
| 144 |
$gutenberg = ! ( false === has_filter( 'replace_editor', 'gutenberg_init' ) ); |
| 145 |
|
| 146 |
// Block editor since 5.0. |
| 147 |
$block_editor = version_compare( $GLOBALS['wp_version'], '5.0-beta', '>' ); |
| 148 |
|
| 149 |
if ( ! $gutenberg && ! $block_editor ) { |
| 150 |
return false; |
| 151 |
} |
| 152 |
|
| 153 |
if ( $this->is_classic_editor_plugin_active() ) { |
| 154 |
$editor_option = get_option( 'classic-editor-replace' ); |
| 155 |
$block_editor_active = array( 'no-replace', 'block' ); |
| 156 |
|
| 157 |
return in_array( $editor_option, $block_editor_active, true ); |
| 158 |
} |
| 159 |
|
| 160 |
return true; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Check if Classic Editor plugin is active. |
| 165 |
* |
| 166 |
* @return bool |
| 167 |
*/ |
| 168 |
protected function is_classic_editor_plugin_active() { |
| 169 |
if ( class_exists( 'Classic_Editor' ) && method_exists( 'Classic_Editor', 'init_actions' ) ) { |
| 170 |
return true; |
| 171 |
} |
| 172 |
|
| 173 |
if ( ! function_exists( 'is_plugin_active' ) ) { |
| 174 |
include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 175 |
} |
| 176 |
|
| 177 |
if ( is_plugin_active( 'classic-editor/classic-editor.php' ) ) { |
| 178 |
return true; |
| 179 |
} |
| 180 |
|
| 181 |
return false; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Define the locale for this plugin for internationalization. |
| 186 |
* |
| 187 |
* Uses the Gridable_i18n class in order to set the domain and to register the hook |
| 188 |
* with WordPress. |
| 189 |
* |
| 190 |
* @since 1.0.0 |
| 191 |
* @access private |
| 192 |
*/ |
| 193 |
protected function set_locale() { |
| 194 |
|
| 195 |
$plugin_i18n = new Gridable_i18n(); |
| 196 |
|
| 197 |
$this->loader->add_action( 'plugins_loaded', $plugin_i18n, 'load_plugin_textdomain' ); |
| 198 |
|
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Register all of the hooks related to the admin area functionality |
| 203 |
* of the plugin. |
| 204 |
* |
| 205 |
* @since 1.0.0 |
| 206 |
* @access private |
| 207 |
*/ |
| 208 |
public function define_admin_hooks() { |
| 209 |
|
| 210 |
$plugin_admin = new Gridable_Admin( $this->get_gridable(), $this->get_version() ); |
| 211 |
|
| 212 |
add_action( 'media_buttons', array( $plugin_admin, 'add_media_button' ), 15 ); |
| 213 |
|
| 214 |
// Gridable's editing UI is intentionally limited to Classic Editor/TinyMCE. |
| 215 |
// Register the TinyMCE plugin whenever admin TinyMCE initializes so Classic Editor |
| 216 |
// per-post switching works even when the global default editor is the block editor. |
| 217 |
if ( is_admin() ) { |
| 218 |
add_filter( 'mce_external_plugins', array( $plugin_admin, 'add_tinymce_plugin' ) ); |
| 219 |
} |
| 220 |
|
| 221 |
add_filter( 'wp_editor_settings', array( $plugin_admin, 'change_tinymce_settings' ) ); |
| 222 |
add_action( 'admin_footer', array( $plugin_admin, 'print_tinymce_templates' ) ); |
| 223 |
|
| 224 |
// also inside the wp-editor we cannot localize parameters, so we simply output the javascript code |
| 225 |
add_action( 'admin_head', array( $plugin_admin, 'styles_scripts' ) ); |
| 226 |
|
| 227 |
if ( apply_filters( 'gridable_support_for_customizer', true ) && ! wp_doing_ajax() ) { |
| 228 |
// This is needed for wp-editors added in customizer |
| 229 |
add_action( 'customize_controls_print_footer_scripts', array( $plugin_admin, 'styles_scripts' ) ); |
| 230 |
add_action( 'customize_controls_print_footer_scripts', array( $plugin_admin, 'print_tinymce_templates' ) ); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Register all of the hooks related to the public-facing functionality |
| 236 |
* of the plugin. |
| 237 |
* |
| 238 |
* @since 1.0.0 |
| 239 |
* @access private |
| 240 |
*/ |
| 241 |
protected function define_public_hooks() { |
| 242 |
|
| 243 |
$plugin_public = new Gridable_Public( $this->get_gridable(), $this->get_version() ); |
| 244 |
|
| 245 |
add_action( 'wp_enqueue_scripts', array( $plugin_public, 'enqueue_styles' ) ); |
| 246 |
add_action( 'wp_enqueue_scripts', array( $plugin_public, 'enqueue_scripts' ) ); |
| 247 |
add_shortcode( 'row', array( $plugin_public, 'add_row_shortcode' ) ); |
| 248 |
add_shortcode( 'col', array( $plugin_public, 'add_column_shortcode' ) ); |
| 249 |
|
| 250 |
add_filter( 'the_content', array( $plugin_public, 'parse_content_for_nested_rows' ), 9 ); |
| 251 |
|
| 252 |
// clear lost p tags in front-end |
| 253 |
if ( ! is_admin() ) { |
| 254 |
add_filter( 'gridable_the_column_content', array( $plugin_public, 'gridable_fix_lost_p_tags' ), 10, 2 ); |
| 255 |
|
| 256 |
if ( true === apply_filters( 'gridable_add_empty_column_class', true ) ) { |
| 257 |
add_filter( 'gridable_column_class', array( |
| 258 |
$plugin_public, |
| 259 |
'gridable_add_empty_column_class', |
| 260 |
), 10, 4 ); |
| 261 |
} |
| 262 |
|
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Run the loader to execute all of the hooks with WordPress. |
| 268 |
* |
| 269 |
* @since 1.0.0 |
| 270 |
*/ |
| 271 |
public function run() { |
| 272 |
$this->loader->run(); |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* The name of the plugin used to uniquely identify it within the context of |
| 277 |
* WordPress and to define internationalization functionality. |
| 278 |
* |
| 279 |
* @return string The name of the plugin. |
| 280 |
* @since 1.0.0 |
| 281 |
*/ |
| 282 |
public function get_gridable() { |
| 283 |
return $this->gridable; |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* The reference to the class that orchestrates the hooks with the plugin. |
| 288 |
* |
| 289 |
* @return Gridable_Loader Orchestrates the hooks of the plugin. |
| 290 |
* @since 1.0.0 |
| 291 |
*/ |
| 292 |
public function get_loader() { |
| 293 |
return $this->loader; |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Retrieve the version number of the plugin. |
| 298 |
* |
| 299 |
* @return string The version number of the plugin. |
| 300 |
* @since 1.0.0 |
| 301 |
*/ |
| 302 |
public function get_version() { |
| 303 |
return $this->version; |
| 304 |
} |
| 305 |
|
| 306 |
} |
| 307 |
|