@@ -16,562 +16,562 @@ |
||
| 16 | 16 | abstract class EEM_CPT_Base extends EEM_Soft_Delete_Base |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - const EVENT_CATEGORY_TAXONOMY = 'espresso_event_categories'; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * @var string post_status_publish - the wp post status for published cpts |
|
| 23 | - */ |
|
| 24 | - const post_status_publish = 'publish'; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * @var string post_status_future - the wp post status for scheduled cpts |
|
| 28 | - */ |
|
| 29 | - const post_status_future = 'future'; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @var string post_status_draft - the wp post status for draft cpts |
|
| 33 | - */ |
|
| 34 | - const post_status_draft = 'draft'; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @var string post_status_pending - the wp post status for pending cpts |
|
| 38 | - */ |
|
| 39 | - const post_status_pending = 'pending'; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @var string post_status_private - the wp post status for private cpts |
|
| 43 | - */ |
|
| 44 | - const post_status_private = 'private'; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @var string post_status_trashed - the wp post status for trashed cpts |
|
| 48 | - */ |
|
| 49 | - const post_status_trashed = 'trash'; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * This is an array of custom statuses for the given CPT model (modified by children) |
|
| 53 | - * format: |
|
| 54 | - * array( |
|
| 55 | - * 'status_name' => array( |
|
| 56 | - * 'label' => __('Status Name', 'event_espresso'), |
|
| 57 | - * 'public' => TRUE //whether a public status or not. |
|
| 58 | - * ) |
|
| 59 | - * ) |
|
| 60 | - * |
|
| 61 | - * @var array |
|
| 62 | - */ |
|
| 63 | - protected $_custom_stati = array(); |
|
| 64 | - |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Adds a relationship to Term_Taxonomy for each CPT_Base |
|
| 68 | - * |
|
| 69 | - * @param string $timezone |
|
| 70 | - * @throws \EE_Error |
|
| 71 | - */ |
|
| 72 | - protected function __construct($timezone = null) |
|
| 73 | - { |
|
| 74 | - // adds a relationship to Term_Taxonomy for all these models. For this to work |
|
| 75 | - // Term_Relationship must have a relation to each model subclassing EE_CPT_Base explicitly |
|
| 76 | - // eg, in EEM_Term_Relationship, inside the _model_relations array, there must be an entry |
|
| 77 | - // with key equalling the subclassing model's model name (eg 'Event' or 'Venue'), and the value |
|
| 78 | - // must also be new EE_HABTM_Relation('Term_Relationship'); |
|
| 79 | - $this->_model_relations['Term_Taxonomy'] = new EE_HABTM_Relation('Term_Relationship'); |
|
| 80 | - $primary_table_name = null; |
|
| 81 | - // add the common _status field to all CPT primary tables. |
|
| 82 | - foreach ($this->_tables as $alias => $table_obj) { |
|
| 83 | - if ($table_obj instanceof EE_Primary_Table) { |
|
| 84 | - $primary_table_name = $alias; |
|
| 85 | - } |
|
| 86 | - } |
|
| 87 | - // set default wp post statuses if child has not already set. |
|
| 88 | - if (! isset($this->_fields[ $primary_table_name ]['status'])) { |
|
| 89 | - $this->_fields[ $primary_table_name ]['status'] = new EE_WP_Post_Status_Field( |
|
| 90 | - 'post_status', |
|
| 91 | - __("Event Status", "event_espresso"), |
|
| 92 | - false, |
|
| 93 | - 'draft' |
|
| 94 | - ); |
|
| 95 | - } |
|
| 96 | - if (! isset($this->_fields[ $primary_table_name ]['to_ping'])) { |
|
| 97 | - $this->_fields[ $primary_table_name ]['to_ping'] = new EE_DB_Only_Text_Field( |
|
| 98 | - 'to_ping', |
|
| 99 | - __('To Ping', 'event_espresso'), |
|
| 100 | - false, |
|
| 101 | - '' |
|
| 102 | - ); |
|
| 103 | - } |
|
| 104 | - if (! isset($this->_fields[ $primary_table_name ]['pinged'])) { |
|
| 105 | - $this->_fields[ $primary_table_name ]['pinged'] = new EE_DB_Only_Text_Field( |
|
| 106 | - 'pinged', |
|
| 107 | - __('Pinged', 'event_espresso'), |
|
| 108 | - false, |
|
| 109 | - '' |
|
| 110 | - ); |
|
| 111 | - } |
|
| 112 | - if (! isset($this->_fields[ $primary_table_name ]['comment_status'])) { |
|
| 113 | - $this->_fields[ $primary_table_name ]['comment_status'] = new EE_Plain_Text_Field( |
|
| 114 | - 'comment_status', |
|
| 115 | - __('Comment Status', 'event_espresso'), |
|
| 116 | - false, |
|
| 117 | - 'open' |
|
| 118 | - ); |
|
| 119 | - } |
|
| 120 | - if (! isset($this->_fields[ $primary_table_name ]['ping_status'])) { |
|
| 121 | - $this->_fields[ $primary_table_name ]['ping_status'] = new EE_Plain_Text_Field( |
|
| 122 | - 'ping_status', |
|
| 123 | - __('Ping Status', 'event_espresso'), |
|
| 124 | - false, |
|
| 125 | - 'open' |
|
| 126 | - ); |
|
| 127 | - } |
|
| 128 | - if (! isset($this->_fields[ $primary_table_name ]['post_content_filtered'])) { |
|
| 129 | - $this->_fields[ $primary_table_name ]['post_content_filtered'] = new EE_DB_Only_Text_Field( |
|
| 130 | - 'post_content_filtered', |
|
| 131 | - __('Post Content Filtered', 'event_espresso'), |
|
| 132 | - false, |
|
| 133 | - '' |
|
| 134 | - ); |
|
| 135 | - } |
|
| 136 | - if (! isset($this->_model_relations['Post_Meta'])) { |
|
| 137 | - // don't block deletes though because we want to maintain the current behaviour |
|
| 138 | - $this->_model_relations['Post_Meta'] = new EE_Has_Many_Relation(false); |
|
| 139 | - } |
|
| 140 | - if (! $this->_minimum_where_conditions_strategy instanceof EE_Default_Where_Conditions) { |
|
| 141 | - // nothing was set during child constructor, so set default |
|
| 142 | - $this->_minimum_where_conditions_strategy = new EE_CPT_Minimum_Where_Conditions($this->post_type()); |
|
| 143 | - } |
|
| 144 | - if (! $this->_default_where_conditions_strategy instanceof EE_Default_Where_Conditions) { |
|
| 145 | - // nothing was set during child constructor, so set default |
|
| 146 | - // it's ok for child classes to specify this, but generally this is more DRY |
|
| 147 | - $this->_default_where_conditions_strategy = new EE_CPT_Where_Conditions($this->post_type()); |
|
| 148 | - } |
|
| 149 | - parent::__construct($timezone); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * @return array |
|
| 155 | - */ |
|
| 156 | - public function public_event_stati() |
|
| 157 | - { |
|
| 158 | - // @see wp-includes/post.php |
|
| 159 | - return get_post_stati(array('public' => true)); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Searches for field on this model of type 'deleted_flag'. if it is found, |
|
| 165 | - * returns it's name. BUT That doesn't apply to CPTs. We should instead use post_status_field_name |
|
| 166 | - * |
|
| 167 | - * @return string |
|
| 168 | - * @throws EE_Error |
|
| 169 | - */ |
|
| 170 | - public function deleted_field_name() |
|
| 171 | - { |
|
| 172 | - throw new EE_Error( |
|
| 173 | - sprintf( |
|
| 174 | - __( |
|
| 175 | - "EEM_CPT_Base should nto call deleted_field_name! It should instead use post_status_field_name", |
|
| 176 | - "event_espresso" |
|
| 177 | - ) |
|
| 178 | - ) |
|
| 179 | - ); |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * Gets the field's name that sets the post status |
|
| 185 | - * |
|
| 186 | - * @return string |
|
| 187 | - * @throws EE_Error |
|
| 188 | - */ |
|
| 189 | - public function post_status_field_name() |
|
| 190 | - { |
|
| 191 | - $field = $this->get_a_field_of_type('EE_WP_Post_Status_Field'); |
|
| 192 | - if ($field) { |
|
| 193 | - return $field->get_name(); |
|
| 194 | - } else { |
|
| 195 | - throw new EE_Error( |
|
| 196 | - sprintf( |
|
| 197 | - __( |
|
| 198 | - 'We are trying to find the post status flag field on %s, but none was found. Are you sure there is a field of type EE_Trashed_Flag_Field in %s constructor?', |
|
| 199 | - 'event_espresso' |
|
| 200 | - ), |
|
| 201 | - get_class($this), |
|
| 202 | - get_class($this) |
|
| 203 | - ) |
|
| 204 | - ); |
|
| 205 | - } |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * Alters the query params so that only trashed/soft-deleted items are considered |
|
| 211 | - * |
|
| 212 | - * @param array $query_params like EEM_Base::get_all's $query_params |
|
| 213 | - * @return array like EEM_Base::get_all's $query_params |
|
| 214 | - */ |
|
| 215 | - protected function _alter_query_params_so_only_trashed_items_included($query_params) |
|
| 216 | - { |
|
| 217 | - $post_status_field_name = $this->post_status_field_name(); |
|
| 218 | - $query_params[0][ $post_status_field_name ] = self::post_status_trashed; |
|
| 219 | - return $query_params; |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * Alters the query params so each item's deleted status is ignored. |
|
| 225 | - * |
|
| 226 | - * @param array $query_params |
|
| 227 | - * @return array |
|
| 228 | - */ |
|
| 229 | - protected function _alter_query_params_so_deleted_and_undeleted_items_included($query_params) |
|
| 230 | - { |
|
| 231 | - $query_params['default_where_conditions'] = 'minimum'; |
|
| 232 | - return $query_params; |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - |
|
| 236 | - /** |
|
| 237 | - * Performs deletes or restores on items. Both soft-deleted and non-soft-deleted items considered. |
|
| 238 | - * |
|
| 239 | - * @param boolean $delete true to indicate deletion, false to indicate restoration |
|
| 240 | - * @param array $query_params like EEM_Base::get_all |
|
| 241 | - * @return boolean success |
|
| 242 | - */ |
|
| 243 | - public function delete_or_restore($delete = true, $query_params = array()) |
|
| 244 | - { |
|
| 245 | - $post_status_field_name = $this->post_status_field_name(); |
|
| 246 | - $query_params = $this->_alter_query_params_so_deleted_and_undeleted_items_included($query_params); |
|
| 247 | - $new_status = $delete ? self::post_status_trashed : 'draft'; |
|
| 248 | - if ($this->update(array($post_status_field_name => $new_status), $query_params)) { |
|
| 249 | - return true; |
|
| 250 | - } else { |
|
| 251 | - return false; |
|
| 252 | - } |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - |
|
| 256 | - /** |
|
| 257 | - * meta_table |
|
| 258 | - * returns first EE_Secondary_Table table name |
|
| 259 | - * |
|
| 260 | - * @access public |
|
| 261 | - * @return string |
|
| 262 | - */ |
|
| 263 | - public function meta_table() |
|
| 264 | - { |
|
| 265 | - $meta_table = $this->_get_other_tables(); |
|
| 266 | - $meta_table = reset($meta_table); |
|
| 267 | - return $meta_table instanceof EE_Secondary_Table ? $meta_table->get_table_name() : null; |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - |
|
| 271 | - /** |
|
| 272 | - * This simply returns an array of the meta table fields (useful for when we just need to update those fields) |
|
| 273 | - * |
|
| 274 | - * @param bool $all triggers whether we include DB_Only fields or JUST non DB_Only fields. Defaults to false (no |
|
| 275 | - * db only fields) |
|
| 276 | - * @return array |
|
| 277 | - */ |
|
| 278 | - public function get_meta_table_fields($all = false) |
|
| 279 | - { |
|
| 280 | - $all_fields = $fields_to_return = array(); |
|
| 281 | - foreach ($this->_tables as $alias => $table_obj) { |
|
| 282 | - if ($table_obj instanceof EE_Secondary_Table) { |
|
| 283 | - $all_fields = array_merge($this->_get_fields_for_table($alias), $all_fields); |
|
| 284 | - } |
|
| 285 | - } |
|
| 286 | - if (! $all) { |
|
| 287 | - foreach ($all_fields as $name => $obj) { |
|
| 288 | - if ($obj instanceof EE_DB_Only_Field_Base) { |
|
| 289 | - continue; |
|
| 290 | - } |
|
| 291 | - $fields_to_return[] = $name; |
|
| 292 | - } |
|
| 293 | - } else { |
|
| 294 | - $fields_to_return = array_keys($all_fields); |
|
| 295 | - } |
|
| 296 | - return $fields_to_return; |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - |
|
| 300 | - /** |
|
| 301 | - * Adds an event category with the specified name and description to the specified |
|
| 302 | - * $cpt_model_object. Intelligently adds a term if necessary, and adds a term_taxonomy if necessary, |
|
| 303 | - * and adds an entry in the term_relationship if necessary. |
|
| 304 | - * |
|
| 305 | - * @param EE_CPT_Base $cpt_model_object |
|
| 306 | - * @param string $category_name (used to derive the term slug too) |
|
| 307 | - * @param string $category_description |
|
| 308 | - * @param int $parent_term_taxonomy_id |
|
| 309 | - * @return EE_Term_Taxonomy |
|
| 310 | - */ |
|
| 311 | - public function add_event_category( |
|
| 312 | - EE_CPT_Base $cpt_model_object, |
|
| 313 | - $category_name, |
|
| 314 | - $category_description = '', |
|
| 315 | - $parent_term_taxonomy_id = null |
|
| 316 | - ) { |
|
| 317 | - // create term |
|
| 318 | - require_once(EE_MODELS . 'EEM_Term.model.php'); |
|
| 319 | - // first, check for a term by the same name or slug |
|
| 320 | - $category_slug = sanitize_title($category_name); |
|
| 321 | - $term = EEM_Term::instance()->get_one( |
|
| 322 | - array( |
|
| 323 | - array( |
|
| 324 | - 'OR' => array( |
|
| 325 | - 'name' => $category_name, |
|
| 326 | - 'slug' => $category_slug, |
|
| 327 | - ), |
|
| 328 | - 'Term_Taxonomy.taxonomy' => self::EVENT_CATEGORY_TAXONOMY |
|
| 329 | - ), |
|
| 330 | - ) |
|
| 331 | - ); |
|
| 332 | - if (! $term) { |
|
| 333 | - $term = EE_Term::new_instance( |
|
| 334 | - array( |
|
| 335 | - 'name' => $category_name, |
|
| 336 | - 'slug' => $category_slug, |
|
| 337 | - ) |
|
| 338 | - ); |
|
| 339 | - $term->save(); |
|
| 340 | - } |
|
| 341 | - // make sure there's a term-taxonomy entry too |
|
| 342 | - require_once(EE_MODELS . 'EEM_Term_Taxonomy.model.php'); |
|
| 343 | - $term_taxonomy = EEM_Term_Taxonomy::instance()->get_one( |
|
| 344 | - array( |
|
| 345 | - array( |
|
| 346 | - 'term_id' => $term->ID(), |
|
| 347 | - 'taxonomy' => self::EVENT_CATEGORY_TAXONOMY, |
|
| 348 | - ), |
|
| 349 | - ) |
|
| 350 | - ); |
|
| 351 | - /** @var $term_taxonomy EE_Term_Taxonomy */ |
|
| 352 | - if (! $term_taxonomy) { |
|
| 353 | - $term_taxonomy = EE_Term_Taxonomy::new_instance( |
|
| 354 | - array( |
|
| 355 | - 'term_id' => $term->ID(), |
|
| 356 | - 'taxonomy' => self::EVENT_CATEGORY_TAXONOMY, |
|
| 357 | - 'description' => $category_description, |
|
| 358 | - 'term_count' => 1, |
|
| 359 | - 'parent' => $parent_term_taxonomy_id, |
|
| 360 | - ) |
|
| 361 | - ); |
|
| 362 | - $term_taxonomy->save(); |
|
| 363 | - } else { |
|
| 364 | - $term_taxonomy->set_count($term_taxonomy->count() + 1); |
|
| 365 | - $term_taxonomy->save(); |
|
| 366 | - } |
|
| 367 | - return $this->add_relationship_to($cpt_model_object, $term_taxonomy, 'Term_Taxonomy'); |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - |
|
| 371 | - /** |
|
| 372 | - * Removed the category specified by name as having a relation to this event. |
|
| 373 | - * Does not remove the term or term_taxonomy. |
|
| 374 | - * |
|
| 375 | - * @param EE_CPT_Base $cpt_model_object_event |
|
| 376 | - * @param string $category_name name of the event category (term) |
|
| 377 | - * @return bool |
|
| 378 | - */ |
|
| 379 | - public function remove_event_category(EE_CPT_Base $cpt_model_object_event, $category_name) |
|
| 380 | - { |
|
| 381 | - // find the term_taxonomy by that name |
|
| 382 | - $term_taxonomy = $this->get_first_related( |
|
| 383 | - $cpt_model_object_event, |
|
| 384 | - 'Term_Taxonomy', |
|
| 385 | - array(array('Term.name' => $category_name, 'taxonomy' => self::EVENT_CATEGORY_TAXONOMY)) |
|
| 386 | - ); |
|
| 387 | - /** @var $term_taxonomy EE_Term_Taxonomy */ |
|
| 388 | - if ($term_taxonomy) { |
|
| 389 | - $term_taxonomy->set_count($term_taxonomy->count() - 1); |
|
| 390 | - $term_taxonomy->save(); |
|
| 391 | - } |
|
| 392 | - return $this->remove_relationship_to($cpt_model_object_event, $term_taxonomy, 'Term_Taxonomy'); |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - |
|
| 396 | - /** |
|
| 397 | - * This is a wrapper for the WordPress get_the_post_thumbnail() function that returns the feature image for the |
|
| 398 | - * given CPT ID. It accepts the same params as what get_the_post_thumbnail() accepts. |
|
| 399 | - * |
|
| 400 | - * @link http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail |
|
| 401 | - * @access public |
|
| 402 | - * @param int $id the ID for the cpt we want the feature image for |
|
| 403 | - * @param string|array $size (optional) Image size. Defaults to 'post-thumbnail' but can also be a 2-item array |
|
| 404 | - * representing width and height in pixels (i.e. array(32,32) ). |
|
| 405 | - * @param string|array $attr Optional. Query string or array of attributes. |
|
| 406 | - * @return string HTML image element |
|
| 407 | - */ |
|
| 408 | - public function get_feature_image($id, $size = 'thumbnail', $attr = '') |
|
| 409 | - { |
|
| 410 | - return get_the_post_thumbnail($id, $size, $attr); |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - |
|
| 414 | - /** |
|
| 415 | - * Just a handy way to get the list of post statuses currently registered with WP. |
|
| 416 | - * |
|
| 417 | - * @global array $wp_post_statuses set in wp core for storing all the post stati |
|
| 418 | - * @return array |
|
| 419 | - */ |
|
| 420 | - public function get_post_statuses() |
|
| 421 | - { |
|
| 422 | - global $wp_post_statuses; |
|
| 423 | - $statuses = array(); |
|
| 424 | - foreach ($wp_post_statuses as $post_status => $args_object) { |
|
| 425 | - $statuses[ $post_status ] = $args_object->label; |
|
| 426 | - } |
|
| 427 | - return $statuses; |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - |
|
| 431 | - /** |
|
| 432 | - * public method that can be used to retrieve the protected status array on the instantiated cpt model |
|
| 433 | - * |
|
| 434 | - * @return array array of statuses. |
|
| 435 | - */ |
|
| 436 | - public function get_status_array() |
|
| 437 | - { |
|
| 438 | - $statuses = $this->get_post_statuses(); |
|
| 439 | - // first the global filter |
|
| 440 | - $statuses = apply_filters('FHEE_EEM_CPT_Base__get_status_array', $statuses); |
|
| 441 | - // now the class specific filter |
|
| 442 | - $statuses = apply_filters('FHEE_EEM_' . get_class($this) . '__get_status_array', $statuses); |
|
| 443 | - return $statuses; |
|
| 444 | - } |
|
| 445 | - |
|
| 446 | - |
|
| 447 | - /** |
|
| 448 | - * this returns the post statuses that are NOT the default wordpress status |
|
| 449 | - * |
|
| 450 | - * @return array |
|
| 451 | - */ |
|
| 452 | - public function get_custom_post_statuses() |
|
| 453 | - { |
|
| 454 | - $new_stati = array(); |
|
| 455 | - foreach ($this->_custom_stati as $status => $props) { |
|
| 456 | - $new_stati[ $status ] = $props['label']; |
|
| 457 | - } |
|
| 458 | - return $new_stati; |
|
| 459 | - } |
|
| 460 | - |
|
| 461 | - |
|
| 462 | - /** |
|
| 463 | - * Creates a child of EE_CPT_Base given a WP_Post or array of wpdb results which |
|
| 464 | - * are a row from the posts table. If we're missing any fields required for the model, |
|
| 465 | - * we just fetch the entire entry from the DB (ie, if you want to use this to save DB queries, |
|
| 466 | - * make sure you are attaching all the model's fields onto the post) |
|
| 467 | - * |
|
| 468 | - * @param WP_Post|array $post |
|
| 469 | - * @return EE_Base_Class|EE_Soft_Delete_Base_Class |
|
| 470 | - */ |
|
| 471 | - public function instantiate_class_from_post_object_orig($post) |
|
| 472 | - { |
|
| 473 | - $post = (array) $post; |
|
| 474 | - $has_all_necessary_fields_for_table = true; |
|
| 475 | - // check if the post has fields on the meta table already |
|
| 476 | - foreach ($this->_get_other_tables() as $table_obj) { |
|
| 477 | - $fields_for_that_table = $this->_get_fields_for_table($table_obj->get_table_alias()); |
|
| 478 | - foreach ($fields_for_that_table as $field_obj) { |
|
| 479 | - if (! isset($post[ $field_obj->get_table_column() ]) |
|
| 480 | - && ! isset($post[ $field_obj->get_qualified_column() ]) |
|
| 481 | - ) { |
|
| 482 | - $has_all_necessary_fields_for_table = false; |
|
| 483 | - } |
|
| 484 | - } |
|
| 485 | - } |
|
| 486 | - // if we don't have all the fields we need, then just fetch the proper model from the DB |
|
| 487 | - if (! $has_all_necessary_fields_for_table) { |
|
| 488 | - return $this->get_one_by_ID($post['ID']); |
|
| 489 | - } else { |
|
| 490 | - return $this->instantiate_class_from_array_or_object($post); |
|
| 491 | - } |
|
| 492 | - } |
|
| 493 | - |
|
| 494 | - |
|
| 495 | - /** |
|
| 496 | - * @param null $post |
|
| 497 | - * @return EE_Base_Class|EE_Soft_Delete_Base_Class |
|
| 498 | - */ |
|
| 499 | - public function instantiate_class_from_post_object($post = null) |
|
| 500 | - { |
|
| 501 | - if (empty($post)) { |
|
| 502 | - global $post; |
|
| 503 | - } |
|
| 504 | - $post = (array) $post; |
|
| 505 | - $tables_needing_to_be_queried = array(); |
|
| 506 | - // check if the post has fields on the meta table already |
|
| 507 | - foreach ($this->get_tables() as $table_obj) { |
|
| 508 | - $fields_for_that_table = $this->_get_fields_for_table($table_obj->get_table_alias()); |
|
| 509 | - foreach ($fields_for_that_table as $field_obj) { |
|
| 510 | - if (! isset($post[ $field_obj->get_table_column() ]) |
|
| 511 | - && ! isset($post[ $field_obj->get_qualified_column() ]) |
|
| 512 | - ) { |
|
| 513 | - $tables_needing_to_be_queried[ $table_obj->get_table_alias() ] = $table_obj; |
|
| 514 | - } |
|
| 515 | - } |
|
| 516 | - } |
|
| 517 | - // if we don't have all the fields we need, then just fetch the proper model from the DB |
|
| 518 | - if ($tables_needing_to_be_queried) { |
|
| 519 | - if (count($tables_needing_to_be_queried) == 1 |
|
| 520 | - && reset($tables_needing_to_be_queried) |
|
| 521 | - instanceof |
|
| 522 | - EE_Secondary_Table |
|
| 523 | - ) { |
|
| 524 | - // so we're only missing data from a secondary table. Well that's not too hard to query for |
|
| 525 | - $table_to_query = reset($tables_needing_to_be_queried); |
|
| 526 | - $missing_data = $this->_do_wpdb_query( |
|
| 527 | - 'get_row', |
|
| 528 | - array( |
|
| 529 | - 'SELECT * FROM ' |
|
| 530 | - . $table_to_query->get_table_name() |
|
| 531 | - . ' WHERE ' |
|
| 532 | - . $table_to_query->get_fk_on_table() |
|
| 533 | - . ' = ' |
|
| 534 | - . $post['ID'], |
|
| 535 | - ARRAY_A, |
|
| 536 | - ) |
|
| 537 | - ); |
|
| 538 | - if (! empty($missing_data)) { |
|
| 539 | - $post = array_merge($post, $missing_data); |
|
| 540 | - } |
|
| 541 | - } else { |
|
| 542 | - return $this->get_one_by_ID($post['ID']); |
|
| 543 | - } |
|
| 544 | - } |
|
| 545 | - return $this->instantiate_class_from_array_or_object($post); |
|
| 546 | - } |
|
| 547 | - |
|
| 548 | - |
|
| 549 | - /** |
|
| 550 | - * Gets the post type associated with this |
|
| 551 | - * |
|
| 552 | - * @throws EE_Error |
|
| 553 | - * @return string |
|
| 554 | - */ |
|
| 555 | - public function post_type() |
|
| 556 | - { |
|
| 557 | - $post_type_field = null; |
|
| 558 | - foreach ($this->field_settings(true) as $field_obj) { |
|
| 559 | - if ($field_obj instanceof EE_WP_Post_Type_Field) { |
|
| 560 | - $post_type_field = $field_obj; |
|
| 561 | - break; |
|
| 562 | - } |
|
| 563 | - } |
|
| 564 | - if ($post_type_field == null) { |
|
| 565 | - throw new EE_Error( |
|
| 566 | - sprintf( |
|
| 567 | - __( |
|
| 568 | - "CPT Model %s should have a field of type EE_WP_Post_Type, but doesnt", |
|
| 569 | - "event_espresso" |
|
| 570 | - ), |
|
| 571 | - get_class($this) |
|
| 572 | - ) |
|
| 573 | - ); |
|
| 574 | - } |
|
| 575 | - return $post_type_field->get_default_value(); |
|
| 576 | - } |
|
| 19 | + const EVENT_CATEGORY_TAXONOMY = 'espresso_event_categories'; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * @var string post_status_publish - the wp post status for published cpts |
|
| 23 | + */ |
|
| 24 | + const post_status_publish = 'publish'; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * @var string post_status_future - the wp post status for scheduled cpts |
|
| 28 | + */ |
|
| 29 | + const post_status_future = 'future'; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @var string post_status_draft - the wp post status for draft cpts |
|
| 33 | + */ |
|
| 34 | + const post_status_draft = 'draft'; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @var string post_status_pending - the wp post status for pending cpts |
|
| 38 | + */ |
|
| 39 | + const post_status_pending = 'pending'; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @var string post_status_private - the wp post status for private cpts |
|
| 43 | + */ |
|
| 44 | + const post_status_private = 'private'; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @var string post_status_trashed - the wp post status for trashed cpts |
|
| 48 | + */ |
|
| 49 | + const post_status_trashed = 'trash'; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * This is an array of custom statuses for the given CPT model (modified by children) |
|
| 53 | + * format: |
|
| 54 | + * array( |
|
| 55 | + * 'status_name' => array( |
|
| 56 | + * 'label' => __('Status Name', 'event_espresso'), |
|
| 57 | + * 'public' => TRUE //whether a public status or not. |
|
| 58 | + * ) |
|
| 59 | + * ) |
|
| 60 | + * |
|
| 61 | + * @var array |
|
| 62 | + */ |
|
| 63 | + protected $_custom_stati = array(); |
|
| 64 | + |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Adds a relationship to Term_Taxonomy for each CPT_Base |
|
| 68 | + * |
|
| 69 | + * @param string $timezone |
|
| 70 | + * @throws \EE_Error |
|
| 71 | + */ |
|
| 72 | + protected function __construct($timezone = null) |
|
| 73 | + { |
|
| 74 | + // adds a relationship to Term_Taxonomy for all these models. For this to work |
|
| 75 | + // Term_Relationship must have a relation to each model subclassing EE_CPT_Base explicitly |
|
| 76 | + // eg, in EEM_Term_Relationship, inside the _model_relations array, there must be an entry |
|
| 77 | + // with key equalling the subclassing model's model name (eg 'Event' or 'Venue'), and the value |
|
| 78 | + // must also be new EE_HABTM_Relation('Term_Relationship'); |
|
| 79 | + $this->_model_relations['Term_Taxonomy'] = new EE_HABTM_Relation('Term_Relationship'); |
|
| 80 | + $primary_table_name = null; |
|
| 81 | + // add the common _status field to all CPT primary tables. |
|
| 82 | + foreach ($this->_tables as $alias => $table_obj) { |
|
| 83 | + if ($table_obj instanceof EE_Primary_Table) { |
|
| 84 | + $primary_table_name = $alias; |
|
| 85 | + } |
|
| 86 | + } |
|
| 87 | + // set default wp post statuses if child has not already set. |
|
| 88 | + if (! isset($this->_fields[ $primary_table_name ]['status'])) { |
|
| 89 | + $this->_fields[ $primary_table_name ]['status'] = new EE_WP_Post_Status_Field( |
|
| 90 | + 'post_status', |
|
| 91 | + __("Event Status", "event_espresso"), |
|
| 92 | + false, |
|
| 93 | + 'draft' |
|
| 94 | + ); |
|
| 95 | + } |
|
| 96 | + if (! isset($this->_fields[ $primary_table_name ]['to_ping'])) { |
|
| 97 | + $this->_fields[ $primary_table_name ]['to_ping'] = new EE_DB_Only_Text_Field( |
|
| 98 | + 'to_ping', |
|
| 99 | + __('To Ping', 'event_espresso'), |
|
| 100 | + false, |
|
| 101 | + '' |
|
| 102 | + ); |
|
| 103 | + } |
|
| 104 | + if (! isset($this->_fields[ $primary_table_name ]['pinged'])) { |
|
| 105 | + $this->_fields[ $primary_table_name ]['pinged'] = new EE_DB_Only_Text_Field( |
|
| 106 | + 'pinged', |
|
| 107 | + __('Pinged', 'event_espresso'), |
|
| 108 | + false, |
|
| 109 | + '' |
|
| 110 | + ); |
|
| 111 | + } |
|
| 112 | + if (! isset($this->_fields[ $primary_table_name ]['comment_status'])) { |
|
| 113 | + $this->_fields[ $primary_table_name ]['comment_status'] = new EE_Plain_Text_Field( |
|
| 114 | + 'comment_status', |
|
| 115 | + __('Comment Status', 'event_espresso'), |
|
| 116 | + false, |
|
| 117 | + 'open' |
|
| 118 | + ); |
|
| 119 | + } |
|
| 120 | + if (! isset($this->_fields[ $primary_table_name ]['ping_status'])) { |
|
| 121 | + $this->_fields[ $primary_table_name ]['ping_status'] = new EE_Plain_Text_Field( |
|
| 122 | + 'ping_status', |
|
| 123 | + __('Ping Status', 'event_espresso'), |
|
| 124 | + false, |
|
| 125 | + 'open' |
|
| 126 | + ); |
|
| 127 | + } |
|
| 128 | + if (! isset($this->_fields[ $primary_table_name ]['post_content_filtered'])) { |
|
| 129 | + $this->_fields[ $primary_table_name ]['post_content_filtered'] = new EE_DB_Only_Text_Field( |
|
| 130 | + 'post_content_filtered', |
|
| 131 | + __('Post Content Filtered', 'event_espresso'), |
|
| 132 | + false, |
|
| 133 | + '' |
|
| 134 | + ); |
|
| 135 | + } |
|
| 136 | + if (! isset($this->_model_relations['Post_Meta'])) { |
|
| 137 | + // don't block deletes though because we want to maintain the current behaviour |
|
| 138 | + $this->_model_relations['Post_Meta'] = new EE_Has_Many_Relation(false); |
|
| 139 | + } |
|
| 140 | + if (! $this->_minimum_where_conditions_strategy instanceof EE_Default_Where_Conditions) { |
|
| 141 | + // nothing was set during child constructor, so set default |
|
| 142 | + $this->_minimum_where_conditions_strategy = new EE_CPT_Minimum_Where_Conditions($this->post_type()); |
|
| 143 | + } |
|
| 144 | + if (! $this->_default_where_conditions_strategy instanceof EE_Default_Where_Conditions) { |
|
| 145 | + // nothing was set during child constructor, so set default |
|
| 146 | + // it's ok for child classes to specify this, but generally this is more DRY |
|
| 147 | + $this->_default_where_conditions_strategy = new EE_CPT_Where_Conditions($this->post_type()); |
|
| 148 | + } |
|
| 149 | + parent::__construct($timezone); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * @return array |
|
| 155 | + */ |
|
| 156 | + public function public_event_stati() |
|
| 157 | + { |
|
| 158 | + // @see wp-includes/post.php |
|
| 159 | + return get_post_stati(array('public' => true)); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Searches for field on this model of type 'deleted_flag'. if it is found, |
|
| 165 | + * returns it's name. BUT That doesn't apply to CPTs. We should instead use post_status_field_name |
|
| 166 | + * |
|
| 167 | + * @return string |
|
| 168 | + * @throws EE_Error |
|
| 169 | + */ |
|
| 170 | + public function deleted_field_name() |
|
| 171 | + { |
|
| 172 | + throw new EE_Error( |
|
| 173 | + sprintf( |
|
| 174 | + __( |
|
| 175 | + "EEM_CPT_Base should nto call deleted_field_name! It should instead use post_status_field_name", |
|
| 176 | + "event_espresso" |
|
| 177 | + ) |
|
| 178 | + ) |
|
| 179 | + ); |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * Gets the field's name that sets the post status |
|
| 185 | + * |
|
| 186 | + * @return string |
|
| 187 | + * @throws EE_Error |
|
| 188 | + */ |
|
| 189 | + public function post_status_field_name() |
|
| 190 | + { |
|
| 191 | + $field = $this->get_a_field_of_type('EE_WP_Post_Status_Field'); |
|
| 192 | + if ($field) { |
|
| 193 | + return $field->get_name(); |
|
| 194 | + } else { |
|
| 195 | + throw new EE_Error( |
|
| 196 | + sprintf( |
|
| 197 | + __( |
|
| 198 | + 'We are trying to find the post status flag field on %s, but none was found. Are you sure there is a field of type EE_Trashed_Flag_Field in %s constructor?', |
|
| 199 | + 'event_espresso' |
|
| 200 | + ), |
|
| 201 | + get_class($this), |
|
| 202 | + get_class($this) |
|
| 203 | + ) |
|
| 204 | + ); |
|
| 205 | + } |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Alters the query params so that only trashed/soft-deleted items are considered |
|
| 211 | + * |
|
| 212 | + * @param array $query_params like EEM_Base::get_all's $query_params |
|
| 213 | + * @return array like EEM_Base::get_all's $query_params |
|
| 214 | + */ |
|
| 215 | + protected function _alter_query_params_so_only_trashed_items_included($query_params) |
|
| 216 | + { |
|
| 217 | + $post_status_field_name = $this->post_status_field_name(); |
|
| 218 | + $query_params[0][ $post_status_field_name ] = self::post_status_trashed; |
|
| 219 | + return $query_params; |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * Alters the query params so each item's deleted status is ignored. |
|
| 225 | + * |
|
| 226 | + * @param array $query_params |
|
| 227 | + * @return array |
|
| 228 | + */ |
|
| 229 | + protected function _alter_query_params_so_deleted_and_undeleted_items_included($query_params) |
|
| 230 | + { |
|
| 231 | + $query_params['default_where_conditions'] = 'minimum'; |
|
| 232 | + return $query_params; |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + |
|
| 236 | + /** |
|
| 237 | + * Performs deletes or restores on items. Both soft-deleted and non-soft-deleted items considered. |
|
| 238 | + * |
|
| 239 | + * @param boolean $delete true to indicate deletion, false to indicate restoration |
|
| 240 | + * @param array $query_params like EEM_Base::get_all |
|
| 241 | + * @return boolean success |
|
| 242 | + */ |
|
| 243 | + public function delete_or_restore($delete = true, $query_params = array()) |
|
| 244 | + { |
|
| 245 | + $post_status_field_name = $this->post_status_field_name(); |
|
| 246 | + $query_params = $this->_alter_query_params_so_deleted_and_undeleted_items_included($query_params); |
|
| 247 | + $new_status = $delete ? self::post_status_trashed : 'draft'; |
|
| 248 | + if ($this->update(array($post_status_field_name => $new_status), $query_params)) { |
|
| 249 | + return true; |
|
| 250 | + } else { |
|
| 251 | + return false; |
|
| 252 | + } |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + |
|
| 256 | + /** |
|
| 257 | + * meta_table |
|
| 258 | + * returns first EE_Secondary_Table table name |
|
| 259 | + * |
|
| 260 | + * @access public |
|
| 261 | + * @return string |
|
| 262 | + */ |
|
| 263 | + public function meta_table() |
|
| 264 | + { |
|
| 265 | + $meta_table = $this->_get_other_tables(); |
|
| 266 | + $meta_table = reset($meta_table); |
|
| 267 | + return $meta_table instanceof EE_Secondary_Table ? $meta_table->get_table_name() : null; |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + |
|
| 271 | + /** |
|
| 272 | + * This simply returns an array of the meta table fields (useful for when we just need to update those fields) |
|
| 273 | + * |
|
| 274 | + * @param bool $all triggers whether we include DB_Only fields or JUST non DB_Only fields. Defaults to false (no |
|
| 275 | + * db only fields) |
|
| 276 | + * @return array |
|
| 277 | + */ |
|
| 278 | + public function get_meta_table_fields($all = false) |
|
| 279 | + { |
|
| 280 | + $all_fields = $fields_to_return = array(); |
|
| 281 | + foreach ($this->_tables as $alias => $table_obj) { |
|
| 282 | + if ($table_obj instanceof EE_Secondary_Table) { |
|
| 283 | + $all_fields = array_merge($this->_get_fields_for_table($alias), $all_fields); |
|
| 284 | + } |
|
| 285 | + } |
|
| 286 | + if (! $all) { |
|
| 287 | + foreach ($all_fields as $name => $obj) { |
|
| 288 | + if ($obj instanceof EE_DB_Only_Field_Base) { |
|
| 289 | + continue; |
|
| 290 | + } |
|
| 291 | + $fields_to_return[] = $name; |
|
| 292 | + } |
|
| 293 | + } else { |
|
| 294 | + $fields_to_return = array_keys($all_fields); |
|
| 295 | + } |
|
| 296 | + return $fields_to_return; |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + |
|
| 300 | + /** |
|
| 301 | + * Adds an event category with the specified name and description to the specified |
|
| 302 | + * $cpt_model_object. Intelligently adds a term if necessary, and adds a term_taxonomy if necessary, |
|
| 303 | + * and adds an entry in the term_relationship if necessary. |
|
| 304 | + * |
|
| 305 | + * @param EE_CPT_Base $cpt_model_object |
|
| 306 | + * @param string $category_name (used to derive the term slug too) |
|
| 307 | + * @param string $category_description |
|
| 308 | + * @param int $parent_term_taxonomy_id |
|
| 309 | + * @return EE_Term_Taxonomy |
|
| 310 | + */ |
|
| 311 | + public function add_event_category( |
|
| 312 | + EE_CPT_Base $cpt_model_object, |
|
| 313 | + $category_name, |
|
| 314 | + $category_description = '', |
|
| 315 | + $parent_term_taxonomy_id = null |
|
| 316 | + ) { |
|
| 317 | + // create term |
|
| 318 | + require_once(EE_MODELS . 'EEM_Term.model.php'); |
|
| 319 | + // first, check for a term by the same name or slug |
|
| 320 | + $category_slug = sanitize_title($category_name); |
|
| 321 | + $term = EEM_Term::instance()->get_one( |
|
| 322 | + array( |
|
| 323 | + array( |
|
| 324 | + 'OR' => array( |
|
| 325 | + 'name' => $category_name, |
|
| 326 | + 'slug' => $category_slug, |
|
| 327 | + ), |
|
| 328 | + 'Term_Taxonomy.taxonomy' => self::EVENT_CATEGORY_TAXONOMY |
|
| 329 | + ), |
|
| 330 | + ) |
|
| 331 | + ); |
|
| 332 | + if (! $term) { |
|
| 333 | + $term = EE_Term::new_instance( |
|
| 334 | + array( |
|
| 335 | + 'name' => $category_name, |
|
| 336 | + 'slug' => $category_slug, |
|
| 337 | + ) |
|
| 338 | + ); |
|
| 339 | + $term->save(); |
|
| 340 | + } |
|
| 341 | + // make sure there's a term-taxonomy entry too |
|
| 342 | + require_once(EE_MODELS . 'EEM_Term_Taxonomy.model.php'); |
|
| 343 | + $term_taxonomy = EEM_Term_Taxonomy::instance()->get_one( |
|
| 344 | + array( |
|
| 345 | + array( |
|
| 346 | + 'term_id' => $term->ID(), |
|
| 347 | + 'taxonomy' => self::EVENT_CATEGORY_TAXONOMY, |
|
| 348 | + ), |
|
| 349 | + ) |
|
| 350 | + ); |
|
| 351 | + /** @var $term_taxonomy EE_Term_Taxonomy */ |
|
| 352 | + if (! $term_taxonomy) { |
|
| 353 | + $term_taxonomy = EE_Term_Taxonomy::new_instance( |
|
| 354 | + array( |
|
| 355 | + 'term_id' => $term->ID(), |
|
| 356 | + 'taxonomy' => self::EVENT_CATEGORY_TAXONOMY, |
|
| 357 | + 'description' => $category_description, |
|
| 358 | + 'term_count' => 1, |
|
| 359 | + 'parent' => $parent_term_taxonomy_id, |
|
| 360 | + ) |
|
| 361 | + ); |
|
| 362 | + $term_taxonomy->save(); |
|
| 363 | + } else { |
|
| 364 | + $term_taxonomy->set_count($term_taxonomy->count() + 1); |
|
| 365 | + $term_taxonomy->save(); |
|
| 366 | + } |
|
| 367 | + return $this->add_relationship_to($cpt_model_object, $term_taxonomy, 'Term_Taxonomy'); |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + |
|
| 371 | + /** |
|
| 372 | + * Removed the category specified by name as having a relation to this event. |
|
| 373 | + * Does not remove the term or term_taxonomy. |
|
| 374 | + * |
|
| 375 | + * @param EE_CPT_Base $cpt_model_object_event |
|
| 376 | + * @param string $category_name name of the event category (term) |
|
| 377 | + * @return bool |
|
| 378 | + */ |
|
| 379 | + public function remove_event_category(EE_CPT_Base $cpt_model_object_event, $category_name) |
|
| 380 | + { |
|
| 381 | + // find the term_taxonomy by that name |
|
| 382 | + $term_taxonomy = $this->get_first_related( |
|
| 383 | + $cpt_model_object_event, |
|
| 384 | + 'Term_Taxonomy', |
|
| 385 | + array(array('Term.name' => $category_name, 'taxonomy' => self::EVENT_CATEGORY_TAXONOMY)) |
|
| 386 | + ); |
|
| 387 | + /** @var $term_taxonomy EE_Term_Taxonomy */ |
|
| 388 | + if ($term_taxonomy) { |
|
| 389 | + $term_taxonomy->set_count($term_taxonomy->count() - 1); |
|
| 390 | + $term_taxonomy->save(); |
|
| 391 | + } |
|
| 392 | + return $this->remove_relationship_to($cpt_model_object_event, $term_taxonomy, 'Term_Taxonomy'); |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + |
|
| 396 | + /** |
|
| 397 | + * This is a wrapper for the WordPress get_the_post_thumbnail() function that returns the feature image for the |
|
| 398 | + * given CPT ID. It accepts the same params as what get_the_post_thumbnail() accepts. |
|
| 399 | + * |
|
| 400 | + * @link http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail |
|
| 401 | + * @access public |
|
| 402 | + * @param int $id the ID for the cpt we want the feature image for |
|
| 403 | + * @param string|array $size (optional) Image size. Defaults to 'post-thumbnail' but can also be a 2-item array |
|
| 404 | + * representing width and height in pixels (i.e. array(32,32) ). |
|
| 405 | + * @param string|array $attr Optional. Query string or array of attributes. |
|
| 406 | + * @return string HTML image element |
|
| 407 | + */ |
|
| 408 | + public function get_feature_image($id, $size = 'thumbnail', $attr = '') |
|
| 409 | + { |
|
| 410 | + return get_the_post_thumbnail($id, $size, $attr); |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + |
|
| 414 | + /** |
|
| 415 | + * Just a handy way to get the list of post statuses currently registered with WP. |
|
| 416 | + * |
|
| 417 | + * @global array $wp_post_statuses set in wp core for storing all the post stati |
|
| 418 | + * @return array |
|
| 419 | + */ |
|
| 420 | + public function get_post_statuses() |
|
| 421 | + { |
|
| 422 | + global $wp_post_statuses; |
|
| 423 | + $statuses = array(); |
|
| 424 | + foreach ($wp_post_statuses as $post_status => $args_object) { |
|
| 425 | + $statuses[ $post_status ] = $args_object->label; |
|
| 426 | + } |
|
| 427 | + return $statuses; |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + |
|
| 431 | + /** |
|
| 432 | + * public method that can be used to retrieve the protected status array on the instantiated cpt model |
|
| 433 | + * |
|
| 434 | + * @return array array of statuses. |
|
| 435 | + */ |
|
| 436 | + public function get_status_array() |
|
| 437 | + { |
|
| 438 | + $statuses = $this->get_post_statuses(); |
|
| 439 | + // first the global filter |
|
| 440 | + $statuses = apply_filters('FHEE_EEM_CPT_Base__get_status_array', $statuses); |
|
| 441 | + // now the class specific filter |
|
| 442 | + $statuses = apply_filters('FHEE_EEM_' . get_class($this) . '__get_status_array', $statuses); |
|
| 443 | + return $statuses; |
|
| 444 | + } |
|
| 445 | + |
|
| 446 | + |
|
| 447 | + /** |
|
| 448 | + * this returns the post statuses that are NOT the default wordpress status |
|
| 449 | + * |
|
| 450 | + * @return array |
|
| 451 | + */ |
|
| 452 | + public function get_custom_post_statuses() |
|
| 453 | + { |
|
| 454 | + $new_stati = array(); |
|
| 455 | + foreach ($this->_custom_stati as $status => $props) { |
|
| 456 | + $new_stati[ $status ] = $props['label']; |
|
| 457 | + } |
|
| 458 | + return $new_stati; |
|
| 459 | + } |
|
| 460 | + |
|
| 461 | + |
|
| 462 | + /** |
|
| 463 | + * Creates a child of EE_CPT_Base given a WP_Post or array of wpdb results which |
|
| 464 | + * are a row from the posts table. If we're missing any fields required for the model, |
|
| 465 | + * we just fetch the entire entry from the DB (ie, if you want to use this to save DB queries, |
|
| 466 | + * make sure you are attaching all the model's fields onto the post) |
|
| 467 | + * |
|
| 468 | + * @param WP_Post|array $post |
|
| 469 | + * @return EE_Base_Class|EE_Soft_Delete_Base_Class |
|
| 470 | + */ |
|
| 471 | + public function instantiate_class_from_post_object_orig($post) |
|
| 472 | + { |
|
| 473 | + $post = (array) $post; |
|
| 474 | + $has_all_necessary_fields_for_table = true; |
|
| 475 | + // check if the post has fields on the meta table already |
|
| 476 | + foreach ($this->_get_other_tables() as $table_obj) { |
|
| 477 | + $fields_for_that_table = $this->_get_fields_for_table($table_obj->get_table_alias()); |
|
| 478 | + foreach ($fields_for_that_table as $field_obj) { |
|
| 479 | + if (! isset($post[ $field_obj->get_table_column() ]) |
|
| 480 | + && ! isset($post[ $field_obj->get_qualified_column() ]) |
|
| 481 | + ) { |
|
| 482 | + $has_all_necessary_fields_for_table = false; |
|
| 483 | + } |
|
| 484 | + } |
|
| 485 | + } |
|
| 486 | + // if we don't have all the fields we need, then just fetch the proper model from the DB |
|
| 487 | + if (! $has_all_necessary_fields_for_table) { |
|
| 488 | + return $this->get_one_by_ID($post['ID']); |
|
| 489 | + } else { |
|
| 490 | + return $this->instantiate_class_from_array_or_object($post); |
|
| 491 | + } |
|
| 492 | + } |
|
| 493 | + |
|
| 494 | + |
|
| 495 | + /** |
|
| 496 | + * @param null $post |
|
| 497 | + * @return EE_Base_Class|EE_Soft_Delete_Base_Class |
|
| 498 | + */ |
|
| 499 | + public function instantiate_class_from_post_object($post = null) |
|
| 500 | + { |
|
| 501 | + if (empty($post)) { |
|
| 502 | + global $post; |
|
| 503 | + } |
|
| 504 | + $post = (array) $post; |
|
| 505 | + $tables_needing_to_be_queried = array(); |
|
| 506 | + // check if the post has fields on the meta table already |
|
| 507 | + foreach ($this->get_tables() as $table_obj) { |
|
| 508 | + $fields_for_that_table = $this->_get_fields_for_table($table_obj->get_table_alias()); |
|
| 509 | + foreach ($fields_for_that_table as $field_obj) { |
|
| 510 | + if (! isset($post[ $field_obj->get_table_column() ]) |
|
| 511 | + && ! isset($post[ $field_obj->get_qualified_column() ]) |
|
| 512 | + ) { |
|
| 513 | + $tables_needing_to_be_queried[ $table_obj->get_table_alias() ] = $table_obj; |
|
| 514 | + } |
|
| 515 | + } |
|
| 516 | + } |
|
| 517 | + // if we don't have all the fields we need, then just fetch the proper model from the DB |
|
| 518 | + if ($tables_needing_to_be_queried) { |
|
| 519 | + if (count($tables_needing_to_be_queried) == 1 |
|
| 520 | + && reset($tables_needing_to_be_queried) |
|
| 521 | + instanceof |
|
| 522 | + EE_Secondary_Table |
|
| 523 | + ) { |
|
| 524 | + // so we're only missing data from a secondary table. Well that's not too hard to query for |
|
| 525 | + $table_to_query = reset($tables_needing_to_be_queried); |
|
| 526 | + $missing_data = $this->_do_wpdb_query( |
|
| 527 | + 'get_row', |
|
| 528 | + array( |
|
| 529 | + 'SELECT * FROM ' |
|
| 530 | + . $table_to_query->get_table_name() |
|
| 531 | + . ' WHERE ' |
|
| 532 | + . $table_to_query->get_fk_on_table() |
|
| 533 | + . ' = ' |
|
| 534 | + . $post['ID'], |
|
| 535 | + ARRAY_A, |
|
| 536 | + ) |
|
| 537 | + ); |
|
| 538 | + if (! empty($missing_data)) { |
|
| 539 | + $post = array_merge($post, $missing_data); |
|
| 540 | + } |
|
| 541 | + } else { |
|
| 542 | + return $this->get_one_by_ID($post['ID']); |
|
| 543 | + } |
|
| 544 | + } |
|
| 545 | + return $this->instantiate_class_from_array_or_object($post); |
|
| 546 | + } |
|
| 547 | + |
|
| 548 | + |
|
| 549 | + /** |
|
| 550 | + * Gets the post type associated with this |
|
| 551 | + * |
|
| 552 | + * @throws EE_Error |
|
| 553 | + * @return string |
|
| 554 | + */ |
|
| 555 | + public function post_type() |
|
| 556 | + { |
|
| 557 | + $post_type_field = null; |
|
| 558 | + foreach ($this->field_settings(true) as $field_obj) { |
|
| 559 | + if ($field_obj instanceof EE_WP_Post_Type_Field) { |
|
| 560 | + $post_type_field = $field_obj; |
|
| 561 | + break; |
|
| 562 | + } |
|
| 563 | + } |
|
| 564 | + if ($post_type_field == null) { |
|
| 565 | + throw new EE_Error( |
|
| 566 | + sprintf( |
|
| 567 | + __( |
|
| 568 | + "CPT Model %s should have a field of type EE_WP_Post_Type, but doesnt", |
|
| 569 | + "event_espresso" |
|
| 570 | + ), |
|
| 571 | + get_class($this) |
|
| 572 | + ) |
|
| 573 | + ); |
|
| 574 | + } |
|
| 575 | + return $post_type_field->get_default_value(); |
|
| 576 | + } |
|
| 577 | 577 | } |
@@ -38,103 +38,103 @@ |
||
| 38 | 38 | * @since 4.0 |
| 39 | 39 | */ |
| 40 | 40 | if (function_exists('espresso_version')) { |
| 41 | - if (! function_exists('espresso_duplicate_plugin_error')) { |
|
| 42 | - /** |
|
| 43 | - * espresso_duplicate_plugin_error |
|
| 44 | - * displays if more than one version of EE is activated at the same time |
|
| 45 | - */ |
|
| 46 | - function espresso_duplicate_plugin_error() |
|
| 47 | - { |
|
| 48 | - ?> |
|
| 41 | + if (! function_exists('espresso_duplicate_plugin_error')) { |
|
| 42 | + /** |
|
| 43 | + * espresso_duplicate_plugin_error |
|
| 44 | + * displays if more than one version of EE is activated at the same time |
|
| 45 | + */ |
|
| 46 | + function espresso_duplicate_plugin_error() |
|
| 47 | + { |
|
| 48 | + ?> |
|
| 49 | 49 | <div class="error"> |
| 50 | 50 | <p> |
| 51 | 51 | <?php |
| 52 | - echo esc_html__( |
|
| 53 | - 'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.', |
|
| 54 | - 'event_espresso' |
|
| 55 | - ); ?> |
|
| 52 | + echo esc_html__( |
|
| 53 | + 'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.', |
|
| 54 | + 'event_espresso' |
|
| 55 | + ); ?> |
|
| 56 | 56 | </p> |
| 57 | 57 | </div> |
| 58 | 58 | <?php |
| 59 | - espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
| 60 | - } |
|
| 61 | - } |
|
| 62 | - add_action('admin_notices', 'espresso_duplicate_plugin_error', 1); |
|
| 59 | + espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
| 60 | + } |
|
| 61 | + } |
|
| 62 | + add_action('admin_notices', 'espresso_duplicate_plugin_error', 1); |
|
| 63 | 63 | } else { |
| 64 | - define('EE_MIN_PHP_VER_REQUIRED', '5.4.0'); |
|
| 65 | - if (! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) { |
|
| 66 | - /** |
|
| 67 | - * espresso_minimum_php_version_error |
|
| 68 | - * |
|
| 69 | - * @return void |
|
| 70 | - */ |
|
| 71 | - function espresso_minimum_php_version_error() |
|
| 72 | - { |
|
| 73 | - ?> |
|
| 64 | + define('EE_MIN_PHP_VER_REQUIRED', '5.4.0'); |
|
| 65 | + if (! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) { |
|
| 66 | + /** |
|
| 67 | + * espresso_minimum_php_version_error |
|
| 68 | + * |
|
| 69 | + * @return void |
|
| 70 | + */ |
|
| 71 | + function espresso_minimum_php_version_error() |
|
| 72 | + { |
|
| 73 | + ?> |
|
| 74 | 74 | <div class="error"> |
| 75 | 75 | <p> |
| 76 | 76 | <?php |
| 77 | - printf( |
|
| 78 | - esc_html__( |
|
| 79 | - 'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.', |
|
| 80 | - 'event_espresso' |
|
| 81 | - ), |
|
| 82 | - EE_MIN_PHP_VER_REQUIRED, |
|
| 83 | - PHP_VERSION, |
|
| 84 | - '<br/>', |
|
| 85 | - '<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>' |
|
| 86 | - ); |
|
| 87 | - ?> |
|
| 77 | + printf( |
|
| 78 | + esc_html__( |
|
| 79 | + 'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.', |
|
| 80 | + 'event_espresso' |
|
| 81 | + ), |
|
| 82 | + EE_MIN_PHP_VER_REQUIRED, |
|
| 83 | + PHP_VERSION, |
|
| 84 | + '<br/>', |
|
| 85 | + '<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>' |
|
| 86 | + ); |
|
| 87 | + ?> |
|
| 88 | 88 | </p> |
| 89 | 89 | </div> |
| 90 | 90 | <?php |
| 91 | - espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
| 92 | - } |
|
| 91 | + espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - add_action('admin_notices', 'espresso_minimum_php_version_error', 1); |
|
| 95 | - } else { |
|
| 96 | - define('EVENT_ESPRESSO_MAIN_FILE', __FILE__); |
|
| 97 | - /** |
|
| 98 | - * espresso_version |
|
| 99 | - * Returns the plugin version |
|
| 100 | - * |
|
| 101 | - * @return string |
|
| 102 | - */ |
|
| 103 | - function espresso_version() |
|
| 104 | - { |
|
| 105 | - return apply_filters('FHEE__espresso__espresso_version', '4.9.63.rc.009'); |
|
| 106 | - } |
|
| 94 | + add_action('admin_notices', 'espresso_minimum_php_version_error', 1); |
|
| 95 | + } else { |
|
| 96 | + define('EVENT_ESPRESSO_MAIN_FILE', __FILE__); |
|
| 97 | + /** |
|
| 98 | + * espresso_version |
|
| 99 | + * Returns the plugin version |
|
| 100 | + * |
|
| 101 | + * @return string |
|
| 102 | + */ |
|
| 103 | + function espresso_version() |
|
| 104 | + { |
|
| 105 | + return apply_filters('FHEE__espresso__espresso_version', '4.9.63.rc.009'); |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | - /** |
|
| 109 | - * espresso_plugin_activation |
|
| 110 | - * adds a wp-option to indicate that EE has been activated via the WP admin plugins page |
|
| 111 | - */ |
|
| 112 | - function espresso_plugin_activation() |
|
| 113 | - { |
|
| 114 | - update_option('ee_espresso_activation', true); |
|
| 115 | - } |
|
| 108 | + /** |
|
| 109 | + * espresso_plugin_activation |
|
| 110 | + * adds a wp-option to indicate that EE has been activated via the WP admin plugins page |
|
| 111 | + */ |
|
| 112 | + function espresso_plugin_activation() |
|
| 113 | + { |
|
| 114 | + update_option('ee_espresso_activation', true); |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation'); |
|
| 117 | + register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation'); |
|
| 118 | 118 | |
| 119 | - require_once __DIR__ . '/core/bootstrap_espresso.php'; |
|
| 120 | - bootstrap_espresso(); |
|
| 121 | - } |
|
| 119 | + require_once __DIR__ . '/core/bootstrap_espresso.php'; |
|
| 120 | + bootstrap_espresso(); |
|
| 121 | + } |
|
| 122 | 122 | } |
| 123 | 123 | if (! function_exists('espresso_deactivate_plugin')) { |
| 124 | - /** |
|
| 125 | - * deactivate_plugin |
|
| 126 | - * usage: espresso_deactivate_plugin( plugin_basename( __FILE__ )); |
|
| 127 | - * |
|
| 128 | - * @access public |
|
| 129 | - * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file |
|
| 130 | - * @return void |
|
| 131 | - */ |
|
| 132 | - function espresso_deactivate_plugin($plugin_basename = '') |
|
| 133 | - { |
|
| 134 | - if (! function_exists('deactivate_plugins')) { |
|
| 135 | - require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
|
| 136 | - } |
|
| 137 | - unset($_GET['activate'], $_REQUEST['activate']); |
|
| 138 | - deactivate_plugins($plugin_basename); |
|
| 139 | - } |
|
| 124 | + /** |
|
| 125 | + * deactivate_plugin |
|
| 126 | + * usage: espresso_deactivate_plugin( plugin_basename( __FILE__ )); |
|
| 127 | + * |
|
| 128 | + * @access public |
|
| 129 | + * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file |
|
| 130 | + * @return void |
|
| 131 | + */ |
|
| 132 | + function espresso_deactivate_plugin($plugin_basename = '') |
|
| 133 | + { |
|
| 134 | + if (! function_exists('deactivate_plugins')) { |
|
| 135 | + require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
|
| 136 | + } |
|
| 137 | + unset($_GET['activate'], $_REQUEST['activate']); |
|
| 138 | + deactivate_plugins($plugin_basename); |
|
| 139 | + } |
|
| 140 | 140 | } |
@@ -75,7 +75,7 @@ discard block |
||
| 75 | 75 | /** @var Asset $asset */ |
| 76 | 76 | $asset = $this->current(); |
| 77 | 77 | if ($asset->type() === $type) { |
| 78 | - $files[ $asset->handle() ] = $asset; |
|
| 78 | + $files[$asset->handle()] = $asset; |
|
| 79 | 79 | } |
| 80 | 80 | $this->next(); |
| 81 | 81 | } |
@@ -96,7 +96,7 @@ discard block |
||
| 96 | 96 | /** @var JavascriptAsset $asset */ |
| 97 | 97 | $asset = $this->current(); |
| 98 | 98 | if ($asset->type() === Asset::TYPE_JS && $asset->hasInlineData()) { |
| 99 | - $files[ $asset->handle() ] = $asset; |
|
| 99 | + $files[$asset->handle()] = $asset; |
|
| 100 | 100 | } |
| 101 | 101 | $this->next(); |
| 102 | 102 | } |
@@ -21,201 +21,201 @@ |
||
| 21 | 21 | { |
| 22 | 22 | |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * AssetCollection constructor |
|
| 26 | - * |
|
| 27 | - * @throws InvalidInterfaceException |
|
| 28 | - */ |
|
| 29 | - public function __construct() |
|
| 30 | - { |
|
| 31 | - parent::__construct('EventEspresso\core\domain\values\assets\Asset'); |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @return StylesheetAsset[] |
|
| 37 | - * @since 4.9.62.p |
|
| 38 | - */ |
|
| 39 | - public function getStylesheetAssets() |
|
| 40 | - { |
|
| 41 | - return $this->getAssetsOfType(Asset::TYPE_CSS); |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @return JavascriptAsset[] |
|
| 47 | - * @since 4.9.62.p |
|
| 48 | - */ |
|
| 49 | - public function getJavascriptAssets() |
|
| 50 | - { |
|
| 51 | - return $this->getAssetsOfType(Asset::TYPE_JS); |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * @return ManifestFile[] |
|
| 57 | - * @since 4.9.62.p |
|
| 58 | - */ |
|
| 59 | - public function getManifestFiles() |
|
| 60 | - { |
|
| 61 | - return $this->getAssetsOfType(Asset::TYPE_MANIFEST); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * @param $type |
|
| 67 | - * @return JavascriptAsset[]|StylesheetAsset[]|ManifestFile[] |
|
| 68 | - * @since 4.9.62.p |
|
| 69 | - */ |
|
| 70 | - protected function getAssetsOfType($type) |
|
| 71 | - { |
|
| 72 | - $files = array(); |
|
| 73 | - $this->rewind(); |
|
| 74 | - while ($this->valid()) { |
|
| 75 | - /** @var Asset $asset */ |
|
| 76 | - $asset = $this->current(); |
|
| 77 | - if ($asset->type() === $type) { |
|
| 78 | - $files[ $asset->handle() ] = $asset; |
|
| 79 | - } |
|
| 80 | - $this->next(); |
|
| 81 | - } |
|
| 82 | - $this->rewind(); |
|
| 83 | - return $files; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * @return JavascriptAsset[] |
|
| 89 | - * @since 4.9.62.p |
|
| 90 | - */ |
|
| 91 | - public function getJavascriptAssetsWithData() |
|
| 92 | - { |
|
| 93 | - $files = array(); |
|
| 94 | - $this->rewind(); |
|
| 95 | - while ($this->valid()) { |
|
| 96 | - /** @var JavascriptAsset $asset */ |
|
| 97 | - $asset = $this->current(); |
|
| 98 | - if ($asset->type() === Asset::TYPE_JS && $asset->hasInlineData()) { |
|
| 99 | - $files[ $asset->handle() ] = $asset; |
|
| 100 | - } |
|
| 101 | - $this->next(); |
|
| 102 | - } |
|
| 103 | - $this->rewind(); |
|
| 104 | - return $files; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * has |
|
| 110 | - * returns TRUE or FALSE |
|
| 111 | - * depending on whether the object is within the Collection |
|
| 112 | - * based on the supplied $identifier and type |
|
| 113 | - * |
|
| 114 | - * @access public |
|
| 115 | - * @param mixed $identifier |
|
| 116 | - * @param string $type |
|
| 117 | - * @return bool |
|
| 118 | - */ |
|
| 119 | - public function hasAssetOfType($identifier, $type = Asset::TYPE_JS) |
|
| 120 | - { |
|
| 121 | - $this->rewind(); |
|
| 122 | - while ($this->valid()) { |
|
| 123 | - if ($this->getInfo() === $identifier && $this->current()->type() === $type) { |
|
| 124 | - $this->rewind(); |
|
| 125 | - return true; |
|
| 126 | - } |
|
| 127 | - $this->next(); |
|
| 128 | - } |
|
| 129 | - return false; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * has |
|
| 135 | - * returns TRUE or FALSE |
|
| 136 | - * depending on whether the Stylesheet Asset is within the Collection |
|
| 137 | - * based on the supplied $identifier |
|
| 138 | - * |
|
| 139 | - * @access public |
|
| 140 | - * @param mixed $identifier |
|
| 141 | - * @return bool |
|
| 142 | - */ |
|
| 143 | - public function hasStylesheetAsset($identifier) |
|
| 144 | - { |
|
| 145 | - return $this->hasAssetOfType($identifier, Asset::TYPE_CSS); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * has |
|
| 151 | - * returns TRUE or FALSE |
|
| 152 | - * depending on whether the Javascript Asset is within the Collection |
|
| 153 | - * based on the supplied $identifier |
|
| 154 | - * |
|
| 155 | - * @access public |
|
| 156 | - * @param mixed $identifier |
|
| 157 | - * @return bool |
|
| 158 | - */ |
|
| 159 | - public function hasJavascriptAsset($identifier) |
|
| 160 | - { |
|
| 161 | - return $this->hasAssetOfType($identifier, Asset::TYPE_JS); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * has |
|
| 166 | - * returns TRUE or FALSE |
|
| 167 | - * depending on whether the object is within the Collection |
|
| 168 | - * based on the supplied $identifier and type |
|
| 169 | - * |
|
| 170 | - * @access public |
|
| 171 | - * @param mixed $identifier |
|
| 172 | - * @param string $type |
|
| 173 | - * @return JavascriptAsset|StylesheetAsset |
|
| 174 | - */ |
|
| 175 | - public function getAssetOfType($identifier, $type = Asset::TYPE_JS) |
|
| 176 | - { |
|
| 177 | - $this->rewind(); |
|
| 178 | - while ($this->valid()) { |
|
| 179 | - if ($this->getInfo() === $identifier && $this->current()->type() === $type) { |
|
| 180 | - /** @var JavascriptAsset|StylesheetAsset $object */ |
|
| 181 | - $object = $this->current(); |
|
| 182 | - $this->rewind(); |
|
| 183 | - return $object; |
|
| 184 | - } |
|
| 185 | - $this->next(); |
|
| 186 | - } |
|
| 187 | - return null; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * has |
|
| 193 | - * returns TRUE or FALSE |
|
| 194 | - * depending on whether the Stylesheet Asset is within the Collection |
|
| 195 | - * based on the supplied $identifier |
|
| 196 | - * |
|
| 197 | - * @access public |
|
| 198 | - * @param mixed $identifier |
|
| 199 | - * @return StylesheetAsset |
|
| 200 | - */ |
|
| 201 | - public function getStylesheetAsset($identifier) |
|
| 202 | - { |
|
| 203 | - return $this->getAssetOfType($identifier, Asset::TYPE_CSS); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * has |
|
| 209 | - * returns TRUE or FALSE |
|
| 210 | - * depending on whether the Javascript Asset is within the Collection |
|
| 211 | - * based on the supplied $identifier |
|
| 212 | - * |
|
| 213 | - * @access public |
|
| 214 | - * @param mixed $identifier |
|
| 215 | - * @return JavascriptAsset |
|
| 216 | - */ |
|
| 217 | - public function getJavascriptAsset($identifier) |
|
| 218 | - { |
|
| 219 | - return $this->getAssetOfType($identifier, Asset::TYPE_JS); |
|
| 220 | - } |
|
| 24 | + /** |
|
| 25 | + * AssetCollection constructor |
|
| 26 | + * |
|
| 27 | + * @throws InvalidInterfaceException |
|
| 28 | + */ |
|
| 29 | + public function __construct() |
|
| 30 | + { |
|
| 31 | + parent::__construct('EventEspresso\core\domain\values\assets\Asset'); |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @return StylesheetAsset[] |
|
| 37 | + * @since 4.9.62.p |
|
| 38 | + */ |
|
| 39 | + public function getStylesheetAssets() |
|
| 40 | + { |
|
| 41 | + return $this->getAssetsOfType(Asset::TYPE_CSS); |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @return JavascriptAsset[] |
|
| 47 | + * @since 4.9.62.p |
|
| 48 | + */ |
|
| 49 | + public function getJavascriptAssets() |
|
| 50 | + { |
|
| 51 | + return $this->getAssetsOfType(Asset::TYPE_JS); |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * @return ManifestFile[] |
|
| 57 | + * @since 4.9.62.p |
|
| 58 | + */ |
|
| 59 | + public function getManifestFiles() |
|
| 60 | + { |
|
| 61 | + return $this->getAssetsOfType(Asset::TYPE_MANIFEST); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * @param $type |
|
| 67 | + * @return JavascriptAsset[]|StylesheetAsset[]|ManifestFile[] |
|
| 68 | + * @since 4.9.62.p |
|
| 69 | + */ |
|
| 70 | + protected function getAssetsOfType($type) |
|
| 71 | + { |
|
| 72 | + $files = array(); |
|
| 73 | + $this->rewind(); |
|
| 74 | + while ($this->valid()) { |
|
| 75 | + /** @var Asset $asset */ |
|
| 76 | + $asset = $this->current(); |
|
| 77 | + if ($asset->type() === $type) { |
|
| 78 | + $files[ $asset->handle() ] = $asset; |
|
| 79 | + } |
|
| 80 | + $this->next(); |
|
| 81 | + } |
|
| 82 | + $this->rewind(); |
|
| 83 | + return $files; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * @return JavascriptAsset[] |
|
| 89 | + * @since 4.9.62.p |
|
| 90 | + */ |
|
| 91 | + public function getJavascriptAssetsWithData() |
|
| 92 | + { |
|
| 93 | + $files = array(); |
|
| 94 | + $this->rewind(); |
|
| 95 | + while ($this->valid()) { |
|
| 96 | + /** @var JavascriptAsset $asset */ |
|
| 97 | + $asset = $this->current(); |
|
| 98 | + if ($asset->type() === Asset::TYPE_JS && $asset->hasInlineData()) { |
|
| 99 | + $files[ $asset->handle() ] = $asset; |
|
| 100 | + } |
|
| 101 | + $this->next(); |
|
| 102 | + } |
|
| 103 | + $this->rewind(); |
|
| 104 | + return $files; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * has |
|
| 110 | + * returns TRUE or FALSE |
|
| 111 | + * depending on whether the object is within the Collection |
|
| 112 | + * based on the supplied $identifier and type |
|
| 113 | + * |
|
| 114 | + * @access public |
|
| 115 | + * @param mixed $identifier |
|
| 116 | + * @param string $type |
|
| 117 | + * @return bool |
|
| 118 | + */ |
|
| 119 | + public function hasAssetOfType($identifier, $type = Asset::TYPE_JS) |
|
| 120 | + { |
|
| 121 | + $this->rewind(); |
|
| 122 | + while ($this->valid()) { |
|
| 123 | + if ($this->getInfo() === $identifier && $this->current()->type() === $type) { |
|
| 124 | + $this->rewind(); |
|
| 125 | + return true; |
|
| 126 | + } |
|
| 127 | + $this->next(); |
|
| 128 | + } |
|
| 129 | + return false; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * has |
|
| 135 | + * returns TRUE or FALSE |
|
| 136 | + * depending on whether the Stylesheet Asset is within the Collection |
|
| 137 | + * based on the supplied $identifier |
|
| 138 | + * |
|
| 139 | + * @access public |
|
| 140 | + * @param mixed $identifier |
|
| 141 | + * @return bool |
|
| 142 | + */ |
|
| 143 | + public function hasStylesheetAsset($identifier) |
|
| 144 | + { |
|
| 145 | + return $this->hasAssetOfType($identifier, Asset::TYPE_CSS); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * has |
|
| 151 | + * returns TRUE or FALSE |
|
| 152 | + * depending on whether the Javascript Asset is within the Collection |
|
| 153 | + * based on the supplied $identifier |
|
| 154 | + * |
|
| 155 | + * @access public |
|
| 156 | + * @param mixed $identifier |
|
| 157 | + * @return bool |
|
| 158 | + */ |
|
| 159 | + public function hasJavascriptAsset($identifier) |
|
| 160 | + { |
|
| 161 | + return $this->hasAssetOfType($identifier, Asset::TYPE_JS); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * has |
|
| 166 | + * returns TRUE or FALSE |
|
| 167 | + * depending on whether the object is within the Collection |
|
| 168 | + * based on the supplied $identifier and type |
|
| 169 | + * |
|
| 170 | + * @access public |
|
| 171 | + * @param mixed $identifier |
|
| 172 | + * @param string $type |
|
| 173 | + * @return JavascriptAsset|StylesheetAsset |
|
| 174 | + */ |
|
| 175 | + public function getAssetOfType($identifier, $type = Asset::TYPE_JS) |
|
| 176 | + { |
|
| 177 | + $this->rewind(); |
|
| 178 | + while ($this->valid()) { |
|
| 179 | + if ($this->getInfo() === $identifier && $this->current()->type() === $type) { |
|
| 180 | + /** @var JavascriptAsset|StylesheetAsset $object */ |
|
| 181 | + $object = $this->current(); |
|
| 182 | + $this->rewind(); |
|
| 183 | + return $object; |
|
| 184 | + } |
|
| 185 | + $this->next(); |
|
| 186 | + } |
|
| 187 | + return null; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * has |
|
| 193 | + * returns TRUE or FALSE |
|
| 194 | + * depending on whether the Stylesheet Asset is within the Collection |
|
| 195 | + * based on the supplied $identifier |
|
| 196 | + * |
|
| 197 | + * @access public |
|
| 198 | + * @param mixed $identifier |
|
| 199 | + * @return StylesheetAsset |
|
| 200 | + */ |
|
| 201 | + public function getStylesheetAsset($identifier) |
|
| 202 | + { |
|
| 203 | + return $this->getAssetOfType($identifier, Asset::TYPE_CSS); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * has |
|
| 209 | + * returns TRUE or FALSE |
|
| 210 | + * depending on whether the Javascript Asset is within the Collection |
|
| 211 | + * based on the supplied $identifier |
|
| 212 | + * |
|
| 213 | + * @access public |
|
| 214 | + * @param mixed $identifier |
|
| 215 | + * @return JavascriptAsset |
|
| 216 | + */ |
|
| 217 | + public function getJavascriptAsset($identifier) |
|
| 218 | + { |
|
| 219 | + return $this->getAssetOfType($identifier, Asset::TYPE_JS); |
|
| 220 | + } |
|
| 221 | 221 | } |
@@ -4,7 +4,6 @@ |
||
| 4 | 4 | |
| 5 | 5 | use EventEspresso\core\domain\entities\editor\BlockInterface; |
| 6 | 6 | use EventEspresso\core\domain\services\assets\CoreAssetManager; |
| 7 | -use EventEspresso\core\domain\values\assets\Asset; |
|
| 8 | 7 | use EventEspresso\core\domain\values\assets\BrowserAsset; |
| 9 | 8 | use EventEspresso\core\domain\values\assets\JavascriptAsset; |
| 10 | 9 | use EventEspresso\core\domain\values\assets\StylesheetAsset; |
@@ -23,311 +23,311 @@ |
||
| 23 | 23 | abstract class BlockAssetManager extends AssetManager implements BlockAssetManagerInterface |
| 24 | 24 | { |
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * @var string $editor_script_handle |
|
| 28 | - */ |
|
| 29 | - private $editor_script_handle; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @var string $editor_style_handle |
|
| 33 | - */ |
|
| 34 | - private $editor_style_handle; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @var string $script_handle |
|
| 38 | - */ |
|
| 39 | - private $script_handle; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @var string $style_handle |
|
| 43 | - */ |
|
| 44 | - private $style_handle; |
|
| 45 | - |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * @return string |
|
| 49 | - */ |
|
| 50 | - public function getEditorScriptHandle() |
|
| 51 | - { |
|
| 52 | - return $this->editor_script_handle; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * @param string $editor_script_handle |
|
| 58 | - */ |
|
| 59 | - public function setEditorScriptHandle($editor_script_handle) |
|
| 60 | - { |
|
| 61 | - if(strpos($editor_script_handle, BlockInterface::NAME_SPACE . '-') !== 0) { |
|
| 62 | - $editor_script_handle = BlockInterface::NAME_SPACE . '-' . $editor_script_handle; |
|
| 63 | - } |
|
| 64 | - $this->editor_script_handle = $editor_script_handle; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * @return string |
|
| 70 | - */ |
|
| 71 | - public function getEditorStyleHandle() |
|
| 72 | - { |
|
| 73 | - return $this->editor_style_handle; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * @param string $editor_style_handle |
|
| 79 | - */ |
|
| 80 | - public function setEditorStyleHandle($editor_style_handle) |
|
| 81 | - { |
|
| 82 | - if (strpos($editor_style_handle, BlockInterface::NAME_SPACE . '-') !== 0) { |
|
| 83 | - $editor_style_handle = BlockInterface::NAME_SPACE . '-' . $editor_style_handle; |
|
| 84 | - } |
|
| 85 | - $this->editor_style_handle = $editor_style_handle; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * @return string |
|
| 91 | - */ |
|
| 92 | - public function getScriptHandle() |
|
| 93 | - { |
|
| 94 | - return $this->script_handle; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * @param string $script_handle |
|
| 100 | - */ |
|
| 101 | - public function setScriptHandle($script_handle) |
|
| 102 | - { |
|
| 103 | - if (strpos($script_handle, BlockInterface::NAME_SPACE . '-') !== 0) { |
|
| 104 | - $script_handle = BlockInterface::NAME_SPACE . '-' . $script_handle; |
|
| 105 | - } |
|
| 106 | - $this->script_handle = $script_handle; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * @return string |
|
| 112 | - */ |
|
| 113 | - public function getStyleHandle() |
|
| 114 | - { |
|
| 115 | - return $this->style_handle; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * @param string $style_handle |
|
| 121 | - */ |
|
| 122 | - public function setStyleHandle($style_handle) |
|
| 123 | - { |
|
| 124 | - if (strpos($style_handle, BlockInterface::NAME_SPACE . '-') !== 0) { |
|
| 125 | - $style_handle = BlockInterface::NAME_SPACE . '-' . $style_handle; |
|
| 126 | - } |
|
| 127 | - $this->style_handle = $style_handle; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * @since $VID:$ |
|
| 132 | - * @throws InvalidDataTypeException |
|
| 133 | - * @throws InvalidEntityException |
|
| 134 | - * @throws DuplicateCollectionIdentifierException |
|
| 135 | - */ |
|
| 136 | - public function addAssets() |
|
| 137 | - { |
|
| 138 | - $this->addEditorScript($this->getEditorScriptHandle()); |
|
| 139 | - $this->addEditorStyle($this->getEditorStyleHandle()); |
|
| 140 | - $this->setScriptHandle($this->getScriptHandle()); |
|
| 141 | - $this->setStyleHandle($this->getStyleHandle()); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * @param $handle |
|
| 147 | - * @param array $dependencies |
|
| 148 | - * @since $VID:$ |
|
| 149 | - * @return JavascriptAsset |
|
| 150 | - * @throws InvalidDataTypeException |
|
| 151 | - * @throws InvalidEntityException |
|
| 152 | - * @throws DuplicateCollectionIdentifierException |
|
| 153 | - */ |
|
| 154 | - public function addEditorScript($handle, array $dependencies = array()) |
|
| 155 | - { |
|
| 156 | - if($this->assets->hasJavascriptAsset($handle)){ |
|
| 157 | - return $this->assets->getJavascriptAsset($handle); |
|
| 158 | - } |
|
| 159 | - return parent::addJavascript( |
|
| 160 | - $handle, |
|
| 161 | - $this->registry->getJsUrl( |
|
| 162 | - $this->domain->assetNamespace(), |
|
| 163 | - $handle |
|
| 164 | - ), |
|
| 165 | - $this->addDefaultBlockScriptDependencies($dependencies) |
|
| 166 | - ) |
|
| 167 | - ->setRequiresTranslation(); |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * @param $handle |
|
| 173 | - * @param array $dependencies |
|
| 174 | - * @since $VID:$ |
|
| 175 | - * @return StylesheetAsset |
|
| 176 | - * @throws InvalidDataTypeException |
|
| 177 | - * @throws InvalidEntityException |
|
| 178 | - * @throws DuplicateCollectionIdentifierException |
|
| 179 | - */ |
|
| 180 | - public function addEditorStyle($handle, array $dependencies = array()) |
|
| 181 | - { |
|
| 182 | - if ($this->assets->hasStylesheetAsset($handle)) { |
|
| 183 | - return $this->assets->getStylesheetAsset($handle); |
|
| 184 | - } |
|
| 185 | - return parent::addStylesheet( |
|
| 186 | - $handle, |
|
| 187 | - $this->registry->getCssUrl( |
|
| 188 | - $this->domain->assetNamespace(), |
|
| 189 | - $handle |
|
| 190 | - ), |
|
| 191 | - $dependencies |
|
| 192 | - ); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - |
|
| 196 | - /** |
|
| 197 | - * @param $handle |
|
| 198 | - * @param array $dependencies |
|
| 199 | - * @since $VID:$ |
|
| 200 | - * @return JavascriptAsset |
|
| 201 | - * @throws InvalidDataTypeException |
|
| 202 | - * @throws InvalidEntityException |
|
| 203 | - * @throws DuplicateCollectionIdentifierException |
|
| 204 | - */ |
|
| 205 | - public function addScript($handle, array $dependencies = array()) |
|
| 206 | - { |
|
| 207 | - if ($this->assets->hasJavascriptAsset($handle)) { |
|
| 208 | - return $this->assets->getJavascriptAsset($handle); |
|
| 209 | - } |
|
| 210 | - return parent::addJavascript( |
|
| 211 | - $handle, |
|
| 212 | - $this->registry->getJsUrl( |
|
| 213 | - $this->domain->assetNamespace(), |
|
| 214 | - $handle |
|
| 215 | - ), |
|
| 216 | - $this->addDefaultBlockScriptDependencies($dependencies) |
|
| 217 | - ) |
|
| 218 | - ->setRequiresTranslation(); |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * @param $handle |
|
| 224 | - * @param array $dependencies |
|
| 225 | - * @since $VID:$ |
|
| 226 | - * @return StylesheetAsset |
|
| 227 | - * @throws InvalidDataTypeException |
|
| 228 | - * @throws InvalidEntityException |
|
| 229 | - * @throws DuplicateCollectionIdentifierException |
|
| 230 | - */ |
|
| 231 | - public function addStyle($handle, array $dependencies = array()) |
|
| 232 | - { |
|
| 233 | - if ($this->assets->hasStylesheetAsset($handle)) { |
|
| 234 | - return $this->assets->getStylesheetAsset($handle); |
|
| 235 | - } |
|
| 236 | - return parent::addStylesheet( |
|
| 237 | - $handle, |
|
| 238 | - $this->registry->getCssUrl( |
|
| 239 | - $this->domain->assetNamespace(), |
|
| 240 | - $handle |
|
| 241 | - ), |
|
| 242 | - $dependencies |
|
| 243 | - ); |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * @param array $dependencies |
|
| 249 | - * @return array |
|
| 250 | - */ |
|
| 251 | - protected function addDefaultBlockScriptDependencies(array $dependencies) |
|
| 252 | - { |
|
| 253 | - $dependencies += array( |
|
| 254 | - 'wp-blocks', // Provides useful functions and components for extending the editor |
|
| 255 | - 'wp-i18n', // Provides localization functions |
|
| 256 | - 'wp-element', // Provides React.Component |
|
| 257 | - 'wp-components', // Provides many prebuilt components and controls |
|
| 258 | - CoreAssetManager::JS_HANDLE_EE_COMPONENTS |
|
| 259 | - ); |
|
| 260 | - return $dependencies; |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - |
|
| 264 | - /** |
|
| 265 | - * @param string $handle |
|
| 266 | - * @param string $type |
|
| 267 | - * @return mixed|null |
|
| 268 | - * @since $VID:$ |
|
| 269 | - */ |
|
| 270 | - public function getAsset($handle, $type) |
|
| 271 | - { |
|
| 272 | - if ($this->assets->hasAssetOfType($handle, $type)) { |
|
| 273 | - return $this->assets->getAssetOfType($handle, $type); |
|
| 274 | - } |
|
| 275 | - return null; |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - |
|
| 279 | - /** |
|
| 280 | - * @return JavascriptAsset|null |
|
| 281 | - */ |
|
| 282 | - public function getEditorScript() |
|
| 283 | - { |
|
| 284 | - return $this->assets->getJavascriptAsset($this->editor_script_handle); |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * @return StylesheetAsset|null |
|
| 290 | - */ |
|
| 291 | - public function getEditorStyle() |
|
| 292 | - { |
|
| 293 | - return $this->assets->getStylesheetAsset($this->editor_style_handle); |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - |
|
| 297 | - /** |
|
| 298 | - * @return JavascriptAsset|null |
|
| 299 | - */ |
|
| 300 | - public function getScript() |
|
| 301 | - { |
|
| 302 | - return $this->assets->getJavascriptAsset($this->script_handle); |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * @return StylesheetAsset|null |
|
| 308 | - */ |
|
| 309 | - public function getStyle() |
|
| 310 | - { |
|
| 311 | - return $this->assets->getStylesheetAsset($this->style_handle); |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - |
|
| 315 | - /** |
|
| 316 | - * @return void |
|
| 317 | - */ |
|
| 318 | - public function enqueueAssets() |
|
| 319 | - { |
|
| 320 | - $assets = array( |
|
| 321 | - $this->getEditorScript(), |
|
| 322 | - $this->getEditorStyle(), |
|
| 323 | - $this->getScript(), |
|
| 324 | - $this->getStyle(), |
|
| 325 | - ); |
|
| 326 | - foreach ($assets as $asset) { |
|
| 327 | - if ($asset instanceof BrowserAsset && $asset->isRegistered()) { |
|
| 328 | - $asset->enqueueAsset(); |
|
| 329 | - } |
|
| 330 | - } |
|
| 331 | - } |
|
| 26 | + /** |
|
| 27 | + * @var string $editor_script_handle |
|
| 28 | + */ |
|
| 29 | + private $editor_script_handle; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @var string $editor_style_handle |
|
| 33 | + */ |
|
| 34 | + private $editor_style_handle; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @var string $script_handle |
|
| 38 | + */ |
|
| 39 | + private $script_handle; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @var string $style_handle |
|
| 43 | + */ |
|
| 44 | + private $style_handle; |
|
| 45 | + |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * @return string |
|
| 49 | + */ |
|
| 50 | + public function getEditorScriptHandle() |
|
| 51 | + { |
|
| 52 | + return $this->editor_script_handle; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * @param string $editor_script_handle |
|
| 58 | + */ |
|
| 59 | + public function setEditorScriptHandle($editor_script_handle) |
|
| 60 | + { |
|
| 61 | + if(strpos($editor_script_handle, BlockInterface::NAME_SPACE . '-') !== 0) { |
|
| 62 | + $editor_script_handle = BlockInterface::NAME_SPACE . '-' . $editor_script_handle; |
|
| 63 | + } |
|
| 64 | + $this->editor_script_handle = $editor_script_handle; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * @return string |
|
| 70 | + */ |
|
| 71 | + public function getEditorStyleHandle() |
|
| 72 | + { |
|
| 73 | + return $this->editor_style_handle; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * @param string $editor_style_handle |
|
| 79 | + */ |
|
| 80 | + public function setEditorStyleHandle($editor_style_handle) |
|
| 81 | + { |
|
| 82 | + if (strpos($editor_style_handle, BlockInterface::NAME_SPACE . '-') !== 0) { |
|
| 83 | + $editor_style_handle = BlockInterface::NAME_SPACE . '-' . $editor_style_handle; |
|
| 84 | + } |
|
| 85 | + $this->editor_style_handle = $editor_style_handle; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * @return string |
|
| 91 | + */ |
|
| 92 | + public function getScriptHandle() |
|
| 93 | + { |
|
| 94 | + return $this->script_handle; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * @param string $script_handle |
|
| 100 | + */ |
|
| 101 | + public function setScriptHandle($script_handle) |
|
| 102 | + { |
|
| 103 | + if (strpos($script_handle, BlockInterface::NAME_SPACE . '-') !== 0) { |
|
| 104 | + $script_handle = BlockInterface::NAME_SPACE . '-' . $script_handle; |
|
| 105 | + } |
|
| 106 | + $this->script_handle = $script_handle; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * @return string |
|
| 112 | + */ |
|
| 113 | + public function getStyleHandle() |
|
| 114 | + { |
|
| 115 | + return $this->style_handle; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * @param string $style_handle |
|
| 121 | + */ |
|
| 122 | + public function setStyleHandle($style_handle) |
|
| 123 | + { |
|
| 124 | + if (strpos($style_handle, BlockInterface::NAME_SPACE . '-') !== 0) { |
|
| 125 | + $style_handle = BlockInterface::NAME_SPACE . '-' . $style_handle; |
|
| 126 | + } |
|
| 127 | + $this->style_handle = $style_handle; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * @since $VID:$ |
|
| 132 | + * @throws InvalidDataTypeException |
|
| 133 | + * @throws InvalidEntityException |
|
| 134 | + * @throws DuplicateCollectionIdentifierException |
|
| 135 | + */ |
|
| 136 | + public function addAssets() |
|
| 137 | + { |
|
| 138 | + $this->addEditorScript($this->getEditorScriptHandle()); |
|
| 139 | + $this->addEditorStyle($this->getEditorStyleHandle()); |
|
| 140 | + $this->setScriptHandle($this->getScriptHandle()); |
|
| 141 | + $this->setStyleHandle($this->getStyleHandle()); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * @param $handle |
|
| 147 | + * @param array $dependencies |
|
| 148 | + * @since $VID:$ |
|
| 149 | + * @return JavascriptAsset |
|
| 150 | + * @throws InvalidDataTypeException |
|
| 151 | + * @throws InvalidEntityException |
|
| 152 | + * @throws DuplicateCollectionIdentifierException |
|
| 153 | + */ |
|
| 154 | + public function addEditorScript($handle, array $dependencies = array()) |
|
| 155 | + { |
|
| 156 | + if($this->assets->hasJavascriptAsset($handle)){ |
|
| 157 | + return $this->assets->getJavascriptAsset($handle); |
|
| 158 | + } |
|
| 159 | + return parent::addJavascript( |
|
| 160 | + $handle, |
|
| 161 | + $this->registry->getJsUrl( |
|
| 162 | + $this->domain->assetNamespace(), |
|
| 163 | + $handle |
|
| 164 | + ), |
|
| 165 | + $this->addDefaultBlockScriptDependencies($dependencies) |
|
| 166 | + ) |
|
| 167 | + ->setRequiresTranslation(); |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * @param $handle |
|
| 173 | + * @param array $dependencies |
|
| 174 | + * @since $VID:$ |
|
| 175 | + * @return StylesheetAsset |
|
| 176 | + * @throws InvalidDataTypeException |
|
| 177 | + * @throws InvalidEntityException |
|
| 178 | + * @throws DuplicateCollectionIdentifierException |
|
| 179 | + */ |
|
| 180 | + public function addEditorStyle($handle, array $dependencies = array()) |
|
| 181 | + { |
|
| 182 | + if ($this->assets->hasStylesheetAsset($handle)) { |
|
| 183 | + return $this->assets->getStylesheetAsset($handle); |
|
| 184 | + } |
|
| 185 | + return parent::addStylesheet( |
|
| 186 | + $handle, |
|
| 187 | + $this->registry->getCssUrl( |
|
| 188 | + $this->domain->assetNamespace(), |
|
| 189 | + $handle |
|
| 190 | + ), |
|
| 191 | + $dependencies |
|
| 192 | + ); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + |
|
| 196 | + /** |
|
| 197 | + * @param $handle |
|
| 198 | + * @param array $dependencies |
|
| 199 | + * @since $VID:$ |
|
| 200 | + * @return JavascriptAsset |
|
| 201 | + * @throws InvalidDataTypeException |
|
| 202 | + * @throws InvalidEntityException |
|
| 203 | + * @throws DuplicateCollectionIdentifierException |
|
| 204 | + */ |
|
| 205 | + public function addScript($handle, array $dependencies = array()) |
|
| 206 | + { |
|
| 207 | + if ($this->assets->hasJavascriptAsset($handle)) { |
|
| 208 | + return $this->assets->getJavascriptAsset($handle); |
|
| 209 | + } |
|
| 210 | + return parent::addJavascript( |
|
| 211 | + $handle, |
|
| 212 | + $this->registry->getJsUrl( |
|
| 213 | + $this->domain->assetNamespace(), |
|
| 214 | + $handle |
|
| 215 | + ), |
|
| 216 | + $this->addDefaultBlockScriptDependencies($dependencies) |
|
| 217 | + ) |
|
| 218 | + ->setRequiresTranslation(); |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * @param $handle |
|
| 224 | + * @param array $dependencies |
|
| 225 | + * @since $VID:$ |
|
| 226 | + * @return StylesheetAsset |
|
| 227 | + * @throws InvalidDataTypeException |
|
| 228 | + * @throws InvalidEntityException |
|
| 229 | + * @throws DuplicateCollectionIdentifierException |
|
| 230 | + */ |
|
| 231 | + public function addStyle($handle, array $dependencies = array()) |
|
| 232 | + { |
|
| 233 | + if ($this->assets->hasStylesheetAsset($handle)) { |
|
| 234 | + return $this->assets->getStylesheetAsset($handle); |
|
| 235 | + } |
|
| 236 | + return parent::addStylesheet( |
|
| 237 | + $handle, |
|
| 238 | + $this->registry->getCssUrl( |
|
| 239 | + $this->domain->assetNamespace(), |
|
| 240 | + $handle |
|
| 241 | + ), |
|
| 242 | + $dependencies |
|
| 243 | + ); |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * @param array $dependencies |
|
| 249 | + * @return array |
|
| 250 | + */ |
|
| 251 | + protected function addDefaultBlockScriptDependencies(array $dependencies) |
|
| 252 | + { |
|
| 253 | + $dependencies += array( |
|
| 254 | + 'wp-blocks', // Provides useful functions and components for extending the editor |
|
| 255 | + 'wp-i18n', // Provides localization functions |
|
| 256 | + 'wp-element', // Provides React.Component |
|
| 257 | + 'wp-components', // Provides many prebuilt components and controls |
|
| 258 | + CoreAssetManager::JS_HANDLE_EE_COMPONENTS |
|
| 259 | + ); |
|
| 260 | + return $dependencies; |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * @param string $handle |
|
| 266 | + * @param string $type |
|
| 267 | + * @return mixed|null |
|
| 268 | + * @since $VID:$ |
|
| 269 | + */ |
|
| 270 | + public function getAsset($handle, $type) |
|
| 271 | + { |
|
| 272 | + if ($this->assets->hasAssetOfType($handle, $type)) { |
|
| 273 | + return $this->assets->getAssetOfType($handle, $type); |
|
| 274 | + } |
|
| 275 | + return null; |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + |
|
| 279 | + /** |
|
| 280 | + * @return JavascriptAsset|null |
|
| 281 | + */ |
|
| 282 | + public function getEditorScript() |
|
| 283 | + { |
|
| 284 | + return $this->assets->getJavascriptAsset($this->editor_script_handle); |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * @return StylesheetAsset|null |
|
| 290 | + */ |
|
| 291 | + public function getEditorStyle() |
|
| 292 | + { |
|
| 293 | + return $this->assets->getStylesheetAsset($this->editor_style_handle); |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + |
|
| 297 | + /** |
|
| 298 | + * @return JavascriptAsset|null |
|
| 299 | + */ |
|
| 300 | + public function getScript() |
|
| 301 | + { |
|
| 302 | + return $this->assets->getJavascriptAsset($this->script_handle); |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * @return StylesheetAsset|null |
|
| 308 | + */ |
|
| 309 | + public function getStyle() |
|
| 310 | + { |
|
| 311 | + return $this->assets->getStylesheetAsset($this->style_handle); |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + |
|
| 315 | + /** |
|
| 316 | + * @return void |
|
| 317 | + */ |
|
| 318 | + public function enqueueAssets() |
|
| 319 | + { |
|
| 320 | + $assets = array( |
|
| 321 | + $this->getEditorScript(), |
|
| 322 | + $this->getEditorStyle(), |
|
| 323 | + $this->getScript(), |
|
| 324 | + $this->getStyle(), |
|
| 325 | + ); |
|
| 326 | + foreach ($assets as $asset) { |
|
| 327 | + if ($asset instanceof BrowserAsset && $asset->isRegistered()) { |
|
| 328 | + $asset->enqueueAsset(); |
|
| 329 | + } |
|
| 330 | + } |
|
| 331 | + } |
|
| 332 | 332 | |
| 333 | 333 | } |
@@ -58,8 +58,8 @@ discard block |
||
| 58 | 58 | */ |
| 59 | 59 | public function setEditorScriptHandle($editor_script_handle) |
| 60 | 60 | { |
| 61 | - if(strpos($editor_script_handle, BlockInterface::NAME_SPACE . '-') !== 0) { |
|
| 62 | - $editor_script_handle = BlockInterface::NAME_SPACE . '-' . $editor_script_handle; |
|
| 61 | + if (strpos($editor_script_handle, BlockInterface::NAME_SPACE.'-') !== 0) { |
|
| 62 | + $editor_script_handle = BlockInterface::NAME_SPACE.'-'.$editor_script_handle; |
|
| 63 | 63 | } |
| 64 | 64 | $this->editor_script_handle = $editor_script_handle; |
| 65 | 65 | } |
@@ -79,8 +79,8 @@ discard block |
||
| 79 | 79 | */ |
| 80 | 80 | public function setEditorStyleHandle($editor_style_handle) |
| 81 | 81 | { |
| 82 | - if (strpos($editor_style_handle, BlockInterface::NAME_SPACE . '-') !== 0) { |
|
| 83 | - $editor_style_handle = BlockInterface::NAME_SPACE . '-' . $editor_style_handle; |
|
| 82 | + if (strpos($editor_style_handle, BlockInterface::NAME_SPACE.'-') !== 0) { |
|
| 83 | + $editor_style_handle = BlockInterface::NAME_SPACE.'-'.$editor_style_handle; |
|
| 84 | 84 | } |
| 85 | 85 | $this->editor_style_handle = $editor_style_handle; |
| 86 | 86 | } |
@@ -100,8 +100,8 @@ discard block |
||
| 100 | 100 | */ |
| 101 | 101 | public function setScriptHandle($script_handle) |
| 102 | 102 | { |
| 103 | - if (strpos($script_handle, BlockInterface::NAME_SPACE . '-') !== 0) { |
|
| 104 | - $script_handle = BlockInterface::NAME_SPACE . '-' . $script_handle; |
|
| 103 | + if (strpos($script_handle, BlockInterface::NAME_SPACE.'-') !== 0) { |
|
| 104 | + $script_handle = BlockInterface::NAME_SPACE.'-'.$script_handle; |
|
| 105 | 105 | } |
| 106 | 106 | $this->script_handle = $script_handle; |
| 107 | 107 | } |
@@ -121,8 +121,8 @@ discard block |
||
| 121 | 121 | */ |
| 122 | 122 | public function setStyleHandle($style_handle) |
| 123 | 123 | { |
| 124 | - if (strpos($style_handle, BlockInterface::NAME_SPACE . '-') !== 0) { |
|
| 125 | - $style_handle = BlockInterface::NAME_SPACE . '-' . $style_handle; |
|
| 124 | + if (strpos($style_handle, BlockInterface::NAME_SPACE.'-') !== 0) { |
|
| 125 | + $style_handle = BlockInterface::NAME_SPACE.'-'.$style_handle; |
|
| 126 | 126 | } |
| 127 | 127 | $this->style_handle = $style_handle; |
| 128 | 128 | } |
@@ -153,7 +153,7 @@ discard block |
||
| 153 | 153 | */ |
| 154 | 154 | public function addEditorScript($handle, array $dependencies = array()) |
| 155 | 155 | { |
| 156 | - if($this->assets->hasJavascriptAsset($handle)){ |
|
| 156 | + if ($this->assets->hasJavascriptAsset($handle)) { |
|
| 157 | 157 | return $this->assets->getJavascriptAsset($handle); |
| 158 | 158 | } |
| 159 | 159 | return parent::addJavascript( |
@@ -251,9 +251,9 @@ discard block |
||
| 251 | 251 | protected function addDefaultBlockScriptDependencies(array $dependencies) |
| 252 | 252 | { |
| 253 | 253 | $dependencies += array( |
| 254 | - 'wp-blocks', // Provides useful functions and components for extending the editor |
|
| 255 | - 'wp-i18n', // Provides localization functions |
|
| 256 | - 'wp-element', // Provides React.Component |
|
| 254 | + 'wp-blocks', // Provides useful functions and components for extending the editor |
|
| 255 | + 'wp-i18n', // Provides localization functions |
|
| 256 | + 'wp-element', // Provides React.Component |
|
| 257 | 257 | 'wp-components', // Provides many prebuilt components and controls |
| 258 | 258 | CoreAssetManager::JS_HANDLE_EE_COMPONENTS |
| 259 | 259 | ); |
@@ -23,561 +23,561 @@ |
||
| 23 | 23 | class Registry |
| 24 | 24 | { |
| 25 | 25 | |
| 26 | - const FILE_NAME_BUILD_MANIFEST = 'build-manifest.json'; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * @var AssetCollection $assets |
|
| 30 | - */ |
|
| 31 | - protected $assets; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @var I18nRegistry |
|
| 35 | - */ |
|
| 36 | - private $i18n_registry; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * This holds the jsdata data object that will be exposed on pages that enqueue the `eejs-core` script. |
|
| 40 | - * |
|
| 41 | - * @var array |
|
| 42 | - */ |
|
| 43 | - protected $jsdata = array(); |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * This keeps track of all scripts with registered data. It is used to prevent duplicate data objects setup in the |
|
| 47 | - * page source. |
|
| 48 | - * |
|
| 49 | - * @var array |
|
| 50 | - */ |
|
| 51 | - private $script_handles_with_data = array(); |
|
| 52 | - |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Holds the manifest data obtained from registered manifest files. |
|
| 56 | - * Manifests are maps of asset chunk name to actual built asset file names. |
|
| 57 | - * Shape of this array is: |
|
| 58 | - * array( |
|
| 59 | - * 'some_namespace_slug' => array( |
|
| 60 | - * 'some_chunk_name' => array( |
|
| 61 | - * 'js' => 'filename.js' |
|
| 62 | - * 'css' => 'filename.js' |
|
| 63 | - * ), |
|
| 64 | - * 'url_base' => 'https://baseurl.com/to/assets |
|
| 65 | - * ) |
|
| 66 | - * ) |
|
| 67 | - * |
|
| 68 | - * @var array |
|
| 69 | - */ |
|
| 70 | - private $manifest_data = array(); |
|
| 71 | - |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Registry constructor. |
|
| 75 | - * Hooking into WP actions for script registry. |
|
| 76 | - * |
|
| 77 | - * @param AssetCollection $assets |
|
| 78 | - * @param I18nRegistry $i18n_registry |
|
| 79 | - */ |
|
| 80 | - public function __construct(AssetCollection $assets, I18nRegistry $i18n_registry) |
|
| 81 | - { |
|
| 82 | - $this->assets = $assets; |
|
| 83 | - $this->i18n_registry = $i18n_registry; |
|
| 84 | - add_action('wp_enqueue_scripts', array($this, 'registerManifestFiles'), 1); |
|
| 85 | - add_action('admin_enqueue_scripts', array($this, 'registerManifestFiles'), 1); |
|
| 86 | - add_action('wp_enqueue_scripts', array($this, 'registerScriptsAndStyles'), 3); |
|
| 87 | - add_action('admin_enqueue_scripts', array($this, 'registerScriptsAndStyles'), 3); |
|
| 88 | - add_action('wp_enqueue_scripts', array($this, 'enqueueData'), 4); |
|
| 89 | - add_action('admin_enqueue_scripts', array($this, 'enqueueData'), 4); |
|
| 90 | - add_action('wp_print_footer_scripts', array($this, 'enqueueData'), 1); |
|
| 91 | - add_action('admin_print_footer_scripts', array($this, 'enqueueData'), 1); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * For classes that have Registry as a dependency, this provides a handy way to register script handles for i18n |
|
| 97 | - * translation handling. |
|
| 98 | - * |
|
| 99 | - * @return I18nRegistry |
|
| 100 | - */ |
|
| 101 | - public function getI18nRegistry() |
|
| 102 | - { |
|
| 103 | - return $this->i18n_registry; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Callback for the wp_enqueue_scripts actions used to register assets. |
|
| 109 | - * |
|
| 110 | - * @since 4.9.62.p |
|
| 111 | - * @throws Exception |
|
| 112 | - */ |
|
| 113 | - public function registerScriptsAndStyles() |
|
| 114 | - { |
|
| 115 | - try { |
|
| 116 | - $this->registerScripts($this->assets->getJavascriptAssets()); |
|
| 117 | - $this->registerStyles($this->assets->getStylesheetAssets()); |
|
| 118 | - } catch (Exception $exception) { |
|
| 119 | - new ExceptionStackTraceDisplay($exception); |
|
| 120 | - } |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Registers JS assets with WP core |
|
| 126 | - * |
|
| 127 | - * @since 4.9.62.p |
|
| 128 | - * @param JavascriptAsset[] $scripts |
|
| 129 | - * @throws AssetRegistrationException |
|
| 130 | - * @throws InvalidDataTypeException |
|
| 131 | - */ |
|
| 132 | - public function registerScripts(array $scripts) |
|
| 133 | - { |
|
| 134 | - foreach ($scripts as $script) { |
|
| 135 | - // skip to next script if this has already been done |
|
| 136 | - if ($script->isRegistered()) { |
|
| 137 | - continue; |
|
| 138 | - } |
|
| 139 | - do_action( |
|
| 140 | - 'AHEE__EventEspresso_core_services_assets_Registry__registerScripts__before_script', |
|
| 141 | - $script |
|
| 142 | - ); |
|
| 143 | - $registered = wp_register_script( |
|
| 144 | - $script->handle(), |
|
| 145 | - $script->source(), |
|
| 146 | - $script->dependencies(), |
|
| 147 | - $script->version(), |
|
| 148 | - $script->loadInFooter() |
|
| 149 | - ); |
|
| 150 | - if (! $registered && defined('EE_DEBUG') && EE_DEBUG) { |
|
| 151 | - throw new AssetRegistrationException($script->handle()); |
|
| 152 | - } |
|
| 153 | - $script->setRegistered($registered); |
|
| 154 | - if ($script->requiresTranslation()) { |
|
| 155 | - $this->registerTranslation($script->handle()); |
|
| 156 | - } |
|
| 157 | - do_action( |
|
| 158 | - 'AHEE__EventEspresso_core_services_assets_Registry__registerScripts__after_script', |
|
| 159 | - $script |
|
| 160 | - ); |
|
| 161 | - } |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * Registers CSS assets with WP core |
|
| 167 | - * |
|
| 168 | - * @since 4.9.62.p |
|
| 169 | - * @param StylesheetAsset[] $styles |
|
| 170 | - * @throws InvalidDataTypeException |
|
| 171 | - */ |
|
| 172 | - public function registerStyles(array $styles) |
|
| 173 | - { |
|
| 174 | - foreach ($styles as $style) { |
|
| 175 | - // skip to next style if this has already been done |
|
| 176 | - if ($style->isRegistered()) { |
|
| 177 | - continue; |
|
| 178 | - } |
|
| 179 | - do_action( |
|
| 180 | - 'AHEE__EventEspresso_core_services_assets_Registry__registerStyles__before_style', |
|
| 181 | - $style |
|
| 182 | - ); |
|
| 183 | - wp_register_style( |
|
| 184 | - $style->handle(), |
|
| 185 | - $style->source(), |
|
| 186 | - $style->dependencies(), |
|
| 187 | - $style->version(), |
|
| 188 | - $style->media() |
|
| 189 | - ); |
|
| 190 | - $style->setRegistered(); |
|
| 191 | - do_action( |
|
| 192 | - 'AHEE__EventEspresso_core_services_assets_Registry__registerStyles__after_style', |
|
| 193 | - $style |
|
| 194 | - ); |
|
| 195 | - } |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * Call back for the script print in frontend and backend. |
|
| 201 | - * Used to call wp_localize_scripts so that data can be added throughout the runtime until this later hook point. |
|
| 202 | - * |
|
| 203 | - * @since 4.9.31.rc.015 |
|
| 204 | - */ |
|
| 205 | - public function enqueueData() |
|
| 206 | - { |
|
| 207 | - $this->removeAlreadyRegisteredDataForScriptHandles(); |
|
| 208 | - wp_add_inline_script( |
|
| 209 | - 'eejs-core', |
|
| 210 | - 'var eejsdata=' . wp_json_encode(array('data' => $this->jsdata)), |
|
| 211 | - 'before' |
|
| 212 | - ); |
|
| 213 | - $scripts = $this->assets->getJavascriptAssetsWithData(); |
|
| 214 | - foreach ($scripts as $script) { |
|
| 215 | - $this->addRegisteredScriptHandlesWithData($script->handle()); |
|
| 216 | - if ($script->hasInlineDataCallback()) { |
|
| 217 | - $localize = $script->inlineDataCallback(); |
|
| 218 | - $localize(); |
|
| 219 | - } |
|
| 220 | - } |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * Used to add data to eejs.data object. |
|
| 226 | - * Note: Overriding existing data is not allowed. |
|
| 227 | - * Data will be accessible as a javascript object when you list `eejs-core` as a dependency for your javascript. |
|
| 228 | - * If the data you add is something like this: |
|
| 229 | - * $this->addData( 'my_plugin_data', array( 'foo' => 'gar' ) ); |
|
| 230 | - * It will be exposed in the page source as: |
|
| 231 | - * eejs.data.my_plugin_data.foo == gar |
|
| 232 | - * |
|
| 233 | - * @param string $key Key used to access your data |
|
| 234 | - * @param string|array $value Value to attach to key |
|
| 235 | - * @throws InvalidArgumentException |
|
| 236 | - */ |
|
| 237 | - public function addData($key, $value) |
|
| 238 | - { |
|
| 239 | - if ($this->verifyDataNotExisting($key)) { |
|
| 240 | - $this->jsdata[ $key ] = $value; |
|
| 241 | - } |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - |
|
| 245 | - /** |
|
| 246 | - * Similar to addData except this allows for users to push values to an existing key where the values on key are |
|
| 247 | - * elements in an array. |
|
| 248 | - * When you use this method, the value you include will be appended to the end of an array on $key. |
|
| 249 | - * So if the $key was 'test' and you added a value of 'my_data' then it would be represented in the javascript |
|
| 250 | - * object like this, eejs.data.test = [ my_data, |
|
| 251 | - * ] |
|
| 252 | - * If there has already been a scalar value attached to the data object given key, then |
|
| 253 | - * this will throw an exception. |
|
| 254 | - * |
|
| 255 | - * @param string $key Key to attach data to. |
|
| 256 | - * @param string|array $value Value being registered. |
|
| 257 | - * @throws InvalidArgumentException |
|
| 258 | - */ |
|
| 259 | - public function pushData($key, $value) |
|
| 260 | - { |
|
| 261 | - if (isset($this->jsdata[ $key ]) |
|
| 262 | - && ! is_array($this->jsdata[ $key ]) |
|
| 263 | - ) { |
|
| 264 | - throw new InvalidArgumentException( |
|
| 265 | - sprintf( |
|
| 266 | - __( |
|
| 267 | - 'The value for %1$s is already set and it is not an array. The %2$s method can only be used to |
|
| 26 | + const FILE_NAME_BUILD_MANIFEST = 'build-manifest.json'; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * @var AssetCollection $assets |
|
| 30 | + */ |
|
| 31 | + protected $assets; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @var I18nRegistry |
|
| 35 | + */ |
|
| 36 | + private $i18n_registry; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * This holds the jsdata data object that will be exposed on pages that enqueue the `eejs-core` script. |
|
| 40 | + * |
|
| 41 | + * @var array |
|
| 42 | + */ |
|
| 43 | + protected $jsdata = array(); |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * This keeps track of all scripts with registered data. It is used to prevent duplicate data objects setup in the |
|
| 47 | + * page source. |
|
| 48 | + * |
|
| 49 | + * @var array |
|
| 50 | + */ |
|
| 51 | + private $script_handles_with_data = array(); |
|
| 52 | + |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Holds the manifest data obtained from registered manifest files. |
|
| 56 | + * Manifests are maps of asset chunk name to actual built asset file names. |
|
| 57 | + * Shape of this array is: |
|
| 58 | + * array( |
|
| 59 | + * 'some_namespace_slug' => array( |
|
| 60 | + * 'some_chunk_name' => array( |
|
| 61 | + * 'js' => 'filename.js' |
|
| 62 | + * 'css' => 'filename.js' |
|
| 63 | + * ), |
|
| 64 | + * 'url_base' => 'https://baseurl.com/to/assets |
|
| 65 | + * ) |
|
| 66 | + * ) |
|
| 67 | + * |
|
| 68 | + * @var array |
|
| 69 | + */ |
|
| 70 | + private $manifest_data = array(); |
|
| 71 | + |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Registry constructor. |
|
| 75 | + * Hooking into WP actions for script registry. |
|
| 76 | + * |
|
| 77 | + * @param AssetCollection $assets |
|
| 78 | + * @param I18nRegistry $i18n_registry |
|
| 79 | + */ |
|
| 80 | + public function __construct(AssetCollection $assets, I18nRegistry $i18n_registry) |
|
| 81 | + { |
|
| 82 | + $this->assets = $assets; |
|
| 83 | + $this->i18n_registry = $i18n_registry; |
|
| 84 | + add_action('wp_enqueue_scripts', array($this, 'registerManifestFiles'), 1); |
|
| 85 | + add_action('admin_enqueue_scripts', array($this, 'registerManifestFiles'), 1); |
|
| 86 | + add_action('wp_enqueue_scripts', array($this, 'registerScriptsAndStyles'), 3); |
|
| 87 | + add_action('admin_enqueue_scripts', array($this, 'registerScriptsAndStyles'), 3); |
|
| 88 | + add_action('wp_enqueue_scripts', array($this, 'enqueueData'), 4); |
|
| 89 | + add_action('admin_enqueue_scripts', array($this, 'enqueueData'), 4); |
|
| 90 | + add_action('wp_print_footer_scripts', array($this, 'enqueueData'), 1); |
|
| 91 | + add_action('admin_print_footer_scripts', array($this, 'enqueueData'), 1); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * For classes that have Registry as a dependency, this provides a handy way to register script handles for i18n |
|
| 97 | + * translation handling. |
|
| 98 | + * |
|
| 99 | + * @return I18nRegistry |
|
| 100 | + */ |
|
| 101 | + public function getI18nRegistry() |
|
| 102 | + { |
|
| 103 | + return $this->i18n_registry; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Callback for the wp_enqueue_scripts actions used to register assets. |
|
| 109 | + * |
|
| 110 | + * @since 4.9.62.p |
|
| 111 | + * @throws Exception |
|
| 112 | + */ |
|
| 113 | + public function registerScriptsAndStyles() |
|
| 114 | + { |
|
| 115 | + try { |
|
| 116 | + $this->registerScripts($this->assets->getJavascriptAssets()); |
|
| 117 | + $this->registerStyles($this->assets->getStylesheetAssets()); |
|
| 118 | + } catch (Exception $exception) { |
|
| 119 | + new ExceptionStackTraceDisplay($exception); |
|
| 120 | + } |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Registers JS assets with WP core |
|
| 126 | + * |
|
| 127 | + * @since 4.9.62.p |
|
| 128 | + * @param JavascriptAsset[] $scripts |
|
| 129 | + * @throws AssetRegistrationException |
|
| 130 | + * @throws InvalidDataTypeException |
|
| 131 | + */ |
|
| 132 | + public function registerScripts(array $scripts) |
|
| 133 | + { |
|
| 134 | + foreach ($scripts as $script) { |
|
| 135 | + // skip to next script if this has already been done |
|
| 136 | + if ($script->isRegistered()) { |
|
| 137 | + continue; |
|
| 138 | + } |
|
| 139 | + do_action( |
|
| 140 | + 'AHEE__EventEspresso_core_services_assets_Registry__registerScripts__before_script', |
|
| 141 | + $script |
|
| 142 | + ); |
|
| 143 | + $registered = wp_register_script( |
|
| 144 | + $script->handle(), |
|
| 145 | + $script->source(), |
|
| 146 | + $script->dependencies(), |
|
| 147 | + $script->version(), |
|
| 148 | + $script->loadInFooter() |
|
| 149 | + ); |
|
| 150 | + if (! $registered && defined('EE_DEBUG') && EE_DEBUG) { |
|
| 151 | + throw new AssetRegistrationException($script->handle()); |
|
| 152 | + } |
|
| 153 | + $script->setRegistered($registered); |
|
| 154 | + if ($script->requiresTranslation()) { |
|
| 155 | + $this->registerTranslation($script->handle()); |
|
| 156 | + } |
|
| 157 | + do_action( |
|
| 158 | + 'AHEE__EventEspresso_core_services_assets_Registry__registerScripts__after_script', |
|
| 159 | + $script |
|
| 160 | + ); |
|
| 161 | + } |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * Registers CSS assets with WP core |
|
| 167 | + * |
|
| 168 | + * @since 4.9.62.p |
|
| 169 | + * @param StylesheetAsset[] $styles |
|
| 170 | + * @throws InvalidDataTypeException |
|
| 171 | + */ |
|
| 172 | + public function registerStyles(array $styles) |
|
| 173 | + { |
|
| 174 | + foreach ($styles as $style) { |
|
| 175 | + // skip to next style if this has already been done |
|
| 176 | + if ($style->isRegistered()) { |
|
| 177 | + continue; |
|
| 178 | + } |
|
| 179 | + do_action( |
|
| 180 | + 'AHEE__EventEspresso_core_services_assets_Registry__registerStyles__before_style', |
|
| 181 | + $style |
|
| 182 | + ); |
|
| 183 | + wp_register_style( |
|
| 184 | + $style->handle(), |
|
| 185 | + $style->source(), |
|
| 186 | + $style->dependencies(), |
|
| 187 | + $style->version(), |
|
| 188 | + $style->media() |
|
| 189 | + ); |
|
| 190 | + $style->setRegistered(); |
|
| 191 | + do_action( |
|
| 192 | + 'AHEE__EventEspresso_core_services_assets_Registry__registerStyles__after_style', |
|
| 193 | + $style |
|
| 194 | + ); |
|
| 195 | + } |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * Call back for the script print in frontend and backend. |
|
| 201 | + * Used to call wp_localize_scripts so that data can be added throughout the runtime until this later hook point. |
|
| 202 | + * |
|
| 203 | + * @since 4.9.31.rc.015 |
|
| 204 | + */ |
|
| 205 | + public function enqueueData() |
|
| 206 | + { |
|
| 207 | + $this->removeAlreadyRegisteredDataForScriptHandles(); |
|
| 208 | + wp_add_inline_script( |
|
| 209 | + 'eejs-core', |
|
| 210 | + 'var eejsdata=' . wp_json_encode(array('data' => $this->jsdata)), |
|
| 211 | + 'before' |
|
| 212 | + ); |
|
| 213 | + $scripts = $this->assets->getJavascriptAssetsWithData(); |
|
| 214 | + foreach ($scripts as $script) { |
|
| 215 | + $this->addRegisteredScriptHandlesWithData($script->handle()); |
|
| 216 | + if ($script->hasInlineDataCallback()) { |
|
| 217 | + $localize = $script->inlineDataCallback(); |
|
| 218 | + $localize(); |
|
| 219 | + } |
|
| 220 | + } |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * Used to add data to eejs.data object. |
|
| 226 | + * Note: Overriding existing data is not allowed. |
|
| 227 | + * Data will be accessible as a javascript object when you list `eejs-core` as a dependency for your javascript. |
|
| 228 | + * If the data you add is something like this: |
|
| 229 | + * $this->addData( 'my_plugin_data', array( 'foo' => 'gar' ) ); |
|
| 230 | + * It will be exposed in the page source as: |
|
| 231 | + * eejs.data.my_plugin_data.foo == gar |
|
| 232 | + * |
|
| 233 | + * @param string $key Key used to access your data |
|
| 234 | + * @param string|array $value Value to attach to key |
|
| 235 | + * @throws InvalidArgumentException |
|
| 236 | + */ |
|
| 237 | + public function addData($key, $value) |
|
| 238 | + { |
|
| 239 | + if ($this->verifyDataNotExisting($key)) { |
|
| 240 | + $this->jsdata[ $key ] = $value; |
|
| 241 | + } |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + |
|
| 245 | + /** |
|
| 246 | + * Similar to addData except this allows for users to push values to an existing key where the values on key are |
|
| 247 | + * elements in an array. |
|
| 248 | + * When you use this method, the value you include will be appended to the end of an array on $key. |
|
| 249 | + * So if the $key was 'test' and you added a value of 'my_data' then it would be represented in the javascript |
|
| 250 | + * object like this, eejs.data.test = [ my_data, |
|
| 251 | + * ] |
|
| 252 | + * If there has already been a scalar value attached to the data object given key, then |
|
| 253 | + * this will throw an exception. |
|
| 254 | + * |
|
| 255 | + * @param string $key Key to attach data to. |
|
| 256 | + * @param string|array $value Value being registered. |
|
| 257 | + * @throws InvalidArgumentException |
|
| 258 | + */ |
|
| 259 | + public function pushData($key, $value) |
|
| 260 | + { |
|
| 261 | + if (isset($this->jsdata[ $key ]) |
|
| 262 | + && ! is_array($this->jsdata[ $key ]) |
|
| 263 | + ) { |
|
| 264 | + throw new InvalidArgumentException( |
|
| 265 | + sprintf( |
|
| 266 | + __( |
|
| 267 | + 'The value for %1$s is already set and it is not an array. The %2$s method can only be used to |
|
| 268 | 268 | push values to this data element when it is an array.', |
| 269 | - 'event_espresso' |
|
| 270 | - ), |
|
| 271 | - $key, |
|
| 272 | - __METHOD__ |
|
| 273 | - ) |
|
| 274 | - ); |
|
| 275 | - } |
|
| 276 | - $this->jsdata[ $key ][] = $value; |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - |
|
| 280 | - /** |
|
| 281 | - * Used to set content used by javascript for a template. |
|
| 282 | - * Note: Overrides of existing registered templates are not allowed. |
|
| 283 | - * |
|
| 284 | - * @param string $template_reference |
|
| 285 | - * @param string $template_content |
|
| 286 | - * @throws InvalidArgumentException |
|
| 287 | - */ |
|
| 288 | - public function addTemplate($template_reference, $template_content) |
|
| 289 | - { |
|
| 290 | - if (! isset($this->jsdata['templates'])) { |
|
| 291 | - $this->jsdata['templates'] = array(); |
|
| 292 | - } |
|
| 293 | - //no overrides allowed. |
|
| 294 | - if (isset($this->jsdata['templates'][ $template_reference ])) { |
|
| 295 | - throw new InvalidArgumentException( |
|
| 296 | - sprintf( |
|
| 297 | - __( |
|
| 298 | - 'The %1$s key already exists for the templates array in the js data array. No overrides are allowed.', |
|
| 299 | - 'event_espresso' |
|
| 300 | - ), |
|
| 301 | - $template_reference |
|
| 302 | - ) |
|
| 303 | - ); |
|
| 304 | - } |
|
| 305 | - $this->jsdata['templates'][ $template_reference ] = $template_content; |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * Retrieve the template content already registered for the given reference. |
|
| 311 | - * |
|
| 312 | - * @param string $template_reference |
|
| 313 | - * @return string |
|
| 314 | - */ |
|
| 315 | - public function getTemplate($template_reference) |
|
| 316 | - { |
|
| 317 | - return isset($this->jsdata['templates'][ $template_reference ]) |
|
| 318 | - ? $this->jsdata['templates'][ $template_reference ] |
|
| 319 | - : ''; |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - |
|
| 323 | - /** |
|
| 324 | - * Retrieve registered data. |
|
| 325 | - * |
|
| 326 | - * @param string $key Name of key to attach data to. |
|
| 327 | - * @return mixed If there is no for the given key, then false is returned. |
|
| 328 | - */ |
|
| 329 | - public function getData($key) |
|
| 330 | - { |
|
| 331 | - return isset($this->jsdata[ $key ]) |
|
| 332 | - ? $this->jsdata[ $key ] |
|
| 333 | - : false; |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - |
|
| 337 | - /** |
|
| 338 | - * Verifies whether the given data exists already on the jsdata array. |
|
| 339 | - * Overriding data is not allowed. |
|
| 340 | - * |
|
| 341 | - * @param string $key Index for data. |
|
| 342 | - * @return bool If valid then return true. |
|
| 343 | - * @throws InvalidArgumentException if data already exists. |
|
| 344 | - */ |
|
| 345 | - protected function verifyDataNotExisting($key) |
|
| 346 | - { |
|
| 347 | - if (isset($this->jsdata[ $key ])) { |
|
| 348 | - if (is_array($this->jsdata[ $key ])) { |
|
| 349 | - throw new InvalidArgumentException( |
|
| 350 | - sprintf( |
|
| 351 | - __( |
|
| 352 | - 'The value for %1$s already exists in the Registry::eejs object. |
|
| 269 | + 'event_espresso' |
|
| 270 | + ), |
|
| 271 | + $key, |
|
| 272 | + __METHOD__ |
|
| 273 | + ) |
|
| 274 | + ); |
|
| 275 | + } |
|
| 276 | + $this->jsdata[ $key ][] = $value; |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + |
|
| 280 | + /** |
|
| 281 | + * Used to set content used by javascript for a template. |
|
| 282 | + * Note: Overrides of existing registered templates are not allowed. |
|
| 283 | + * |
|
| 284 | + * @param string $template_reference |
|
| 285 | + * @param string $template_content |
|
| 286 | + * @throws InvalidArgumentException |
|
| 287 | + */ |
|
| 288 | + public function addTemplate($template_reference, $template_content) |
|
| 289 | + { |
|
| 290 | + if (! isset($this->jsdata['templates'])) { |
|
| 291 | + $this->jsdata['templates'] = array(); |
|
| 292 | + } |
|
| 293 | + //no overrides allowed. |
|
| 294 | + if (isset($this->jsdata['templates'][ $template_reference ])) { |
|
| 295 | + throw new InvalidArgumentException( |
|
| 296 | + sprintf( |
|
| 297 | + __( |
|
| 298 | + 'The %1$s key already exists for the templates array in the js data array. No overrides are allowed.', |
|
| 299 | + 'event_espresso' |
|
| 300 | + ), |
|
| 301 | + $template_reference |
|
| 302 | + ) |
|
| 303 | + ); |
|
| 304 | + } |
|
| 305 | + $this->jsdata['templates'][ $template_reference ] = $template_content; |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * Retrieve the template content already registered for the given reference. |
|
| 311 | + * |
|
| 312 | + * @param string $template_reference |
|
| 313 | + * @return string |
|
| 314 | + */ |
|
| 315 | + public function getTemplate($template_reference) |
|
| 316 | + { |
|
| 317 | + return isset($this->jsdata['templates'][ $template_reference ]) |
|
| 318 | + ? $this->jsdata['templates'][ $template_reference ] |
|
| 319 | + : ''; |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + |
|
| 323 | + /** |
|
| 324 | + * Retrieve registered data. |
|
| 325 | + * |
|
| 326 | + * @param string $key Name of key to attach data to. |
|
| 327 | + * @return mixed If there is no for the given key, then false is returned. |
|
| 328 | + */ |
|
| 329 | + public function getData($key) |
|
| 330 | + { |
|
| 331 | + return isset($this->jsdata[ $key ]) |
|
| 332 | + ? $this->jsdata[ $key ] |
|
| 333 | + : false; |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + |
|
| 337 | + /** |
|
| 338 | + * Verifies whether the given data exists already on the jsdata array. |
|
| 339 | + * Overriding data is not allowed. |
|
| 340 | + * |
|
| 341 | + * @param string $key Index for data. |
|
| 342 | + * @return bool If valid then return true. |
|
| 343 | + * @throws InvalidArgumentException if data already exists. |
|
| 344 | + */ |
|
| 345 | + protected function verifyDataNotExisting($key) |
|
| 346 | + { |
|
| 347 | + if (isset($this->jsdata[ $key ])) { |
|
| 348 | + if (is_array($this->jsdata[ $key ])) { |
|
| 349 | + throw new InvalidArgumentException( |
|
| 350 | + sprintf( |
|
| 351 | + __( |
|
| 352 | + 'The value for %1$s already exists in the Registry::eejs object. |
|
| 353 | 353 | Overrides are not allowed. Since the value of this data is an array, you may want to use the |
| 354 | 354 | %2$s method to push your value to the array.', |
| 355 | - 'event_espresso' |
|
| 356 | - ), |
|
| 357 | - $key, |
|
| 358 | - 'pushData()' |
|
| 359 | - ) |
|
| 360 | - ); |
|
| 361 | - } |
|
| 362 | - throw new InvalidArgumentException( |
|
| 363 | - sprintf( |
|
| 364 | - __( |
|
| 365 | - 'The value for %1$s already exists in the Registry::eejs object. Overrides are not |
|
| 355 | + 'event_espresso' |
|
| 356 | + ), |
|
| 357 | + $key, |
|
| 358 | + 'pushData()' |
|
| 359 | + ) |
|
| 360 | + ); |
|
| 361 | + } |
|
| 362 | + throw new InvalidArgumentException( |
|
| 363 | + sprintf( |
|
| 364 | + __( |
|
| 365 | + 'The value for %1$s already exists in the Registry::eejs object. Overrides are not |
|
| 366 | 366 | allowed. Consider attaching your value to a different key', |
| 367 | - 'event_espresso' |
|
| 368 | - ), |
|
| 369 | - $key |
|
| 370 | - ) |
|
| 371 | - ); |
|
| 372 | - } |
|
| 373 | - return true; |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - |
|
| 377 | - /** |
|
| 378 | - * Get the actual asset path for asset manifests. |
|
| 379 | - * If there is no asset path found for the given $chunk_name, then the $chunk_name is returned. |
|
| 380 | - * |
|
| 381 | - * @param string $namespace The namespace associated with the manifest file hosting the map of chunk_name to actual |
|
| 382 | - * asset file location. |
|
| 383 | - * @param string $chunk_name |
|
| 384 | - * @param string $asset_type |
|
| 385 | - * @return string |
|
| 386 | - * @since 4.9.59.p |
|
| 387 | - */ |
|
| 388 | - public function getAssetUrl($namespace, $chunk_name, $asset_type) |
|
| 389 | - { |
|
| 390 | - $url = isset( |
|
| 391 | - $this->manifest_data[ $namespace ][ $chunk_name . '.' . $asset_type ], |
|
| 392 | - $this->manifest_data[ $namespace ]['url_base'] |
|
| 393 | - ) |
|
| 394 | - ? $this->manifest_data[ $namespace ]['url_base'] |
|
| 395 | - . $this->manifest_data[ $namespace ][ $chunk_name . '.' . $asset_type ] |
|
| 396 | - : $chunk_name; |
|
| 397 | - return apply_filters( |
|
| 398 | - 'FHEE__EventEspresso_core_services_assets_Registry__getAssetUrl', |
|
| 399 | - $url, |
|
| 400 | - $namespace, |
|
| 401 | - $chunk_name, |
|
| 402 | - $asset_type |
|
| 403 | - ); |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - |
|
| 407 | - |
|
| 408 | - /** |
|
| 409 | - * Return the url to a js file for the given namespace and chunk name. |
|
| 410 | - * |
|
| 411 | - * @param string $namespace |
|
| 412 | - * @param string $chunk_name |
|
| 413 | - * @return string |
|
| 414 | - */ |
|
| 415 | - public function getJsUrl($namespace, $chunk_name) |
|
| 416 | - { |
|
| 417 | - return $this->getAssetUrl($namespace, $chunk_name, Asset::TYPE_JS); |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - |
|
| 421 | - /** |
|
| 422 | - * Return the url to a css file for the given namespace and chunk name. |
|
| 423 | - * |
|
| 424 | - * @param string $namespace |
|
| 425 | - * @param string $chunk_name |
|
| 426 | - * @return string |
|
| 427 | - */ |
|
| 428 | - public function getCssUrl($namespace, $chunk_name) |
|
| 429 | - { |
|
| 430 | - return $this->getAssetUrl($namespace, $chunk_name, Asset::TYPE_CSS); |
|
| 431 | - } |
|
| 432 | - |
|
| 433 | - |
|
| 434 | - /** |
|
| 435 | - * @since 4.9.62.p |
|
| 436 | - * @throws InvalidArgumentException |
|
| 437 | - * @throws InvalidFilePathException |
|
| 438 | - */ |
|
| 439 | - public function registerManifestFiles() |
|
| 440 | - { |
|
| 441 | - $manifest_files = $this->assets->getManifestFiles(); |
|
| 442 | - foreach ($manifest_files as $manifest_file) { |
|
| 443 | - $this->registerManifestFile( |
|
| 444 | - $manifest_file->assetNamespace(), |
|
| 445 | - $manifest_file->urlBase(), |
|
| 446 | - $manifest_file->filepath() . Registry::FILE_NAME_BUILD_MANIFEST |
|
| 447 | - ); |
|
| 448 | - } |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - |
|
| 452 | - /** |
|
| 453 | - * Used to register a js/css manifest file with the registered_manifest_files property. |
|
| 454 | - * |
|
| 455 | - * @param string $namespace Provided to associate the manifest file with a specific namespace. |
|
| 456 | - * @param string $url_base The url base for the manifest file location. |
|
| 457 | - * @param string $manifest_file The absolute path to the manifest file. |
|
| 458 | - * @throws InvalidArgumentException |
|
| 459 | - * @throws InvalidFilePathException |
|
| 460 | - * @since 4.9.59.p |
|
| 461 | - */ |
|
| 462 | - public function registerManifestFile($namespace, $url_base, $manifest_file) |
|
| 463 | - { |
|
| 464 | - if (isset($this->manifest_data[ $namespace ])) { |
|
| 465 | - throw new InvalidArgumentException( |
|
| 466 | - sprintf( |
|
| 467 | - esc_html__( |
|
| 468 | - 'The namespace for this manifest file has already been registered, choose a namespace other than %s', |
|
| 469 | - 'event_espresso' |
|
| 470 | - ), |
|
| 471 | - $namespace |
|
| 472 | - ) |
|
| 473 | - ); |
|
| 474 | - } |
|
| 475 | - if (filter_var($url_base, FILTER_VALIDATE_URL) === false) { |
|
| 476 | - if (is_admin()) { |
|
| 477 | - EE_Error::add_error( |
|
| 478 | - sprintf( |
|
| 479 | - esc_html__( |
|
| 480 | - 'The url given for %1$s assets is invalid. The url provided was: "%2$s". This usually happens when another plugin or theme on a site is using the "%3$s" filter or has an invalid url set for the "%4$s" constant', |
|
| 481 | - 'event_espresso' |
|
| 482 | - ), |
|
| 483 | - 'Event Espresso', |
|
| 484 | - $url_base, |
|
| 485 | - 'plugins_url', |
|
| 486 | - 'WP_PLUGIN_URL' |
|
| 487 | - ), |
|
| 488 | - __FILE__, |
|
| 489 | - __FUNCTION__, |
|
| 490 | - __LINE__ |
|
| 491 | - ); |
|
| 492 | - } |
|
| 493 | - return; |
|
| 494 | - } |
|
| 495 | - $this->manifest_data[ $namespace ] = $this->decodeManifestFile($manifest_file); |
|
| 496 | - if (! isset($this->manifest_data[ $namespace ]['url_base'])) { |
|
| 497 | - $this->manifest_data[ $namespace ]['url_base'] = trailingslashit($url_base); |
|
| 498 | - } |
|
| 499 | - } |
|
| 500 | - |
|
| 501 | - |
|
| 502 | - /** |
|
| 503 | - * Decodes json from the provided manifest file. |
|
| 504 | - * |
|
| 505 | - * @since 4.9.59.p |
|
| 506 | - * @param string $manifest_file Path to manifest file. |
|
| 507 | - * @return array |
|
| 508 | - * @throws InvalidFilePathException |
|
| 509 | - */ |
|
| 510 | - private function decodeManifestFile($manifest_file) |
|
| 511 | - { |
|
| 512 | - if (! file_exists($manifest_file)) { |
|
| 513 | - throw new InvalidFilePathException($manifest_file); |
|
| 514 | - } |
|
| 515 | - return json_decode(file_get_contents($manifest_file), true); |
|
| 516 | - } |
|
| 517 | - |
|
| 518 | - |
|
| 519 | - /** |
|
| 520 | - * This is used to set registered script handles that have data. |
|
| 521 | - * |
|
| 522 | - * @param string $script_handle |
|
| 523 | - */ |
|
| 524 | - private function addRegisteredScriptHandlesWithData($script_handle) |
|
| 525 | - { |
|
| 526 | - $this->script_handles_with_data[ $script_handle ] = $script_handle; |
|
| 527 | - } |
|
| 528 | - |
|
| 529 | - |
|
| 530 | - /**i |
|
| 367 | + 'event_espresso' |
|
| 368 | + ), |
|
| 369 | + $key |
|
| 370 | + ) |
|
| 371 | + ); |
|
| 372 | + } |
|
| 373 | + return true; |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + |
|
| 377 | + /** |
|
| 378 | + * Get the actual asset path for asset manifests. |
|
| 379 | + * If there is no asset path found for the given $chunk_name, then the $chunk_name is returned. |
|
| 380 | + * |
|
| 381 | + * @param string $namespace The namespace associated with the manifest file hosting the map of chunk_name to actual |
|
| 382 | + * asset file location. |
|
| 383 | + * @param string $chunk_name |
|
| 384 | + * @param string $asset_type |
|
| 385 | + * @return string |
|
| 386 | + * @since 4.9.59.p |
|
| 387 | + */ |
|
| 388 | + public function getAssetUrl($namespace, $chunk_name, $asset_type) |
|
| 389 | + { |
|
| 390 | + $url = isset( |
|
| 391 | + $this->manifest_data[ $namespace ][ $chunk_name . '.' . $asset_type ], |
|
| 392 | + $this->manifest_data[ $namespace ]['url_base'] |
|
| 393 | + ) |
|
| 394 | + ? $this->manifest_data[ $namespace ]['url_base'] |
|
| 395 | + . $this->manifest_data[ $namespace ][ $chunk_name . '.' . $asset_type ] |
|
| 396 | + : $chunk_name; |
|
| 397 | + return apply_filters( |
|
| 398 | + 'FHEE__EventEspresso_core_services_assets_Registry__getAssetUrl', |
|
| 399 | + $url, |
|
| 400 | + $namespace, |
|
| 401 | + $chunk_name, |
|
| 402 | + $asset_type |
|
| 403 | + ); |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + |
|
| 407 | + |
|
| 408 | + /** |
|
| 409 | + * Return the url to a js file for the given namespace and chunk name. |
|
| 410 | + * |
|
| 411 | + * @param string $namespace |
|
| 412 | + * @param string $chunk_name |
|
| 413 | + * @return string |
|
| 414 | + */ |
|
| 415 | + public function getJsUrl($namespace, $chunk_name) |
|
| 416 | + { |
|
| 417 | + return $this->getAssetUrl($namespace, $chunk_name, Asset::TYPE_JS); |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + |
|
| 421 | + /** |
|
| 422 | + * Return the url to a css file for the given namespace and chunk name. |
|
| 423 | + * |
|
| 424 | + * @param string $namespace |
|
| 425 | + * @param string $chunk_name |
|
| 426 | + * @return string |
|
| 427 | + */ |
|
| 428 | + public function getCssUrl($namespace, $chunk_name) |
|
| 429 | + { |
|
| 430 | + return $this->getAssetUrl($namespace, $chunk_name, Asset::TYPE_CSS); |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + |
|
| 434 | + /** |
|
| 435 | + * @since 4.9.62.p |
|
| 436 | + * @throws InvalidArgumentException |
|
| 437 | + * @throws InvalidFilePathException |
|
| 438 | + */ |
|
| 439 | + public function registerManifestFiles() |
|
| 440 | + { |
|
| 441 | + $manifest_files = $this->assets->getManifestFiles(); |
|
| 442 | + foreach ($manifest_files as $manifest_file) { |
|
| 443 | + $this->registerManifestFile( |
|
| 444 | + $manifest_file->assetNamespace(), |
|
| 445 | + $manifest_file->urlBase(), |
|
| 446 | + $manifest_file->filepath() . Registry::FILE_NAME_BUILD_MANIFEST |
|
| 447 | + ); |
|
| 448 | + } |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + |
|
| 452 | + /** |
|
| 453 | + * Used to register a js/css manifest file with the registered_manifest_files property. |
|
| 454 | + * |
|
| 455 | + * @param string $namespace Provided to associate the manifest file with a specific namespace. |
|
| 456 | + * @param string $url_base The url base for the manifest file location. |
|
| 457 | + * @param string $manifest_file The absolute path to the manifest file. |
|
| 458 | + * @throws InvalidArgumentException |
|
| 459 | + * @throws InvalidFilePathException |
|
| 460 | + * @since 4.9.59.p |
|
| 461 | + */ |
|
| 462 | + public function registerManifestFile($namespace, $url_base, $manifest_file) |
|
| 463 | + { |
|
| 464 | + if (isset($this->manifest_data[ $namespace ])) { |
|
| 465 | + throw new InvalidArgumentException( |
|
| 466 | + sprintf( |
|
| 467 | + esc_html__( |
|
| 468 | + 'The namespace for this manifest file has already been registered, choose a namespace other than %s', |
|
| 469 | + 'event_espresso' |
|
| 470 | + ), |
|
| 471 | + $namespace |
|
| 472 | + ) |
|
| 473 | + ); |
|
| 474 | + } |
|
| 475 | + if (filter_var($url_base, FILTER_VALIDATE_URL) === false) { |
|
| 476 | + if (is_admin()) { |
|
| 477 | + EE_Error::add_error( |
|
| 478 | + sprintf( |
|
| 479 | + esc_html__( |
|
| 480 | + 'The url given for %1$s assets is invalid. The url provided was: "%2$s". This usually happens when another plugin or theme on a site is using the "%3$s" filter or has an invalid url set for the "%4$s" constant', |
|
| 481 | + 'event_espresso' |
|
| 482 | + ), |
|
| 483 | + 'Event Espresso', |
|
| 484 | + $url_base, |
|
| 485 | + 'plugins_url', |
|
| 486 | + 'WP_PLUGIN_URL' |
|
| 487 | + ), |
|
| 488 | + __FILE__, |
|
| 489 | + __FUNCTION__, |
|
| 490 | + __LINE__ |
|
| 491 | + ); |
|
| 492 | + } |
|
| 493 | + return; |
|
| 494 | + } |
|
| 495 | + $this->manifest_data[ $namespace ] = $this->decodeManifestFile($manifest_file); |
|
| 496 | + if (! isset($this->manifest_data[ $namespace ]['url_base'])) { |
|
| 497 | + $this->manifest_data[ $namespace ]['url_base'] = trailingslashit($url_base); |
|
| 498 | + } |
|
| 499 | + } |
|
| 500 | + |
|
| 501 | + |
|
| 502 | + /** |
|
| 503 | + * Decodes json from the provided manifest file. |
|
| 504 | + * |
|
| 505 | + * @since 4.9.59.p |
|
| 506 | + * @param string $manifest_file Path to manifest file. |
|
| 507 | + * @return array |
|
| 508 | + * @throws InvalidFilePathException |
|
| 509 | + */ |
|
| 510 | + private function decodeManifestFile($manifest_file) |
|
| 511 | + { |
|
| 512 | + if (! file_exists($manifest_file)) { |
|
| 513 | + throw new InvalidFilePathException($manifest_file); |
|
| 514 | + } |
|
| 515 | + return json_decode(file_get_contents($manifest_file), true); |
|
| 516 | + } |
|
| 517 | + |
|
| 518 | + |
|
| 519 | + /** |
|
| 520 | + * This is used to set registered script handles that have data. |
|
| 521 | + * |
|
| 522 | + * @param string $script_handle |
|
| 523 | + */ |
|
| 524 | + private function addRegisteredScriptHandlesWithData($script_handle) |
|
| 525 | + { |
|
| 526 | + $this->script_handles_with_data[ $script_handle ] = $script_handle; |
|
| 527 | + } |
|
| 528 | + |
|
| 529 | + |
|
| 530 | + /**i |
|
| 531 | 531 | * Checks WP_Scripts for all of each script handle registered internally as having data and unsets from the |
| 532 | 532 | * Dependency stored in WP_Scripts if its set. |
| 533 | 533 | */ |
| 534 | - private function removeAlreadyRegisteredDataForScriptHandles() |
|
| 535 | - { |
|
| 536 | - if (empty($this->script_handles_with_data)) { |
|
| 537 | - return; |
|
| 538 | - } |
|
| 539 | - foreach ($this->script_handles_with_data as $script_handle) { |
|
| 540 | - $this->removeAlreadyRegisteredDataForScriptHandle($script_handle); |
|
| 541 | - } |
|
| 542 | - } |
|
| 543 | - |
|
| 544 | - |
|
| 545 | - /** |
|
| 546 | - * Removes any data dependency registered in WP_Scripts if its set. |
|
| 547 | - * |
|
| 548 | - * @param string $script_handle |
|
| 549 | - */ |
|
| 550 | - private function removeAlreadyRegisteredDataForScriptHandle($script_handle) |
|
| 551 | - { |
|
| 552 | - if (isset($this->script_handles_with_data[ $script_handle ])) { |
|
| 553 | - global $wp_scripts; |
|
| 554 | - $unset_handle = false; |
|
| 555 | - if ($wp_scripts->get_data($script_handle, 'data')) { |
|
| 556 | - unset($wp_scripts->registered[ $script_handle ]->extra['data']); |
|
| 557 | - $unset_handle = true; |
|
| 558 | - } |
|
| 559 | - //deal with inline_scripts |
|
| 560 | - if ($wp_scripts->get_data($script_handle, 'before')) { |
|
| 561 | - unset($wp_scripts->registered[ $script_handle ]->extra['before']); |
|
| 562 | - $unset_handle = true; |
|
| 563 | - } |
|
| 564 | - if ($wp_scripts->get_data($script_handle, 'after')) { |
|
| 565 | - unset($wp_scripts->registered[ $script_handle ]->extra['after']); |
|
| 566 | - } |
|
| 567 | - if ($unset_handle) { |
|
| 568 | - unset($this->script_handles_with_data[ $script_handle ]); |
|
| 569 | - } |
|
| 570 | - } |
|
| 571 | - } |
|
| 572 | - |
|
| 573 | - |
|
| 574 | - /** |
|
| 575 | - * register translations for a registered script |
|
| 576 | - * |
|
| 577 | - * @param string $handle |
|
| 578 | - */ |
|
| 579 | - public function registerTranslation($handle) |
|
| 580 | - { |
|
| 581 | - $this->i18n_registry->registerScriptI18n($handle); |
|
| 582 | - } |
|
| 534 | + private function removeAlreadyRegisteredDataForScriptHandles() |
|
| 535 | + { |
|
| 536 | + if (empty($this->script_handles_with_data)) { |
|
| 537 | + return; |
|
| 538 | + } |
|
| 539 | + foreach ($this->script_handles_with_data as $script_handle) { |
|
| 540 | + $this->removeAlreadyRegisteredDataForScriptHandle($script_handle); |
|
| 541 | + } |
|
| 542 | + } |
|
| 543 | + |
|
| 544 | + |
|
| 545 | + /** |
|
| 546 | + * Removes any data dependency registered in WP_Scripts if its set. |
|
| 547 | + * |
|
| 548 | + * @param string $script_handle |
|
| 549 | + */ |
|
| 550 | + private function removeAlreadyRegisteredDataForScriptHandle($script_handle) |
|
| 551 | + { |
|
| 552 | + if (isset($this->script_handles_with_data[ $script_handle ])) { |
|
| 553 | + global $wp_scripts; |
|
| 554 | + $unset_handle = false; |
|
| 555 | + if ($wp_scripts->get_data($script_handle, 'data')) { |
|
| 556 | + unset($wp_scripts->registered[ $script_handle ]->extra['data']); |
|
| 557 | + $unset_handle = true; |
|
| 558 | + } |
|
| 559 | + //deal with inline_scripts |
|
| 560 | + if ($wp_scripts->get_data($script_handle, 'before')) { |
|
| 561 | + unset($wp_scripts->registered[ $script_handle ]->extra['before']); |
|
| 562 | + $unset_handle = true; |
|
| 563 | + } |
|
| 564 | + if ($wp_scripts->get_data($script_handle, 'after')) { |
|
| 565 | + unset($wp_scripts->registered[ $script_handle ]->extra['after']); |
|
| 566 | + } |
|
| 567 | + if ($unset_handle) { |
|
| 568 | + unset($this->script_handles_with_data[ $script_handle ]); |
|
| 569 | + } |
|
| 570 | + } |
|
| 571 | + } |
|
| 572 | + |
|
| 573 | + |
|
| 574 | + /** |
|
| 575 | + * register translations for a registered script |
|
| 576 | + * |
|
| 577 | + * @param string $handle |
|
| 578 | + */ |
|
| 579 | + public function registerTranslation($handle) |
|
| 580 | + { |
|
| 581 | + $this->i18n_registry->registerScriptI18n($handle); |
|
| 582 | + } |
|
| 583 | 583 | } |