/** * parse_post_content_on_save * * any time a post is saved, we need to check for any EE shortcodes that may be embedded in the content, * and then track what posts those shortcodes are on, so that we can initialize shortcodes well before the_content() runs. * this allows us to do things like enqueue scripts for shortcodes ONLY on the pages the shortcodes are actually used on * * @access public * @param int $post_ID * @param \WP_Post $post * @return void */ public static function parse_post_content_on_save($post_ID, $post) { // if the post is trashed, then let's remove our post shortcode tracking if ($post instanceof \WP_Post && $post->post_status === 'trash') { PostShortcodeTracking::unset_post_shortcodes_on_delete($post_ID); return; } // default post types $post_types = array('post' => 0, 'page' => 1); // add CPTs $CPTs = \EE_Register_CPTs::get_CPTs(); $post_types = array_merge($post_types, $CPTs); // for default or CPT posts... if (isset($post_types[$post->post_type])) { // post on frontpage ? $page_for_posts = \EE_Config::get_page_for_posts(); if ($post->post_name === $page_for_posts) { PostShortcodeTracking::set_post_shortcodes_for_posts_page($page_for_posts); return; } // array of shortcodes indexed by post name \EE_Registry::CFG()->core->post_shortcodes = isset(\EE_Registry::CFG()->core->post_shortcodes) ? \EE_Registry::CFG()->core->post_shortcodes : array(); // whether to proceed with update $update_post_shortcodes = false; // empty both arrays \EE_Registry::CFG()->core->post_shortcodes[$post->post_name] = array(); // check that posts page is already being tracked if (!isset(\EE_Registry::CFG()->core->post_shortcodes[$page_for_posts])) { // if not, then ensure that it is properly added \EE_Registry::CFG()->core->post_shortcodes[$page_for_posts] = array(); } // loop thru shortcodes foreach (\EE_Registry::instance()->shortcodes as $EES_Shortcode => $shortcode_dir) { // convert to UPPERCASE to get actual shortcode $EES_Shortcode = strtoupper($EES_Shortcode); // is the shortcode in the post_content ? if (strpos($post->post_content, $EES_Shortcode) !== false) { // map shortcode to post names and post IDs \EE_Registry::CFG()->core->post_shortcodes[$post->post_name][$EES_Shortcode] = $post_ID; // and add this shortcode to the tracking for the blog page PostShortcodeTracking::set_post_shortcode_for_posts_page($page_for_posts, $EES_Shortcode, $post_ID); $update_post_shortcodes = true; } else { // shortcode is not present in post content, so check if we were tracking it previously // stop tracking if shortcode is not used in this specific post if (isset(\EE_Registry::CFG()->core->post_shortcodes[$post->post_name][$EES_Shortcode])) { unset(\EE_Registry::CFG()->core->post_shortcodes[$post->post_name][$EES_Shortcode]); $update_post_shortcodes = true; } // make sure that something is set for the shortcode posts (even though we may remove this) $shortcode_posts = isset(\EE_Registry::CFG()->core->post_shortcodes[$page_for_posts][$EES_Shortcode]) ? \EE_Registry::CFG()->core->post_shortcodes[$page_for_posts][$EES_Shortcode] : array(); // and stop tracking for this shortcode on the blog page if it is not used $update_post_shortcodes = PostShortcodeTracking::unset_posts_page_shortcode_for_post($post_ID, $EES_Shortcode, $shortcode_posts, $page_for_posts, $update_post_shortcodes) ? true : $update_post_shortcodes; } } if ($update_post_shortcodes) { PostShortcodeTracking::update_post_shortcodes($page_for_posts); } } }
/** * class constructor * * @access public * @return \EE_CPT_Strategy */ private function __construct() { // get CPT data $this->_CPTs = EE_Register_CPTs::get_CPTs(); $this->_CPT_endpoints = $this->_set_CPT_endpoints(); $this->_CPT_taxonomies = EE_Register_CPTs::get_taxonomies(); // d( $this->_CPTs ); // d( $this->_CPT_endpoints ); // d( $this->_CPT_taxonomies ); add_action('pre_get_posts', array($this, 'pre_get_posts'), 5); }
/** * parse_post_content_on_save * * any time a post is saved, we need to check for any EE shortcodes that may be embedded in the content, * and then track what posts those shortcodes are on, so that we can initialize shortcodes well before the_content() runs. * this allows us to do things like enqueue scripts for shortcodes ONLY on the pages the shortcodes are actually used on * * @access public * @param $post_ID * @param $post * @return void */ public static function parse_post_content_on_save($post_ID, $post) { // default post types $post_types = array('post' => 0, 'page' => 1); // add CPTs $CPTs = EE_Register_CPTs::get_CPTs(); $post_types = array_merge($post_types, $CPTs); // for default or CPT posts... if (isset($post_types[$post->post_type])) { // post on frontpage ? $page_for_posts = EE_Config::get_page_for_posts(); $maybe_remove_from_posts = array(); // critical page shortcodes that we do NOT want added to the Posts page (blog) $critical_shortcodes = EE_Registry::instance()->CFG->core->get_critical_pages_shortcodes_array(); // array of shortcodes indexed by post name EE_Registry::instance()->CFG->core->post_shortcodes = isset(EE_Registry::instance()->CFG->core->post_shortcodes) ? EE_Registry::instance()->CFG->core->post_shortcodes : array(); // whether to proceed with update, if an entry already exists for this post, then we want to update $update_post_shortcodes = isset(EE_Registry::instance()->CFG->core->post_shortcodes[$post->post_name]) ? true : false; // empty both arrays EE_Registry::instance()->CFG->core->post_shortcodes[$post->post_name] = array(); // check that posts page is already being tracked if (!isset(EE_Registry::instance()->CFG->core->post_shortcodes[$page_for_posts])) { // if not, then ensure that it is properly added EE_Registry::instance()->CFG->core->post_shortcodes[$page_for_posts] = array(); } // loop thru shortcodes foreach (EE_Registry::instance()->shortcodes as $EES_Shortcode => $shortcode_dir) { // convert to UPPERCASE to get actual shortcode $EES_Shortcode = strtoupper($EES_Shortcode); // is the shortcode in the post_content ? if (strpos($post->post_content, $EES_Shortcode) !== FALSE) { // map shortcode to post names and post IDs EE_Registry::instance()->CFG->core->post_shortcodes[$post->post_name][$EES_Shortcode] = $post_ID; // if the shortcode is NOT one of the critical page shortcodes like ESPRESSO_TXN_PAGE if (!in_array($EES_Shortcode, $critical_shortcodes)) { // add shortcode to "Posts page" tracking EE_Registry::instance()->CFG->core->post_shortcodes[$page_for_posts][$EES_Shortcode] = $post_ID; } $update_post_shortcodes = TRUE; unset($maybe_remove_from_posts[$EES_Shortcode]); } else { $maybe_remove_from_posts[$EES_Shortcode] = $post_ID; } } if ($update_post_shortcodes) { // remove shortcodes from $maybe_remove_from_posts that are still being used foreach (EE_Registry::instance()->CFG->core->post_shortcodes as $post_name => $shortcodes) { if ($post_name == $page_for_posts) { continue; } // compute difference between active post_shortcodes array and $maybe_remove_from_posts array $maybe_remove_from_posts = array_diff_key($maybe_remove_from_posts, $shortcodes); } // now unset unused shortcodes from the $page_for_posts post_shortcodes foreach ($maybe_remove_from_posts as $shortcode => $post_ID) { unset(EE_Registry::instance()->CFG->core->post_shortcodes[$page_for_posts][$shortcode]); } EE_Registry::instance()->CFG->update_post_shortcodes($page_for_posts); } } }
/** * Determine whether the current cpt supports page templates or not. * * @since %VER% * @param string $cpt_name The cpt slug we're checking on. * @return bool True supported, false not. */ private function _supports_page_templates($cpt_name) { $cpt_args = EE_Register_CPTs::get_CPTs(); $cpt_args = isset($cpt_args[$cpt_name]) ? $cpt_args[$cpt_name]['args'] : array(); return !empty($cpt_args['page_templates']) ? TRUE : FALSE; }
/** * locate_template * * locate a template file by looking in the following places, in the following order: * <assumed full absolute server path> * <server path up to>/wp-content/uploads/espresso/templates/<current EE theme>/ * <server path up to>/wp-content/uploads/espresso/templates/ * <server path up to>/wp-content/plugins/<EE4 folder>/templates/<current EE theme>/ * <server path up to>/wp-content/plugins/<EE4 folder>/<relative path> * as soon as the template is found in one of these locations, it will be returned or loaded * * @param array|string $templates array of template file names including extension (or just a single string) * @param array $template_args an array of arguments to be extracted for use in the template * @param boolean $load whether to pass the located template path on to the EEH_Template::display_template() method or simply return it * @param boolean $return_string whether to send output immediately to screen, or capture and return as a string * @param boolean $check_if_custom If TRUE, this flags this method to return boolean for whether this will generate a custom template or not. * Used in places where you don't actually load the template, you just want to know if there's a custom version of it. * @return mixed */ public static function locate_template($templates = array(), $template_args = array(), $load = TRUE, $return_string = TRUE, $check_if_custom = FALSE) { // first use WP locate_template to check for template in the current theme folder $template_path = locate_template($templates); if ($check_if_custom && !empty($template_path)) { return TRUE; } // not in the theme if (empty($template_path)) { // not even a template to look for ? if (empty($templates)) { // get post_type $post_type = EE_Registry::instance()->REQ->get('post_type'); // get array of EE Custom Post Types $EE_CPTs = EE_Register_CPTs::get_CPTs(); // build template name based on request if (isset($EE_CPTs[$post_type])) { $archive_or_single = is_archive() ? 'archive' : ''; $archive_or_single = is_single() ? 'single' : $archive_or_single; $templates = $archive_or_single . '-' . $post_type . '.php'; } } // currently active EE template theme $current_theme = EE_Config::get_current_theme(); // array of paths to folders that may contain templates $template_folder_paths = array(EVENT_ESPRESSO_TEMPLATE_DIR . $current_theme, EVENT_ESPRESSO_TEMPLATE_DIR); //add core plugin folders for checking only if we're not $check_if_custom if (!$check_if_custom) { $core_paths = array(EE_PUBLIC . $current_theme, EE_TEMPLATES . $current_theme, EE_PLUGIN_DIR_PATH); $template_folder_paths = array_merge($template_folder_paths, $core_paths); } // now filter that array $template_folder_paths = apply_filters('FHEE__EEH_Template__locate_template__template_folder_paths', $template_folder_paths); // array to hold all possible template paths $full_template_paths = array(); // loop through $templates foreach ((array) $templates as $template) { // while looping through all template folder paths foreach ((array) $template_folder_paths as $template_folder_path) { // build up our template locations array by combining our template folder paths with our templates $full_template_paths[] = rtrim($template_folder_path, DS) . DS . $template; } // if $template is an absolute path, then we'll tack it onto the start of our array so that it gets searched first array_unshift($full_template_paths, $template); } // filter final array of full template paths $full_template_paths = apply_filters('FHEE__EEH_Template__locate_template__full_template_paths', $full_template_paths); // now loop through our final array of template location paths and check each location foreach ((array) $full_template_paths as $full_template_path) { if (is_readable($full_template_path)) { $template_path = str_replace(array('\\', '/'), DIRECTORY_SEPARATOR, $full_template_path); break; } } } // if we got it and you want to see it... if ($template_path && $load && !$check_if_custom) { if ($return_string) { return EEH_Template::display_template($template_path, $template_args, TRUE); } else { EEH_Template::display_template($template_path, $template_args, FALSE); } } return $check_if_custom && !empty($template_path) ? TRUE : $template_path; }
/** * locate_template * * locate a template file by looking in the following places, in the following order: * <server path up to>/wp-content/themes/<current active WordPress theme>/ * <assumed full absolute server path> * <server path up to>/wp-content/uploads/espresso/templates/<current EE theme>/ * <server path up to>/wp-content/uploads/espresso/templates/ * <server path up to>/wp-content/plugins/<EE4 folder>/public/<current EE theme>/ * <server path up to>/wp-content/plugins/<EE4 folder>/core/templates/<current EE theme>/ * <server path up to>/wp-content/plugins/<EE4 folder>/ * as soon as the template is found in one of these locations, it will be returned or loaded * * Example: * You are using the WordPress Twenty Sixteen theme, * and you want to customize the "some-event.template.php" template, * which is located in the "/relative/path/to/" folder relative to the main EE plugin folder. * Assuming WP is installed on your server in the "/home/public_html/" folder, * EEH_Template::locate_template() will look at the following paths in order until the template is found: * * /home/public_html/wp-content/themes/twentysixteen/some-event.template.php * /relative/path/to/some-event.template.php * /home/public_html/wp-content/uploads/espresso/templates/Espresso_Arabica_2014/relative/path/to/some-event.template.php * /home/public_html/wp-content/uploads/espresso/templates/relative/path/to/some-event.template.php * /home/public_html/wp-content/plugins/event-espresso-core-reg/public/Espresso_Arabica_2014/relative/path/to/some-event.template.php * /home/public_html/wp-content/plugins/event-espresso-core-reg/core/templates/Espresso_Arabica_2014/relative/path/to/some-event.template.php * /home/public_html/wp-content/plugins/event-espresso-core-reg/relative/path/to/some-event.template.php * * Had you passed an absolute path to your template that was in some other location, * ie: "/absolute/path/to/some-event.template.php" * then the search would have been : * * /home/public_html/wp-content/themes/twentysixteen/some-event.template.php * /absolute/path/to/some-event.template.php * * and stopped there upon finding it in the second location * * @param array|string $templates array of template file names including extension (or just a single string) * @param array $template_args an array of arguments to be extracted for use in the template * @param boolean $load whether to pass the located template path on to the EEH_Template::display_template() method or simply return it * @param boolean $return_string whether to send output immediately to screen, or capture and return as a string * @param boolean $check_if_custom If TRUE, this flags this method to return boolean for whether this will generate a custom template or not. * Used in places where you don't actually load the template, you just want to know if there's a custom version of it. * @return mixed */ public static function locate_template($templates = array(), $template_args = array(), $load = TRUE, $return_string = TRUE, $check_if_custom = FALSE) { // first use WP locate_template to check for template in the current theme folder $template_path = locate_template($templates); if ($check_if_custom && !empty($template_path)) { return TRUE; } // not in the theme if (empty($template_path)) { // not even a template to look for ? if (empty($templates)) { // get post_type $post_type = EE_Registry::instance()->REQ->get('post_type'); // get array of EE Custom Post Types $EE_CPTs = EE_Register_CPTs::get_CPTs(); // build template name based on request if (isset($EE_CPTs[$post_type])) { $archive_or_single = is_archive() ? 'archive' : ''; $archive_or_single = is_single() ? 'single' : $archive_or_single; $templates = $archive_or_single . '-' . $post_type . '.php'; } } // currently active EE template theme $current_theme = EE_Config::get_current_theme(); // array of paths to folders that may contain templates $template_folder_paths = array(EVENT_ESPRESSO_TEMPLATE_DIR . $current_theme, EVENT_ESPRESSO_TEMPLATE_DIR); //add core plugin folders for checking only if we're not $check_if_custom if (!$check_if_custom) { $core_paths = array(EE_PUBLIC . $current_theme, EE_TEMPLATES . $current_theme, EE_PLUGIN_DIR_PATH); $template_folder_paths = array_merge($template_folder_paths, $core_paths); } // now filter that array $template_folder_paths = apply_filters('FHEE__EEH_Template__locate_template__template_folder_paths', $template_folder_paths); $templates = is_array($templates) ? $templates : array($templates); $template_folder_paths = is_array($template_folder_paths) ? $template_folder_paths : array($template_folder_paths); // array to hold all possible template paths $full_template_paths = array(); EE_Registry::instance()->load_helper('File'); // loop through $templates foreach ($templates as $template) { // normalize directory separators $template = EEH_File::standardise_directory_separators($template); $file_name = basename($template); $template_path_minus_file_name = substr($template, 0, strlen($file_name) * -1); // while looping through all template folder paths foreach ($template_folder_paths as $template_folder_path) { // normalize directory separators $template_folder_path = EEH_File::standardise_directory_separators($template_folder_path); // determine if any common base path exists between the two paths $common_base_path = EEH_Template::_find_common_base_path(array($template_folder_path, $template_path_minus_file_name)); if ($common_base_path !== '') { // both paths have a common base, so just tack the filename onto our search path $resolved_path = EEH_File::end_with_directory_separator($template_folder_path) . $file_name; } else { // no common base path, so let's just concatenate $resolved_path = EEH_File::end_with_directory_separator($template_folder_path) . $template; } // build up our template locations array by adding our resolved paths $full_template_paths[] = $resolved_path; } // if $template is an absolute path, then we'll tack it onto the start of our array so that it gets searched first array_unshift($full_template_paths, $template); // path to the directory of the current theme: /wp-content/themes/(current WP theme)/ array_unshift($full_template_paths, get_stylesheet_directory() . DS . $file_name); } // filter final array of full template paths $full_template_paths = apply_filters('FHEE__EEH_Template__locate_template__full_template_paths', $full_template_paths, $file_name); // now loop through our final array of template location paths and check each location foreach ((array) $full_template_paths as $full_template_path) { if (is_readable($full_template_path)) { $template_path = str_replace(array('\\', '/'), DIRECTORY_SEPARATOR, $full_template_path); break; } } } // if we got it and you want to see it... if ($template_path && $load && !$check_if_custom) { if ($return_string) { return EEH_Template::display_template($template_path, $template_args, TRUE); } else { EEH_Template::display_template($template_path, $template_args, FALSE); } } return $check_if_custom && !empty($template_path) ? TRUE : $template_path; }
/** * parse_post_content_on_save * * any time a post is saved, we need to check for any EE shortcodes that may be embedded in the content, * and then track what posts those shortcodes are on, so that we can initialize shortcodes well before the_content() runs. * this allows us to do things like enqueue scripts for shortcodes ONLY on the pages the shortcodes are actually used on * * @access public * @param $post_ID * @param $post * @return void */ public static function parse_post_content_on_save($post_ID, $post) { // default post types $post_types = array('post' => 0, 'page' => 1); // add CPTs $CPTs = EE_Register_CPTs::get_CPTs(); $post_types = array_merge($post_types, $CPTs); // for default or CPT posts... if (isset($post_types[$post->post_type])) { // whether to proceed with update $update_post_shortcodes = FALSE; // post on frontpage ? $page_for_posts = EE_Config::get_page_for_posts(); // critical page shortcodes that we do NOT want added to the Posts page (blog) $critical_shortcodes = EE_Registry::instance()->CFG->core->get_critical_pages_shortcodes_array(); // array of shortcodes indexed by post name EE_Registry::instance()->CFG->core->post_shortcodes = isset(EE_Registry::instance()->CFG->core->post_shortcodes) ? EE_Registry::instance()->CFG->core->post_shortcodes : array(); // empty both arrays EE_Registry::instance()->CFG->core->post_shortcodes[$post->post_name] = array(); // loop thru shortcodes foreach (EE_Registry::instance()->shortcodes as $EES_Shortcode => $shortcode_dir) { // convert to UPPERCASE to get actual shortcode $EES_Shortcode = strtoupper($EES_Shortcode); // is the shortcode in the post_content ? if (strpos($post->post_content, $EES_Shortcode) !== FALSE) { // map shortcode to post names and post IDs EE_Registry::instance()->CFG->core->post_shortcodes[$post->post_name][$EES_Shortcode] = $post_ID; // if the shortcode is NOT one of the critical page shortcodes like ESPRESSO_TXN_PAGE if (!in_array($EES_Shortcode, $critical_shortcodes)) { // check that posts page is already being tracked if (!isset(EE_Registry::instance()->CFG->core->post_shortcodes[$page_for_posts])) { // if not, then ensure that it is properly added EE_Registry::instance()->CFG->core->post_shortcodes[$page_for_posts] = array(); } // add shortcode to "Posts page" tracking EE_Registry::instance()->CFG->core->post_shortcodes[$page_for_posts][$EES_Shortcode] = $post_ID; } $update_post_shortcodes = TRUE; } } if ($update_post_shortcodes) { EE_Registry::instance()->CFG->update_post_shortcodes($page_for_posts); } } }