/** * ensure_folder_exists_and_is_writable * ensures that a folder exists and is writable, will attempt to create folder if it does not exist * Also ensures all the parent folders exist, and if not tries to create them. * Also, if this function creates the folder, adds a .htaccess file and index.html file * @param string $folder * @throws EE_Error if the folder exists and is writeable, but for some reason we * can't write to it * @return bool false if folder isn't writable; true if it exists and is writeable, */ public static function ensure_folder_exists_and_is_writable($folder = '') { if (empty($folder)) { return false; } // remove ending DS $folder = EEH_File::standardise_directory_separators(rtrim($folder, '/\\')); $parent_folder = EEH_File::get_parent_folder($folder); // add DS to folder $folder = EEH_File::end_with_directory_separator($folder); $wp_filesystem = EEH_File::_get_wp_filesystem($folder); if (!$wp_filesystem->is_dir(EEH_File::convert_local_filepath_to_remote_filepath($folder))) { //ok so it doesn't exist. Does its parent? Can we write to it? if (!EEH_File::ensure_folder_exists_and_is_writable($parent_folder)) { return false; } if (!EEH_File::verify_is_writable($parent_folder, 'folder')) { return false; } else { if (!$wp_filesystem->mkdir(EEH_File::convert_local_filepath_to_remote_filepath($folder))) { if (defined('WP_DEBUG') && WP_DEBUG) { $msg = sprintf(__('"%s" could not be created.', 'event_espresso'), $folder); $msg .= EEH_File::_permissions_error_for_unreadable_filepath($folder); throw new EE_Error($msg); } return false; } EEH_File::add_index_file($folder); } } elseif (!EEH_File::verify_is_writable($folder, 'folder')) { return false; } return true; }
/** * ensure_folder_exists_and_is_writable * ensures that a folder exists and is writable, will attempt to create folder if it does not exist * @param string $folder * @throws EE_Error * @return bool */ public static function ensure_folder_exists_and_is_writable($folder = '') { if (empty($folder)) { return FALSE; } // remove ending DS $folder = EEH_File::standardise_directory_separators(rtrim($folder, '/\\')); // determine parent folder $folder_segments = explode(DS, $folder); array_pop($folder_segments); $parent_folder = implode(DS, $folder_segments) . DS; // add DS to folder $folder = EEH_File::end_with_directory_separator($folder); $wp_filesystem = EEH_File::_get_wp_filesystem(); if (!$wp_filesystem->is_dir($folder)) { if (!EEH_File::verify_is_writable($parent_folder, 'folder')) { return FALSE; } else { if (!$wp_filesystem->mkdir($folder)) { if (defined('WP_DEBUG') && WP_DEBUG) { $msg = sprintf(__('"%s" could not be created.', 'event_espresso'), $folder); $msg .= EEH_File::_permissions_error_for_unreadable_filepath($folder); throw new EE_Error($msg); } return FALSE; } } } elseif (!EEH_File::verify_is_writable($folder, 'folder')) { return FALSE; } return TRUE; }
/** * 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; }