@@ -24,666 +24,666 @@ |
||
| 24 | 24 | class EEH_File extends EEH_Base implements EEHI_File |
| 25 | 25 | { |
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * @var string $_credentials_form |
|
| 29 | - */ |
|
| 30 | - private static $_credentials_form; |
|
| 31 | - |
|
| 32 | - protected static $_wp_filesystem_direct; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * @param string|null $filepath the filepath we want to work in. If its in the |
|
| 36 | - * wp uploads directory, we'll want to just use the filesystem directly. |
|
| 37 | - * If not provided, we have to assume its not in the uploads directory |
|
| 38 | - * @throws EE_Error if filesystem credentials are required |
|
| 39 | - * @return WP_Filesystem_Base |
|
| 40 | - */ |
|
| 41 | - private static function _get_wp_filesystem($filepath = null) |
|
| 42 | - { |
|
| 43 | - if (apply_filters( |
|
| 44 | - 'FHEE__EEH_File___get_wp_filesystem__allow_using_filesystem_direct', |
|
| 45 | - $filepath && EEH_File::is_in_uploads_folder($filepath), |
|
| 46 | - $filepath |
|
| 47 | - ) ) { |
|
| 48 | - if (! EEH_File::$_wp_filesystem_direct instanceof WP_Filesystem_Direct) { |
|
| 49 | - require_once(ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php'); |
|
| 50 | - $method = 'direct'; |
|
| 51 | - $wp_filesystem_direct_file = apply_filters('filesystem_method_file', ABSPATH . 'wp-admin/includes/class-wp-filesystem-' . $method . '.php', $method); |
|
| 52 | - // check constants defined, just like in wp-admin/includes/file.php's WP_Filesystem() |
|
| 53 | - if (! defined('FS_CHMOD_DIR')) { |
|
| 54 | - define('FS_CHMOD_DIR', ( fileperms(ABSPATH) & 0777 | 0755 )); |
|
| 55 | - } |
|
| 56 | - if (! defined('FS_CHMOD_FILE')) { |
|
| 57 | - define('FS_CHMOD_FILE', ( fileperms(ABSPATH . 'index.php') & 0777 | 0644 )); |
|
| 58 | - } |
|
| 59 | - require_once($wp_filesystem_direct_file); |
|
| 60 | - EEH_File::$_wp_filesystem_direct = new WP_Filesystem_Direct(array()); |
|
| 61 | - } |
|
| 62 | - return EEH_File::$_wp_filesystem_direct; |
|
| 63 | - } |
|
| 64 | - global $wp_filesystem; |
|
| 65 | - // no filesystem setup ??? |
|
| 66 | - if (! $wp_filesystem instanceof WP_Filesystem_Base) { |
|
| 67 | - // if some eager beaver's just trying to get in there too early... |
|
| 68 | - // let them do it, because we are one of those eager beavers! :P |
|
| 69 | - /** |
|
| 70 | - * more explanations are probably merited. http://codex.wordpress.org/Filesystem_API#Initializing_WP_Filesystem_Base |
|
| 71 | - * says WP_Filesystem should be used after 'wp_loaded', but currently EE's activation process |
|
| 72 | - * is setup to mostly happen on 'init', and refactoring to have it happen on |
|
| 73 | - * 'wp_loaded' is too much work on a BETA milestone. |
|
| 74 | - * So this fix is expected to work if the WP files are owned by the server user, |
|
| 75 | - * but probably not if the user needs to enter their FTP credentials to modify files |
|
| 76 | - * and there may be troubles if the WP files are owned by a different user |
|
| 77 | - * than the server user. But both of these issues should exist in 4.4 and earlier too |
|
| 78 | - */ |
|
| 79 | - if (false && ! did_action('wp_loaded')) { |
|
| 80 | - $msg = __('An attempt to access and/or write to a file on the server could not be completed due to a lack of sufficient credentials.', 'event_espresso'); |
|
| 81 | - if (WP_DEBUG) { |
|
| 82 | - $msg .= '<br />' . __('The WP Filesystem can not be accessed until after the "wp_loaded" hook has run, so it\'s best not to attempt access until the "admin_init" hookpoint.', 'event_espresso'); |
|
| 83 | - } |
|
| 84 | - throw new EE_Error($msg); |
|
| 85 | - } else { |
|
| 86 | - // should be loaded if we are past the wp_loaded hook... |
|
| 87 | - if (! function_exists('WP_Filesystem')) { |
|
| 88 | - require_once(ABSPATH . 'wp-admin/includes/file.php'); |
|
| 89 | - require_once(ABSPATH . 'wp-admin/includes/template.php'); |
|
| 90 | - } |
|
| 91 | - // turn on output buffering so that we can capture the credentials form |
|
| 92 | - ob_start(); |
|
| 93 | - $credentials = request_filesystem_credentials(''); |
|
| 94 | - // store credentials form for the time being |
|
| 95 | - EEH_File::$_credentials_form = ob_get_clean(); |
|
| 96 | - // basically check for direct or previously configured access |
|
| 97 | - if (! WP_Filesystem($credentials)) { |
|
| 98 | - // if credentials do NOT exist |
|
| 99 | - if ($credentials === false) { |
|
| 100 | - add_action('admin_notices', array( 'EEH_File', 'display_request_filesystem_credentials_form' ), 999); |
|
| 101 | - throw new EE_Error(__('An attempt to access and/or write to a file on the server could not be completed due to a lack of sufficient credentials.', 'event_espresso')); |
|
| 102 | - } elseif (is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code()) { |
|
| 103 | - add_action('admin_notices', array( 'EEH_File', 'display_request_filesystem_credentials_form' ), 999); |
|
| 104 | - throw new EE_Error( |
|
| 105 | - sprintf( |
|
| 106 | - __('WP Filesystem Error: $1%s', 'event_espresso'), |
|
| 107 | - $wp_filesystem->errors->get_error_message() |
|
| 108 | - ) |
|
| 109 | - ); |
|
| 110 | - } |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - } |
|
| 114 | - return $wp_filesystem; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * display_request_filesystem_credentials_form |
|
| 119 | - */ |
|
| 120 | - public static function display_request_filesystem_credentials_form() |
|
| 121 | - { |
|
| 122 | - if (! empty(EEH_File::$_credentials_form)) { |
|
| 123 | - echo '<div class="updated espresso-notices-attention"><p>' . EEH_File::$_credentials_form . '</p></div>'; |
|
| 124 | - } |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * verify_filepath_and_permissions |
|
| 131 | - * checks that a file is readable and has sufficient file permissions set to access |
|
| 132 | - * |
|
| 133 | - * @access public |
|
| 134 | - * @param string $full_file_path - full server path to the folder or file |
|
| 135 | - * @param string $file_name - name of file if checking a file |
|
| 136 | - * @param string $file_ext - file extension (ie: "php") if checking a file |
|
| 137 | - * @param string $type_of_file - general type of file (ie: "module"), this is only used to improve error messages |
|
| 138 | - * @throws EE_Error if filesystem credentials are required |
|
| 139 | - * @return bool |
|
| 140 | - */ |
|
| 141 | - public static function verify_filepath_and_permissions($full_file_path = '', $file_name = '', $file_ext = '', $type_of_file = '') |
|
| 142 | - { |
|
| 143 | - // load WP_Filesystem and set file permissions |
|
| 144 | - $wp_filesystem = EEH_File::_get_wp_filesystem($full_file_path); |
|
| 145 | - $full_file_path = EEH_File::standardise_directory_separators($full_file_path); |
|
| 146 | - if (! $wp_filesystem->is_readable(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path))) { |
|
| 147 | - $file_name = ! empty($type_of_file) ? $file_name . ' ' . $type_of_file : $file_name; |
|
| 148 | - $file_name .= ! empty($file_ext) ? ' file' : ' folder'; |
|
| 149 | - $msg = sprintf( |
|
| 150 | - __('The requested %1$s could not be found or is not readable, possibly due to an incorrect filepath, or incorrect file permissions.%2$s', 'event_espresso'), |
|
| 151 | - $file_name, |
|
| 152 | - '<br />' |
|
| 153 | - ); |
|
| 154 | - if (EEH_File::exists($full_file_path)) { |
|
| 155 | - $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_file_path, $type_of_file); |
|
| 156 | - } else { |
|
| 157 | - // no file permissions means the file was not found |
|
| 158 | - $msg .= sprintf( |
|
| 159 | - __('Please ensure the following path is correct: "%s".', 'event_espresso'), |
|
| 160 | - $full_file_path |
|
| 161 | - ); |
|
| 162 | - } |
|
| 163 | - if (defined('WP_DEBUG') && WP_DEBUG) { |
|
| 164 | - throw new EE_Error($msg . '||' . $msg); |
|
| 165 | - } |
|
| 166 | - return false; |
|
| 167 | - } |
|
| 168 | - return true; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * _permissions_error_for_unreadable_filepath - attempts to determine why permissions are set incorrectly for a file or folder |
|
| 175 | - * |
|
| 176 | - * @access private |
|
| 177 | - * @param string $full_file_path - full server path to the folder or file |
|
| 178 | - * @param string $type_of_file - general type of file (ie: "module"), this is only used to improve error messages |
|
| 179 | - * @throws EE_Error if filesystem credentials are required |
|
| 180 | - * @return string |
|
| 181 | - */ |
|
| 182 | - private static function _permissions_error_for_unreadable_filepath($full_file_path = '', $type_of_file = '') |
|
| 183 | - { |
|
| 184 | - // load WP_Filesystem and set file permissions |
|
| 185 | - $wp_filesystem = EEH_File::_get_wp_filesystem($full_file_path); |
|
| 186 | - // check file permissions |
|
| 187 | - $perms = $wp_filesystem->getchmod(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path)); |
|
| 188 | - if ($perms) { |
|
| 189 | - // file permissions exist, but way be set incorrectly |
|
| 190 | - $type_of_file = ! empty($type_of_file) ? $type_of_file . ' ' : ''; |
|
| 191 | - $type_of_file .= ! empty($type_of_file) ? 'file' : 'folder'; |
|
| 192 | - return sprintf( |
|
| 193 | - __('File permissions for the requested %1$s are currently set at "%2$s". The recommended permissions are 644 for files and 755 for folders.', 'event_espresso'), |
|
| 194 | - $type_of_file, |
|
| 195 | - $perms |
|
| 196 | - ); |
|
| 197 | - } else { |
|
| 198 | - // file exists but file permissions could not be read ?!?! |
|
| 199 | - return sprintf( |
|
| 200 | - __('Please ensure that the server and/or PHP configuration allows the current process to access the following file: "%s".', 'event_espresso'), |
|
| 201 | - $full_file_path |
|
| 202 | - ); |
|
| 203 | - } |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * ensure_folder_exists_and_is_writable |
|
| 210 | - * ensures that a folder exists and is writable, will attempt to create folder if it does not exist |
|
| 211 | - * Also ensures all the parent folders exist, and if not tries to create them. |
|
| 212 | - * Also, if this function creates the folder, adds a .htaccess file and index.html file |
|
| 213 | - * @param string $folder |
|
| 214 | - * @throws EE_Error if the folder exists and is writeable, but for some reason we |
|
| 215 | - * can't write to it |
|
| 216 | - * @return bool false if folder isn't writable; true if it exists and is writeable, |
|
| 217 | - */ |
|
| 218 | - public static function ensure_folder_exists_and_is_writable($folder = '') |
|
| 219 | - { |
|
| 220 | - if (empty($folder)) { |
|
| 221 | - return false; |
|
| 222 | - } |
|
| 223 | - // remove ending DS |
|
| 224 | - $folder = EEH_File::standardise_directory_separators(rtrim($folder, '/\\')); |
|
| 225 | - $parent_folder = EEH_File::get_parent_folder($folder); |
|
| 226 | - // add DS to folder |
|
| 227 | - $folder = EEH_File::end_with_directory_separator($folder); |
|
| 228 | - $wp_filesystem = EEH_File::_get_wp_filesystem($folder); |
|
| 229 | - if (! $wp_filesystem->is_dir(EEH_File::convert_local_filepath_to_remote_filepath($folder))) { |
|
| 230 | - // ok so it doesn't exist. Does its parent? Can we write to it? |
|
| 231 | - if (! EEH_File::ensure_folder_exists_and_is_writable($parent_folder)) { |
|
| 232 | - return false; |
|
| 233 | - } |
|
| 234 | - if (! EEH_File::verify_is_writable($parent_folder, 'folder')) { |
|
| 235 | - return false; |
|
| 236 | - } else { |
|
| 237 | - if (! $wp_filesystem->mkdir(EEH_File::convert_local_filepath_to_remote_filepath($folder))) { |
|
| 238 | - if (defined('WP_DEBUG') && WP_DEBUG) { |
|
| 239 | - $msg = sprintf(__('"%s" could not be created.', 'event_espresso'), $folder); |
|
| 240 | - $msg .= EEH_File::_permissions_error_for_unreadable_filepath($folder); |
|
| 241 | - throw new EE_Error($msg); |
|
| 242 | - } |
|
| 243 | - return false; |
|
| 244 | - } |
|
| 245 | - EEH_File::add_index_file($folder); |
|
| 246 | - } |
|
| 247 | - } elseif (! EEH_File::verify_is_writable($folder, 'folder')) { |
|
| 248 | - return false; |
|
| 249 | - } |
|
| 250 | - return true; |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * verify_is_writable - checks if a file or folder is writable |
|
| 257 | - * @param string $full_path - full server path to file or folder |
|
| 258 | - * @param string $file_or_folder - whether checking a file or folder |
|
| 259 | - * @throws EE_Error if filesystem credentials are required |
|
| 260 | - * @return bool |
|
| 261 | - */ |
|
| 262 | - public static function verify_is_writable($full_path = '', $file_or_folder = 'folder') |
|
| 263 | - { |
|
| 264 | - // load WP_Filesystem and set file permissions |
|
| 265 | - $wp_filesystem = EEH_File::_get_wp_filesystem($full_path); |
|
| 266 | - $full_path = EEH_File::standardise_directory_separators($full_path); |
|
| 267 | - if (! $wp_filesystem->is_writable(EEH_File::convert_local_filepath_to_remote_filepath($full_path))) { |
|
| 268 | - if (defined('WP_DEBUG') && WP_DEBUG) { |
|
| 269 | - $msg = sprintf(__('The "%1$s" %2$s is not writable.', 'event_espresso'), $full_path, $file_or_folder); |
|
| 270 | - $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_path); |
|
| 271 | - throw new EE_Error($msg); |
|
| 272 | - } |
|
| 273 | - return false; |
|
| 274 | - } |
|
| 275 | - return true; |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - |
|
| 279 | - |
|
| 280 | - /** |
|
| 281 | - * ensure_file_exists_and_is_writable |
|
| 282 | - * ensures that a file exists and is writable, will attempt to create file if it does not exist. |
|
| 283 | - * Also ensures all the parent folders exist, and if not tries to create them. |
|
| 284 | - * @param string $full_file_path |
|
| 285 | - * @throws EE_Error if filesystem credentials are required |
|
| 286 | - * @return bool |
|
| 287 | - */ |
|
| 288 | - public static function ensure_file_exists_and_is_writable($full_file_path = '') |
|
| 289 | - { |
|
| 290 | - // load WP_Filesystem and set file permissions |
|
| 291 | - $wp_filesystem = EEH_File::_get_wp_filesystem($full_file_path); |
|
| 292 | - $full_file_path = EEH_File::standardise_directory_separators($full_file_path); |
|
| 293 | - $parent_folder = EEH_File::get_parent_folder($full_file_path); |
|
| 294 | - if (! EEH_File::exists($full_file_path)) { |
|
| 295 | - if (! EEH_File::ensure_folder_exists_and_is_writable($parent_folder)) { |
|
| 296 | - return false; |
|
| 297 | - } |
|
| 298 | - if (! $wp_filesystem->touch(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path))) { |
|
| 299 | - if (defined('WP_DEBUG') && WP_DEBUG) { |
|
| 300 | - $msg = sprintf(__('The "%s" file could not be created.', 'event_espresso'), $full_file_path); |
|
| 301 | - $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_file_path); |
|
| 302 | - throw new EE_Error($msg); |
|
| 303 | - } |
|
| 304 | - return false; |
|
| 305 | - } |
|
| 306 | - } |
|
| 307 | - if (! EEH_File::verify_is_writable($full_file_path, 'file')) { |
|
| 308 | - return false; |
|
| 309 | - } |
|
| 310 | - return true; |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * Gets the parent folder. If provided with file, gets the folder that contains it. |
|
| 315 | - * If provided a folder, gets its parent folder. |
|
| 316 | - * @param string $file_or_folder_path |
|
| 317 | - * @return string parent folder, ENDING with a directory separator |
|
| 318 | - */ |
|
| 319 | - public static function get_parent_folder($file_or_folder_path) |
|
| 320 | - { |
|
| 321 | - // find the last DS, ignoring a DS on the very end |
|
| 322 | - // eg if given "/var/something/somewhere/", we want to get "somewhere"'s |
|
| 323 | - // parent folder, "/var/something/" |
|
| 324 | - $ds = strlen($file_or_folder_path) > 1 |
|
| 325 | - ? strrpos($file_or_folder_path, DS, -2) |
|
| 326 | - : strlen($file_or_folder_path); |
|
| 327 | - return substr($file_or_folder_path, 0, $ds + 1); |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - // public static function ensure_folder_exists_recursively( $folder ) { |
|
| 331 | - // |
|
| 332 | - // } |
|
| 333 | - |
|
| 334 | - |
|
| 335 | - |
|
| 336 | - /** |
|
| 337 | - * get_file_contents |
|
| 338 | - * @param string $full_file_path |
|
| 339 | - * @throws EE_Error if filesystem credentials are required |
|
| 340 | - * @return string |
|
| 341 | - */ |
|
| 342 | - public static function get_file_contents($full_file_path = '') |
|
| 343 | - { |
|
| 344 | - $full_file_path = EEH_File::standardise_directory_separators($full_file_path); |
|
| 345 | - if (EEH_File::verify_filepath_and_permissions($full_file_path, EEH_File::get_filename_from_filepath($full_file_path), EEH_File::get_file_extension($full_file_path))) { |
|
| 346 | - // load WP_Filesystem and set file permissions |
|
| 347 | - $wp_filesystem = EEH_File::_get_wp_filesystem($full_file_path); |
|
| 348 | - return $wp_filesystem->get_contents(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path)); |
|
| 349 | - } |
|
| 350 | - return ''; |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - |
|
| 354 | - |
|
| 355 | - /** |
|
| 356 | - * write_file |
|
| 357 | - * @param string $full_file_path |
|
| 358 | - * @param string $file_contents - the content to be written to the file |
|
| 359 | - * @param string $file_type |
|
| 360 | - * @throws EE_Error if filesystem credentials are required |
|
| 361 | - * @return bool |
|
| 362 | - */ |
|
| 363 | - public static function write_to_file($full_file_path = '', $file_contents = '', $file_type = '') |
|
| 364 | - { |
|
| 365 | - $full_file_path = EEH_File::standardise_directory_separators($full_file_path); |
|
| 366 | - $file_type = ! empty($file_type) ? rtrim($file_type, ' ') . ' ' : ''; |
|
| 367 | - $folder = EEH_File::remove_filename_from_filepath($full_file_path); |
|
| 368 | - if (! EEH_File::verify_is_writable($folder, 'folder')) { |
|
| 369 | - if (defined('WP_DEBUG') && WP_DEBUG) { |
|
| 370 | - $msg = sprintf(__('The %1$sfile located at "%2$s" is not writable.', 'event_espresso'), $file_type, $full_file_path); |
|
| 371 | - $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_file_path); |
|
| 372 | - throw new EE_Error($msg); |
|
| 373 | - } |
|
| 374 | - return false; |
|
| 375 | - } |
|
| 376 | - // load WP_Filesystem and set file permissions |
|
| 377 | - $wp_filesystem = EEH_File::_get_wp_filesystem($full_file_path); |
|
| 378 | - // write the file |
|
| 379 | - if (! $wp_filesystem->put_contents(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path), $file_contents)) { |
|
| 380 | - if (defined('WP_DEBUG') && WP_DEBUG) { |
|
| 381 | - $msg = sprintf(__('The %1$sfile located at "%2$s" could not be written to.', 'event_espresso'), $file_type, $full_file_path); |
|
| 382 | - $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_file_path, 'f'); |
|
| 383 | - throw new EE_Error($msg); |
|
| 384 | - } |
|
| 385 | - return false; |
|
| 386 | - } |
|
| 387 | - return true; |
|
| 388 | - } |
|
| 389 | - |
|
| 390 | - /** |
|
| 391 | - * Wrapper for WP_Filesystem_Base::delete |
|
| 392 | - * |
|
| 393 | - * @param string $filepath |
|
| 394 | - * @param boolean $recursive |
|
| 395 | - * @param boolean|string $type 'd' for directory, 'f' for file |
|
| 396 | - * @throws EE_Error if filesystem credentials are required |
|
| 397 | - * @return boolean |
|
| 398 | - */ |
|
| 399 | - public static function delete($filepath, $recursive = false, $type = false) |
|
| 400 | - { |
|
| 401 | - $wp_filesystem = EEH_File::_get_wp_filesystem(); |
|
| 402 | - return $wp_filesystem->delete($filepath, $recursive, $type) ? true : false; |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - |
|
| 406 | - |
|
| 407 | - /** |
|
| 408 | - * exists |
|
| 409 | - * checks if a file exists using the WP filesystem |
|
| 410 | - * @param string $full_file_path |
|
| 411 | - * @throws EE_Error if filesystem credentials are required |
|
| 412 | - * @return bool |
|
| 413 | - */ |
|
| 414 | - public static function exists($full_file_path = '') |
|
| 415 | - { |
|
| 416 | - $wp_filesystem = EEH_File::_get_wp_filesystem($full_file_path); |
|
| 417 | - return $wp_filesystem->exists(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path)) ? true : false; |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - |
|
| 421 | - |
|
| 422 | - /** |
|
| 423 | - * is_readable |
|
| 424 | - * checks if a file is_readable using the WP filesystem |
|
| 425 | - * |
|
| 426 | - * @param string $full_file_path |
|
| 427 | - * @throws EE_Error if filesystem credentials are required |
|
| 428 | - * @return bool |
|
| 429 | - */ |
|
| 430 | - public static function is_readable($full_file_path = '') |
|
| 431 | - { |
|
| 432 | - $wp_filesystem = EEH_File::_get_wp_filesystem($full_file_path); |
|
| 433 | - if ($wp_filesystem->is_readable(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path))) { |
|
| 434 | - return true; |
|
| 435 | - } else { |
|
| 436 | - return false; |
|
| 437 | - } |
|
| 438 | - } |
|
| 439 | - |
|
| 440 | - |
|
| 441 | - |
|
| 442 | - /** |
|
| 443 | - * remove_filename_from_filepath |
|
| 444 | - * given a full path to a file including the filename itself, this removes the filename and returns the path, up to, but NOT including the filename OR slash |
|
| 445 | - * |
|
| 446 | - * @param string $full_file_path |
|
| 447 | - * @return string |
|
| 448 | - */ |
|
| 449 | - public static function remove_filename_from_filepath($full_file_path = '') |
|
| 450 | - { |
|
| 451 | - return pathinfo($full_file_path, PATHINFO_DIRNAME); |
|
| 452 | - } |
|
| 453 | - |
|
| 454 | - |
|
| 455 | - /** |
|
| 456 | - * get_filename_from_filepath. Arguably the same as basename() |
|
| 457 | - * |
|
| 458 | - * @param string $full_file_path |
|
| 459 | - * @return string |
|
| 460 | - */ |
|
| 461 | - public static function get_filename_from_filepath($full_file_path = '') |
|
| 462 | - { |
|
| 463 | - return pathinfo($full_file_path, PATHINFO_BASENAME); |
|
| 464 | - } |
|
| 465 | - |
|
| 466 | - |
|
| 467 | - /** |
|
| 468 | - * get_file_extension |
|
| 469 | - * |
|
| 470 | - * @param string $full_file_path |
|
| 471 | - * @return string |
|
| 472 | - */ |
|
| 473 | - public static function get_file_extension($full_file_path = '') |
|
| 474 | - { |
|
| 475 | - return pathinfo($full_file_path, PATHINFO_EXTENSION); |
|
| 476 | - } |
|
| 477 | - |
|
| 478 | - |
|
| 479 | - |
|
| 480 | - /** |
|
| 481 | - * add_htaccess_deny_from_all so the webserver cannot access this folder |
|
| 482 | - * @param string $folder |
|
| 483 | - * @throws EE_Error if filesystem credentials are required |
|
| 484 | - * @return bool |
|
| 485 | - */ |
|
| 486 | - public static function add_htaccess_deny_from_all($folder = '') |
|
| 487 | - { |
|
| 488 | - $folder = EEH_File::standardise_and_end_with_directory_separator($folder); |
|
| 489 | - if (! EEH_File::exists($folder . '.htaccess')) { |
|
| 490 | - if (! EEH_File::write_to_file($folder . '.htaccess', 'deny from all', '.htaccess')) { |
|
| 491 | - return false; |
|
| 492 | - } |
|
| 493 | - } |
|
| 494 | - |
|
| 495 | - return true; |
|
| 496 | - } |
|
| 497 | - |
|
| 498 | - /** |
|
| 499 | - * Adds an index file to this folder, so folks can't list all the file's contents |
|
| 500 | - * @param string $folder |
|
| 501 | - * @throws EE_Error if filesystem credentials are required |
|
| 502 | - * @return boolean |
|
| 503 | - */ |
|
| 504 | - public static function add_index_file($folder) |
|
| 505 | - { |
|
| 506 | - $folder = EEH_File::standardise_and_end_with_directory_separator($folder); |
|
| 507 | - if (! EEH_File::exists($folder . 'index.php')) { |
|
| 508 | - if (! EEH_File::write_to_file($folder . 'index.php', 'You are not permitted to read from this folder', '.php')) { |
|
| 509 | - return false; |
|
| 510 | - } |
|
| 511 | - } |
|
| 512 | - return true; |
|
| 513 | - } |
|
| 514 | - |
|
| 515 | - |
|
| 516 | - |
|
| 517 | - /** |
|
| 518 | - * Given that the file in $file_path has the normal name, (ie, CLASSNAME.whatever.php), |
|
| 519 | - * extract that classname. |
|
| 520 | - * @param string $file_path |
|
| 521 | - * @return string |
|
| 522 | - */ |
|
| 523 | - public static function get_classname_from_filepath_with_standard_filename($file_path) |
|
| 524 | - { |
|
| 525 | - // extract file from path |
|
| 526 | - $filename = basename($file_path); |
|
| 527 | - // now remove the first period and everything after |
|
| 528 | - $pos_of_first_period = strpos($filename, '.'); |
|
| 529 | - return substr($filename, 0, $pos_of_first_period); |
|
| 530 | - } |
|
| 531 | - |
|
| 532 | - |
|
| 533 | - |
|
| 534 | - /** |
|
| 535 | - * standardise_directory_separators |
|
| 536 | - * convert all directory separators in a file path to whatever is set for DS |
|
| 537 | - * @param string $file_path |
|
| 538 | - * @return string |
|
| 539 | - */ |
|
| 540 | - public static function standardise_directory_separators($file_path) |
|
| 541 | - { |
|
| 542 | - return str_replace(array( '\\', '/' ), DS, $file_path); |
|
| 543 | - } |
|
| 544 | - |
|
| 545 | - |
|
| 546 | - |
|
| 547 | - /** |
|
| 548 | - * end_with_directory_separator |
|
| 549 | - * ensures that file path ends with DS |
|
| 550 | - * @param string $file_path |
|
| 551 | - * @return string |
|
| 552 | - */ |
|
| 553 | - public static function end_with_directory_separator($file_path) |
|
| 554 | - { |
|
| 555 | - return rtrim($file_path, '/\\') . DS; |
|
| 556 | - } |
|
| 557 | - |
|
| 558 | - |
|
| 559 | - |
|
| 560 | - /** |
|
| 561 | - * shorthand for both EEH_FIle::end_with_directory_separator AND EEH_File::standardise_directory_separators |
|
| 562 | - * @param $file_path |
|
| 563 | - * @return string |
|
| 564 | - */ |
|
| 565 | - public static function standardise_and_end_with_directory_separator($file_path) |
|
| 566 | - { |
|
| 567 | - return self::end_with_directory_separator(self::standardise_directory_separators($file_path)); |
|
| 568 | - } |
|
| 569 | - |
|
| 570 | - |
|
| 571 | - |
|
| 572 | - /** |
|
| 573 | - * takes the folder name (with or without trailing slash) and finds the files it in, |
|
| 574 | - * and what the class's name inside of each should be. |
|
| 575 | - * @param array $folder_paths |
|
| 576 | - * @param boolean $index_numerically if TRUE, the returned array will be indexed numerically; |
|
| 577 | - * if FALSE (Default), returned array will be indexed by the filenames minus extensions. |
|
| 578 | - * Set it TRUE if you know there are files in the directory with the same name but different extensions |
|
| 579 | - * @throws EE_Error if filesystem credentials are required |
|
| 580 | - * @return array if $index_numerically == TRUE keys are numeric , |
|
| 581 | - * if $index_numerically == FALSE (Default) keys are what the class names SHOULD be; |
|
| 582 | - * and values are their filepaths |
|
| 583 | - */ |
|
| 584 | - public static function get_contents_of_folders($folder_paths = array(), $index_numerically = false) |
|
| 585 | - { |
|
| 586 | - $class_to_folder_path = array(); |
|
| 587 | - foreach ($folder_paths as $folder_path) { |
|
| 588 | - $folder_path = self::standardise_and_end_with_directory_separator($folder_path); |
|
| 589 | - // load WP_Filesystem and set file permissions |
|
| 590 | - $files_in_folder = glob($folder_path . '*'); |
|
| 591 | - $class_to_folder_path = array(); |
|
| 592 | - if ($files_in_folder) { |
|
| 593 | - foreach ($files_in_folder as $file_path) { |
|
| 594 | - // only add files, not folders |
|
| 595 | - if (! is_dir($file_path)) { |
|
| 596 | - if ($index_numerically) { |
|
| 597 | - $class_to_folder_path[] = $file_path; |
|
| 598 | - } else { |
|
| 599 | - $classname = self::get_classname_from_filepath_with_standard_filename($file_path); |
|
| 600 | - $class_to_folder_path[ $classname ] = $file_path; |
|
| 601 | - } |
|
| 602 | - } |
|
| 603 | - } |
|
| 604 | - } |
|
| 605 | - } |
|
| 606 | - return $class_to_folder_path; |
|
| 607 | - } |
|
| 608 | - |
|
| 609 | - |
|
| 610 | - |
|
| 611 | - /** |
|
| 612 | - * Copies a file. Mostly a wrapper of WP_Filesystem::copy |
|
| 613 | - * @param string $source_file |
|
| 614 | - * @param string $destination_file |
|
| 615 | - * @param boolean $overwrite |
|
| 616 | - * @throws EE_Error if filesystem credentials are required |
|
| 617 | - * @return boolean success |
|
| 618 | - */ |
|
| 619 | - public static function copy($source_file, $destination_file, $overwrite = false) |
|
| 620 | - { |
|
| 621 | - $full_source_path = EEH_File::standardise_directory_separators($source_file); |
|
| 622 | - if (! EEH_File::exists($full_source_path)) { |
|
| 623 | - if (defined('WP_DEBUG') && WP_DEBUG) { |
|
| 624 | - $msg = sprintf(__('The file located at "%2$s" is not readable or doesn\'t exist.', 'event_espresso'), $full_source_path); |
|
| 625 | - $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_source_path); |
|
| 626 | - throw new EE_Error($msg); |
|
| 627 | - } |
|
| 628 | - return false; |
|
| 629 | - } |
|
| 630 | - |
|
| 631 | - $full_dest_path = EEH_File::standardise_directory_separators($destination_file); |
|
| 632 | - $folder = EEH_File::remove_filename_from_filepath($full_dest_path); |
|
| 633 | - EEH_File::ensure_folder_exists_and_is_writable($folder); |
|
| 634 | - if (! EEH_File::verify_is_writable($folder, 'folder')) { |
|
| 635 | - if (defined('WP_DEBUG') && WP_DEBUG) { |
|
| 636 | - $msg = sprintf(__('The file located at "%2$s" is not writable.', 'event_espresso'), $full_dest_path); |
|
| 637 | - $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_dest_path); |
|
| 638 | - throw new EE_Error($msg); |
|
| 639 | - } |
|
| 640 | - return false; |
|
| 641 | - } |
|
| 642 | - |
|
| 643 | - // load WP_Filesystem and set file permissions |
|
| 644 | - $wp_filesystem = EEH_File::_get_wp_filesystem($destination_file); |
|
| 645 | - // write the file |
|
| 646 | - if (! $wp_filesystem->copy( |
|
| 647 | - EEH_File::convert_local_filepath_to_remote_filepath($full_source_path), |
|
| 648 | - EEH_File::convert_local_filepath_to_remote_filepath($full_dest_path), |
|
| 649 | - $overwrite |
|
| 650 | - )) { |
|
| 651 | - if (defined('WP_DEBUG') && WP_DEBUG) { |
|
| 652 | - $msg = sprintf(__('Attempted writing to file %1$s, but could not, probably because of permissions issues', 'event_espresso'), $full_source_path); |
|
| 653 | - $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_source_path, 'f'); |
|
| 654 | - throw new EE_Error($msg); |
|
| 655 | - } |
|
| 656 | - return false; |
|
| 657 | - } |
|
| 658 | - return true; |
|
| 659 | - } |
|
| 660 | - |
|
| 661 | - /** |
|
| 662 | - * Reports whether or not the filepath is in the EE uploads folder or not |
|
| 663 | - * @param string $filepath |
|
| 664 | - * @return boolean |
|
| 665 | - */ |
|
| 666 | - public static function is_in_uploads_folder($filepath) |
|
| 667 | - { |
|
| 668 | - $uploads = wp_upload_dir(); |
|
| 669 | - return strpos($filepath, $uploads['basedir']) === 0 ? true : false; |
|
| 670 | - } |
|
| 671 | - |
|
| 672 | - /** |
|
| 673 | - * Given a "local" filepath (what you probably thought was the only filepath), |
|
| 674 | - * converts it into a "remote" filepath (the filepath the currently-in-use |
|
| 675 | - * $wp_filesystem needs to use access the folder or file). |
|
| 676 | - * See http://wordpress.stackexchange.com/questions/124900/using-wp-filesystem-in-plugins |
|
| 677 | - * @param WP_Filesystem_Base $wp_filesystem we aren't initially sure which one |
|
| 678 | - * is in use, so you need to provide it |
|
| 679 | - * @param string $local_filepath the filepath to the folder/file locally |
|
| 680 | - * @throws EE_Error if filesystem credentials are required |
|
| 681 | - * @return string the remote filepath (eg the filepath the filesystem method, eg |
|
| 682 | - * ftp or ssh, will use to access the folder |
|
| 683 | - */ |
|
| 684 | - public static function convert_local_filepath_to_remote_filepath($local_filepath) |
|
| 685 | - { |
|
| 686 | - $wp_filesystem = EEH_File::_get_wp_filesystem($local_filepath); |
|
| 687 | - return str_replace(WP_CONTENT_DIR . DS, $wp_filesystem->wp_content_dir(), $local_filepath); |
|
| 688 | - } |
|
| 27 | + /** |
|
| 28 | + * @var string $_credentials_form |
|
| 29 | + */ |
|
| 30 | + private static $_credentials_form; |
|
| 31 | + |
|
| 32 | + protected static $_wp_filesystem_direct; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * @param string|null $filepath the filepath we want to work in. If its in the |
|
| 36 | + * wp uploads directory, we'll want to just use the filesystem directly. |
|
| 37 | + * If not provided, we have to assume its not in the uploads directory |
|
| 38 | + * @throws EE_Error if filesystem credentials are required |
|
| 39 | + * @return WP_Filesystem_Base |
|
| 40 | + */ |
|
| 41 | + private static function _get_wp_filesystem($filepath = null) |
|
| 42 | + { |
|
| 43 | + if (apply_filters( |
|
| 44 | + 'FHEE__EEH_File___get_wp_filesystem__allow_using_filesystem_direct', |
|
| 45 | + $filepath && EEH_File::is_in_uploads_folder($filepath), |
|
| 46 | + $filepath |
|
| 47 | + ) ) { |
|
| 48 | + if (! EEH_File::$_wp_filesystem_direct instanceof WP_Filesystem_Direct) { |
|
| 49 | + require_once(ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php'); |
|
| 50 | + $method = 'direct'; |
|
| 51 | + $wp_filesystem_direct_file = apply_filters('filesystem_method_file', ABSPATH . 'wp-admin/includes/class-wp-filesystem-' . $method . '.php', $method); |
|
| 52 | + // check constants defined, just like in wp-admin/includes/file.php's WP_Filesystem() |
|
| 53 | + if (! defined('FS_CHMOD_DIR')) { |
|
| 54 | + define('FS_CHMOD_DIR', ( fileperms(ABSPATH) & 0777 | 0755 )); |
|
| 55 | + } |
|
| 56 | + if (! defined('FS_CHMOD_FILE')) { |
|
| 57 | + define('FS_CHMOD_FILE', ( fileperms(ABSPATH . 'index.php') & 0777 | 0644 )); |
|
| 58 | + } |
|
| 59 | + require_once($wp_filesystem_direct_file); |
|
| 60 | + EEH_File::$_wp_filesystem_direct = new WP_Filesystem_Direct(array()); |
|
| 61 | + } |
|
| 62 | + return EEH_File::$_wp_filesystem_direct; |
|
| 63 | + } |
|
| 64 | + global $wp_filesystem; |
|
| 65 | + // no filesystem setup ??? |
|
| 66 | + if (! $wp_filesystem instanceof WP_Filesystem_Base) { |
|
| 67 | + // if some eager beaver's just trying to get in there too early... |
|
| 68 | + // let them do it, because we are one of those eager beavers! :P |
|
| 69 | + /** |
|
| 70 | + * more explanations are probably merited. http://codex.wordpress.org/Filesystem_API#Initializing_WP_Filesystem_Base |
|
| 71 | + * says WP_Filesystem should be used after 'wp_loaded', but currently EE's activation process |
|
| 72 | + * is setup to mostly happen on 'init', and refactoring to have it happen on |
|
| 73 | + * 'wp_loaded' is too much work on a BETA milestone. |
|
| 74 | + * So this fix is expected to work if the WP files are owned by the server user, |
|
| 75 | + * but probably not if the user needs to enter their FTP credentials to modify files |
|
| 76 | + * and there may be troubles if the WP files are owned by a different user |
|
| 77 | + * than the server user. But both of these issues should exist in 4.4 and earlier too |
|
| 78 | + */ |
|
| 79 | + if (false && ! did_action('wp_loaded')) { |
|
| 80 | + $msg = __('An attempt to access and/or write to a file on the server could not be completed due to a lack of sufficient credentials.', 'event_espresso'); |
|
| 81 | + if (WP_DEBUG) { |
|
| 82 | + $msg .= '<br />' . __('The WP Filesystem can not be accessed until after the "wp_loaded" hook has run, so it\'s best not to attempt access until the "admin_init" hookpoint.', 'event_espresso'); |
|
| 83 | + } |
|
| 84 | + throw new EE_Error($msg); |
|
| 85 | + } else { |
|
| 86 | + // should be loaded if we are past the wp_loaded hook... |
|
| 87 | + if (! function_exists('WP_Filesystem')) { |
|
| 88 | + require_once(ABSPATH . 'wp-admin/includes/file.php'); |
|
| 89 | + require_once(ABSPATH . 'wp-admin/includes/template.php'); |
|
| 90 | + } |
|
| 91 | + // turn on output buffering so that we can capture the credentials form |
|
| 92 | + ob_start(); |
|
| 93 | + $credentials = request_filesystem_credentials(''); |
|
| 94 | + // store credentials form for the time being |
|
| 95 | + EEH_File::$_credentials_form = ob_get_clean(); |
|
| 96 | + // basically check for direct or previously configured access |
|
| 97 | + if (! WP_Filesystem($credentials)) { |
|
| 98 | + // if credentials do NOT exist |
|
| 99 | + if ($credentials === false) { |
|
| 100 | + add_action('admin_notices', array( 'EEH_File', 'display_request_filesystem_credentials_form' ), 999); |
|
| 101 | + throw new EE_Error(__('An attempt to access and/or write to a file on the server could not be completed due to a lack of sufficient credentials.', 'event_espresso')); |
|
| 102 | + } elseif (is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code()) { |
|
| 103 | + add_action('admin_notices', array( 'EEH_File', 'display_request_filesystem_credentials_form' ), 999); |
|
| 104 | + throw new EE_Error( |
|
| 105 | + sprintf( |
|
| 106 | + __('WP Filesystem Error: $1%s', 'event_espresso'), |
|
| 107 | + $wp_filesystem->errors->get_error_message() |
|
| 108 | + ) |
|
| 109 | + ); |
|
| 110 | + } |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + } |
|
| 114 | + return $wp_filesystem; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * display_request_filesystem_credentials_form |
|
| 119 | + */ |
|
| 120 | + public static function display_request_filesystem_credentials_form() |
|
| 121 | + { |
|
| 122 | + if (! empty(EEH_File::$_credentials_form)) { |
|
| 123 | + echo '<div class="updated espresso-notices-attention"><p>' . EEH_File::$_credentials_form . '</p></div>'; |
|
| 124 | + } |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * verify_filepath_and_permissions |
|
| 131 | + * checks that a file is readable and has sufficient file permissions set to access |
|
| 132 | + * |
|
| 133 | + * @access public |
|
| 134 | + * @param string $full_file_path - full server path to the folder or file |
|
| 135 | + * @param string $file_name - name of file if checking a file |
|
| 136 | + * @param string $file_ext - file extension (ie: "php") if checking a file |
|
| 137 | + * @param string $type_of_file - general type of file (ie: "module"), this is only used to improve error messages |
|
| 138 | + * @throws EE_Error if filesystem credentials are required |
|
| 139 | + * @return bool |
|
| 140 | + */ |
|
| 141 | + public static function verify_filepath_and_permissions($full_file_path = '', $file_name = '', $file_ext = '', $type_of_file = '') |
|
| 142 | + { |
|
| 143 | + // load WP_Filesystem and set file permissions |
|
| 144 | + $wp_filesystem = EEH_File::_get_wp_filesystem($full_file_path); |
|
| 145 | + $full_file_path = EEH_File::standardise_directory_separators($full_file_path); |
|
| 146 | + if (! $wp_filesystem->is_readable(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path))) { |
|
| 147 | + $file_name = ! empty($type_of_file) ? $file_name . ' ' . $type_of_file : $file_name; |
|
| 148 | + $file_name .= ! empty($file_ext) ? ' file' : ' folder'; |
|
| 149 | + $msg = sprintf( |
|
| 150 | + __('The requested %1$s could not be found or is not readable, possibly due to an incorrect filepath, or incorrect file permissions.%2$s', 'event_espresso'), |
|
| 151 | + $file_name, |
|
| 152 | + '<br />' |
|
| 153 | + ); |
|
| 154 | + if (EEH_File::exists($full_file_path)) { |
|
| 155 | + $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_file_path, $type_of_file); |
|
| 156 | + } else { |
|
| 157 | + // no file permissions means the file was not found |
|
| 158 | + $msg .= sprintf( |
|
| 159 | + __('Please ensure the following path is correct: "%s".', 'event_espresso'), |
|
| 160 | + $full_file_path |
|
| 161 | + ); |
|
| 162 | + } |
|
| 163 | + if (defined('WP_DEBUG') && WP_DEBUG) { |
|
| 164 | + throw new EE_Error($msg . '||' . $msg); |
|
| 165 | + } |
|
| 166 | + return false; |
|
| 167 | + } |
|
| 168 | + return true; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * _permissions_error_for_unreadable_filepath - attempts to determine why permissions are set incorrectly for a file or folder |
|
| 175 | + * |
|
| 176 | + * @access private |
|
| 177 | + * @param string $full_file_path - full server path to the folder or file |
|
| 178 | + * @param string $type_of_file - general type of file (ie: "module"), this is only used to improve error messages |
|
| 179 | + * @throws EE_Error if filesystem credentials are required |
|
| 180 | + * @return string |
|
| 181 | + */ |
|
| 182 | + private static function _permissions_error_for_unreadable_filepath($full_file_path = '', $type_of_file = '') |
|
| 183 | + { |
|
| 184 | + // load WP_Filesystem and set file permissions |
|
| 185 | + $wp_filesystem = EEH_File::_get_wp_filesystem($full_file_path); |
|
| 186 | + // check file permissions |
|
| 187 | + $perms = $wp_filesystem->getchmod(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path)); |
|
| 188 | + if ($perms) { |
|
| 189 | + // file permissions exist, but way be set incorrectly |
|
| 190 | + $type_of_file = ! empty($type_of_file) ? $type_of_file . ' ' : ''; |
|
| 191 | + $type_of_file .= ! empty($type_of_file) ? 'file' : 'folder'; |
|
| 192 | + return sprintf( |
|
| 193 | + __('File permissions for the requested %1$s are currently set at "%2$s". The recommended permissions are 644 for files and 755 for folders.', 'event_espresso'), |
|
| 194 | + $type_of_file, |
|
| 195 | + $perms |
|
| 196 | + ); |
|
| 197 | + } else { |
|
| 198 | + // file exists but file permissions could not be read ?!?! |
|
| 199 | + return sprintf( |
|
| 200 | + __('Please ensure that the server and/or PHP configuration allows the current process to access the following file: "%s".', 'event_espresso'), |
|
| 201 | + $full_file_path |
|
| 202 | + ); |
|
| 203 | + } |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * ensure_folder_exists_and_is_writable |
|
| 210 | + * ensures that a folder exists and is writable, will attempt to create folder if it does not exist |
|
| 211 | + * Also ensures all the parent folders exist, and if not tries to create them. |
|
| 212 | + * Also, if this function creates the folder, adds a .htaccess file and index.html file |
|
| 213 | + * @param string $folder |
|
| 214 | + * @throws EE_Error if the folder exists and is writeable, but for some reason we |
|
| 215 | + * can't write to it |
|
| 216 | + * @return bool false if folder isn't writable; true if it exists and is writeable, |
|
| 217 | + */ |
|
| 218 | + public static function ensure_folder_exists_and_is_writable($folder = '') |
|
| 219 | + { |
|
| 220 | + if (empty($folder)) { |
|
| 221 | + return false; |
|
| 222 | + } |
|
| 223 | + // remove ending DS |
|
| 224 | + $folder = EEH_File::standardise_directory_separators(rtrim($folder, '/\\')); |
|
| 225 | + $parent_folder = EEH_File::get_parent_folder($folder); |
|
| 226 | + // add DS to folder |
|
| 227 | + $folder = EEH_File::end_with_directory_separator($folder); |
|
| 228 | + $wp_filesystem = EEH_File::_get_wp_filesystem($folder); |
|
| 229 | + if (! $wp_filesystem->is_dir(EEH_File::convert_local_filepath_to_remote_filepath($folder))) { |
|
| 230 | + // ok so it doesn't exist. Does its parent? Can we write to it? |
|
| 231 | + if (! EEH_File::ensure_folder_exists_and_is_writable($parent_folder)) { |
|
| 232 | + return false; |
|
| 233 | + } |
|
| 234 | + if (! EEH_File::verify_is_writable($parent_folder, 'folder')) { |
|
| 235 | + return false; |
|
| 236 | + } else { |
|
| 237 | + if (! $wp_filesystem->mkdir(EEH_File::convert_local_filepath_to_remote_filepath($folder))) { |
|
| 238 | + if (defined('WP_DEBUG') && WP_DEBUG) { |
|
| 239 | + $msg = sprintf(__('"%s" could not be created.', 'event_espresso'), $folder); |
|
| 240 | + $msg .= EEH_File::_permissions_error_for_unreadable_filepath($folder); |
|
| 241 | + throw new EE_Error($msg); |
|
| 242 | + } |
|
| 243 | + return false; |
|
| 244 | + } |
|
| 245 | + EEH_File::add_index_file($folder); |
|
| 246 | + } |
|
| 247 | + } elseif (! EEH_File::verify_is_writable($folder, 'folder')) { |
|
| 248 | + return false; |
|
| 249 | + } |
|
| 250 | + return true; |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * verify_is_writable - checks if a file or folder is writable |
|
| 257 | + * @param string $full_path - full server path to file or folder |
|
| 258 | + * @param string $file_or_folder - whether checking a file or folder |
|
| 259 | + * @throws EE_Error if filesystem credentials are required |
|
| 260 | + * @return bool |
|
| 261 | + */ |
|
| 262 | + public static function verify_is_writable($full_path = '', $file_or_folder = 'folder') |
|
| 263 | + { |
|
| 264 | + // load WP_Filesystem and set file permissions |
|
| 265 | + $wp_filesystem = EEH_File::_get_wp_filesystem($full_path); |
|
| 266 | + $full_path = EEH_File::standardise_directory_separators($full_path); |
|
| 267 | + if (! $wp_filesystem->is_writable(EEH_File::convert_local_filepath_to_remote_filepath($full_path))) { |
|
| 268 | + if (defined('WP_DEBUG') && WP_DEBUG) { |
|
| 269 | + $msg = sprintf(__('The "%1$s" %2$s is not writable.', 'event_espresso'), $full_path, $file_or_folder); |
|
| 270 | + $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_path); |
|
| 271 | + throw new EE_Error($msg); |
|
| 272 | + } |
|
| 273 | + return false; |
|
| 274 | + } |
|
| 275 | + return true; |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + |
|
| 279 | + |
|
| 280 | + /** |
|
| 281 | + * ensure_file_exists_and_is_writable |
|
| 282 | + * ensures that a file exists and is writable, will attempt to create file if it does not exist. |
|
| 283 | + * Also ensures all the parent folders exist, and if not tries to create them. |
|
| 284 | + * @param string $full_file_path |
|
| 285 | + * @throws EE_Error if filesystem credentials are required |
|
| 286 | + * @return bool |
|
| 287 | + */ |
|
| 288 | + public static function ensure_file_exists_and_is_writable($full_file_path = '') |
|
| 289 | + { |
|
| 290 | + // load WP_Filesystem and set file permissions |
|
| 291 | + $wp_filesystem = EEH_File::_get_wp_filesystem($full_file_path); |
|
| 292 | + $full_file_path = EEH_File::standardise_directory_separators($full_file_path); |
|
| 293 | + $parent_folder = EEH_File::get_parent_folder($full_file_path); |
|
| 294 | + if (! EEH_File::exists($full_file_path)) { |
|
| 295 | + if (! EEH_File::ensure_folder_exists_and_is_writable($parent_folder)) { |
|
| 296 | + return false; |
|
| 297 | + } |
|
| 298 | + if (! $wp_filesystem->touch(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path))) { |
|
| 299 | + if (defined('WP_DEBUG') && WP_DEBUG) { |
|
| 300 | + $msg = sprintf(__('The "%s" file could not be created.', 'event_espresso'), $full_file_path); |
|
| 301 | + $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_file_path); |
|
| 302 | + throw new EE_Error($msg); |
|
| 303 | + } |
|
| 304 | + return false; |
|
| 305 | + } |
|
| 306 | + } |
|
| 307 | + if (! EEH_File::verify_is_writable($full_file_path, 'file')) { |
|
| 308 | + return false; |
|
| 309 | + } |
|
| 310 | + return true; |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * Gets the parent folder. If provided with file, gets the folder that contains it. |
|
| 315 | + * If provided a folder, gets its parent folder. |
|
| 316 | + * @param string $file_or_folder_path |
|
| 317 | + * @return string parent folder, ENDING with a directory separator |
|
| 318 | + */ |
|
| 319 | + public static function get_parent_folder($file_or_folder_path) |
|
| 320 | + { |
|
| 321 | + // find the last DS, ignoring a DS on the very end |
|
| 322 | + // eg if given "/var/something/somewhere/", we want to get "somewhere"'s |
|
| 323 | + // parent folder, "/var/something/" |
|
| 324 | + $ds = strlen($file_or_folder_path) > 1 |
|
| 325 | + ? strrpos($file_or_folder_path, DS, -2) |
|
| 326 | + : strlen($file_or_folder_path); |
|
| 327 | + return substr($file_or_folder_path, 0, $ds + 1); |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + // public static function ensure_folder_exists_recursively( $folder ) { |
|
| 331 | + // |
|
| 332 | + // } |
|
| 333 | + |
|
| 334 | + |
|
| 335 | + |
|
| 336 | + /** |
|
| 337 | + * get_file_contents |
|
| 338 | + * @param string $full_file_path |
|
| 339 | + * @throws EE_Error if filesystem credentials are required |
|
| 340 | + * @return string |
|
| 341 | + */ |
|
| 342 | + public static function get_file_contents($full_file_path = '') |
|
| 343 | + { |
|
| 344 | + $full_file_path = EEH_File::standardise_directory_separators($full_file_path); |
|
| 345 | + if (EEH_File::verify_filepath_and_permissions($full_file_path, EEH_File::get_filename_from_filepath($full_file_path), EEH_File::get_file_extension($full_file_path))) { |
|
| 346 | + // load WP_Filesystem and set file permissions |
|
| 347 | + $wp_filesystem = EEH_File::_get_wp_filesystem($full_file_path); |
|
| 348 | + return $wp_filesystem->get_contents(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path)); |
|
| 349 | + } |
|
| 350 | + return ''; |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + |
|
| 354 | + |
|
| 355 | + /** |
|
| 356 | + * write_file |
|
| 357 | + * @param string $full_file_path |
|
| 358 | + * @param string $file_contents - the content to be written to the file |
|
| 359 | + * @param string $file_type |
|
| 360 | + * @throws EE_Error if filesystem credentials are required |
|
| 361 | + * @return bool |
|
| 362 | + */ |
|
| 363 | + public static function write_to_file($full_file_path = '', $file_contents = '', $file_type = '') |
|
| 364 | + { |
|
| 365 | + $full_file_path = EEH_File::standardise_directory_separators($full_file_path); |
|
| 366 | + $file_type = ! empty($file_type) ? rtrim($file_type, ' ') . ' ' : ''; |
|
| 367 | + $folder = EEH_File::remove_filename_from_filepath($full_file_path); |
|
| 368 | + if (! EEH_File::verify_is_writable($folder, 'folder')) { |
|
| 369 | + if (defined('WP_DEBUG') && WP_DEBUG) { |
|
| 370 | + $msg = sprintf(__('The %1$sfile located at "%2$s" is not writable.', 'event_espresso'), $file_type, $full_file_path); |
|
| 371 | + $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_file_path); |
|
| 372 | + throw new EE_Error($msg); |
|
| 373 | + } |
|
| 374 | + return false; |
|
| 375 | + } |
|
| 376 | + // load WP_Filesystem and set file permissions |
|
| 377 | + $wp_filesystem = EEH_File::_get_wp_filesystem($full_file_path); |
|
| 378 | + // write the file |
|
| 379 | + if (! $wp_filesystem->put_contents(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path), $file_contents)) { |
|
| 380 | + if (defined('WP_DEBUG') && WP_DEBUG) { |
|
| 381 | + $msg = sprintf(__('The %1$sfile located at "%2$s" could not be written to.', 'event_espresso'), $file_type, $full_file_path); |
|
| 382 | + $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_file_path, 'f'); |
|
| 383 | + throw new EE_Error($msg); |
|
| 384 | + } |
|
| 385 | + return false; |
|
| 386 | + } |
|
| 387 | + return true; |
|
| 388 | + } |
|
| 389 | + |
|
| 390 | + /** |
|
| 391 | + * Wrapper for WP_Filesystem_Base::delete |
|
| 392 | + * |
|
| 393 | + * @param string $filepath |
|
| 394 | + * @param boolean $recursive |
|
| 395 | + * @param boolean|string $type 'd' for directory, 'f' for file |
|
| 396 | + * @throws EE_Error if filesystem credentials are required |
|
| 397 | + * @return boolean |
|
| 398 | + */ |
|
| 399 | + public static function delete($filepath, $recursive = false, $type = false) |
|
| 400 | + { |
|
| 401 | + $wp_filesystem = EEH_File::_get_wp_filesystem(); |
|
| 402 | + return $wp_filesystem->delete($filepath, $recursive, $type) ? true : false; |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + |
|
| 406 | + |
|
| 407 | + /** |
|
| 408 | + * exists |
|
| 409 | + * checks if a file exists using the WP filesystem |
|
| 410 | + * @param string $full_file_path |
|
| 411 | + * @throws EE_Error if filesystem credentials are required |
|
| 412 | + * @return bool |
|
| 413 | + */ |
|
| 414 | + public static function exists($full_file_path = '') |
|
| 415 | + { |
|
| 416 | + $wp_filesystem = EEH_File::_get_wp_filesystem($full_file_path); |
|
| 417 | + return $wp_filesystem->exists(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path)) ? true : false; |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + |
|
| 421 | + |
|
| 422 | + /** |
|
| 423 | + * is_readable |
|
| 424 | + * checks if a file is_readable using the WP filesystem |
|
| 425 | + * |
|
| 426 | + * @param string $full_file_path |
|
| 427 | + * @throws EE_Error if filesystem credentials are required |
|
| 428 | + * @return bool |
|
| 429 | + */ |
|
| 430 | + public static function is_readable($full_file_path = '') |
|
| 431 | + { |
|
| 432 | + $wp_filesystem = EEH_File::_get_wp_filesystem($full_file_path); |
|
| 433 | + if ($wp_filesystem->is_readable(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path))) { |
|
| 434 | + return true; |
|
| 435 | + } else { |
|
| 436 | + return false; |
|
| 437 | + } |
|
| 438 | + } |
|
| 439 | + |
|
| 440 | + |
|
| 441 | + |
|
| 442 | + /** |
|
| 443 | + * remove_filename_from_filepath |
|
| 444 | + * given a full path to a file including the filename itself, this removes the filename and returns the path, up to, but NOT including the filename OR slash |
|
| 445 | + * |
|
| 446 | + * @param string $full_file_path |
|
| 447 | + * @return string |
|
| 448 | + */ |
|
| 449 | + public static function remove_filename_from_filepath($full_file_path = '') |
|
| 450 | + { |
|
| 451 | + return pathinfo($full_file_path, PATHINFO_DIRNAME); |
|
| 452 | + } |
|
| 453 | + |
|
| 454 | + |
|
| 455 | + /** |
|
| 456 | + * get_filename_from_filepath. Arguably the same as basename() |
|
| 457 | + * |
|
| 458 | + * @param string $full_file_path |
|
| 459 | + * @return string |
|
| 460 | + */ |
|
| 461 | + public static function get_filename_from_filepath($full_file_path = '') |
|
| 462 | + { |
|
| 463 | + return pathinfo($full_file_path, PATHINFO_BASENAME); |
|
| 464 | + } |
|
| 465 | + |
|
| 466 | + |
|
| 467 | + /** |
|
| 468 | + * get_file_extension |
|
| 469 | + * |
|
| 470 | + * @param string $full_file_path |
|
| 471 | + * @return string |
|
| 472 | + */ |
|
| 473 | + public static function get_file_extension($full_file_path = '') |
|
| 474 | + { |
|
| 475 | + return pathinfo($full_file_path, PATHINFO_EXTENSION); |
|
| 476 | + } |
|
| 477 | + |
|
| 478 | + |
|
| 479 | + |
|
| 480 | + /** |
|
| 481 | + * add_htaccess_deny_from_all so the webserver cannot access this folder |
|
| 482 | + * @param string $folder |
|
| 483 | + * @throws EE_Error if filesystem credentials are required |
|
| 484 | + * @return bool |
|
| 485 | + */ |
|
| 486 | + public static function add_htaccess_deny_from_all($folder = '') |
|
| 487 | + { |
|
| 488 | + $folder = EEH_File::standardise_and_end_with_directory_separator($folder); |
|
| 489 | + if (! EEH_File::exists($folder . '.htaccess')) { |
|
| 490 | + if (! EEH_File::write_to_file($folder . '.htaccess', 'deny from all', '.htaccess')) { |
|
| 491 | + return false; |
|
| 492 | + } |
|
| 493 | + } |
|
| 494 | + |
|
| 495 | + return true; |
|
| 496 | + } |
|
| 497 | + |
|
| 498 | + /** |
|
| 499 | + * Adds an index file to this folder, so folks can't list all the file's contents |
|
| 500 | + * @param string $folder |
|
| 501 | + * @throws EE_Error if filesystem credentials are required |
|
| 502 | + * @return boolean |
|
| 503 | + */ |
|
| 504 | + public static function add_index_file($folder) |
|
| 505 | + { |
|
| 506 | + $folder = EEH_File::standardise_and_end_with_directory_separator($folder); |
|
| 507 | + if (! EEH_File::exists($folder . 'index.php')) { |
|
| 508 | + if (! EEH_File::write_to_file($folder . 'index.php', 'You are not permitted to read from this folder', '.php')) { |
|
| 509 | + return false; |
|
| 510 | + } |
|
| 511 | + } |
|
| 512 | + return true; |
|
| 513 | + } |
|
| 514 | + |
|
| 515 | + |
|
| 516 | + |
|
| 517 | + /** |
|
| 518 | + * Given that the file in $file_path has the normal name, (ie, CLASSNAME.whatever.php), |
|
| 519 | + * extract that classname. |
|
| 520 | + * @param string $file_path |
|
| 521 | + * @return string |
|
| 522 | + */ |
|
| 523 | + public static function get_classname_from_filepath_with_standard_filename($file_path) |
|
| 524 | + { |
|
| 525 | + // extract file from path |
|
| 526 | + $filename = basename($file_path); |
|
| 527 | + // now remove the first period and everything after |
|
| 528 | + $pos_of_first_period = strpos($filename, '.'); |
|
| 529 | + return substr($filename, 0, $pos_of_first_period); |
|
| 530 | + } |
|
| 531 | + |
|
| 532 | + |
|
| 533 | + |
|
| 534 | + /** |
|
| 535 | + * standardise_directory_separators |
|
| 536 | + * convert all directory separators in a file path to whatever is set for DS |
|
| 537 | + * @param string $file_path |
|
| 538 | + * @return string |
|
| 539 | + */ |
|
| 540 | + public static function standardise_directory_separators($file_path) |
|
| 541 | + { |
|
| 542 | + return str_replace(array( '\\', '/' ), DS, $file_path); |
|
| 543 | + } |
|
| 544 | + |
|
| 545 | + |
|
| 546 | + |
|
| 547 | + /** |
|
| 548 | + * end_with_directory_separator |
|
| 549 | + * ensures that file path ends with DS |
|
| 550 | + * @param string $file_path |
|
| 551 | + * @return string |
|
| 552 | + */ |
|
| 553 | + public static function end_with_directory_separator($file_path) |
|
| 554 | + { |
|
| 555 | + return rtrim($file_path, '/\\') . DS; |
|
| 556 | + } |
|
| 557 | + |
|
| 558 | + |
|
| 559 | + |
|
| 560 | + /** |
|
| 561 | + * shorthand for both EEH_FIle::end_with_directory_separator AND EEH_File::standardise_directory_separators |
|
| 562 | + * @param $file_path |
|
| 563 | + * @return string |
|
| 564 | + */ |
|
| 565 | + public static function standardise_and_end_with_directory_separator($file_path) |
|
| 566 | + { |
|
| 567 | + return self::end_with_directory_separator(self::standardise_directory_separators($file_path)); |
|
| 568 | + } |
|
| 569 | + |
|
| 570 | + |
|
| 571 | + |
|
| 572 | + /** |
|
| 573 | + * takes the folder name (with or without trailing slash) and finds the files it in, |
|
| 574 | + * and what the class's name inside of each should be. |
|
| 575 | + * @param array $folder_paths |
|
| 576 | + * @param boolean $index_numerically if TRUE, the returned array will be indexed numerically; |
|
| 577 | + * if FALSE (Default), returned array will be indexed by the filenames minus extensions. |
|
| 578 | + * Set it TRUE if you know there are files in the directory with the same name but different extensions |
|
| 579 | + * @throws EE_Error if filesystem credentials are required |
|
| 580 | + * @return array if $index_numerically == TRUE keys are numeric , |
|
| 581 | + * if $index_numerically == FALSE (Default) keys are what the class names SHOULD be; |
|
| 582 | + * and values are their filepaths |
|
| 583 | + */ |
|
| 584 | + public static function get_contents_of_folders($folder_paths = array(), $index_numerically = false) |
|
| 585 | + { |
|
| 586 | + $class_to_folder_path = array(); |
|
| 587 | + foreach ($folder_paths as $folder_path) { |
|
| 588 | + $folder_path = self::standardise_and_end_with_directory_separator($folder_path); |
|
| 589 | + // load WP_Filesystem and set file permissions |
|
| 590 | + $files_in_folder = glob($folder_path . '*'); |
|
| 591 | + $class_to_folder_path = array(); |
|
| 592 | + if ($files_in_folder) { |
|
| 593 | + foreach ($files_in_folder as $file_path) { |
|
| 594 | + // only add files, not folders |
|
| 595 | + if (! is_dir($file_path)) { |
|
| 596 | + if ($index_numerically) { |
|
| 597 | + $class_to_folder_path[] = $file_path; |
|
| 598 | + } else { |
|
| 599 | + $classname = self::get_classname_from_filepath_with_standard_filename($file_path); |
|
| 600 | + $class_to_folder_path[ $classname ] = $file_path; |
|
| 601 | + } |
|
| 602 | + } |
|
| 603 | + } |
|
| 604 | + } |
|
| 605 | + } |
|
| 606 | + return $class_to_folder_path; |
|
| 607 | + } |
|
| 608 | + |
|
| 609 | + |
|
| 610 | + |
|
| 611 | + /** |
|
| 612 | + * Copies a file. Mostly a wrapper of WP_Filesystem::copy |
|
| 613 | + * @param string $source_file |
|
| 614 | + * @param string $destination_file |
|
| 615 | + * @param boolean $overwrite |
|
| 616 | + * @throws EE_Error if filesystem credentials are required |
|
| 617 | + * @return boolean success |
|
| 618 | + */ |
|
| 619 | + public static function copy($source_file, $destination_file, $overwrite = false) |
|
| 620 | + { |
|
| 621 | + $full_source_path = EEH_File::standardise_directory_separators($source_file); |
|
| 622 | + if (! EEH_File::exists($full_source_path)) { |
|
| 623 | + if (defined('WP_DEBUG') && WP_DEBUG) { |
|
| 624 | + $msg = sprintf(__('The file located at "%2$s" is not readable or doesn\'t exist.', 'event_espresso'), $full_source_path); |
|
| 625 | + $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_source_path); |
|
| 626 | + throw new EE_Error($msg); |
|
| 627 | + } |
|
| 628 | + return false; |
|
| 629 | + } |
|
| 630 | + |
|
| 631 | + $full_dest_path = EEH_File::standardise_directory_separators($destination_file); |
|
| 632 | + $folder = EEH_File::remove_filename_from_filepath($full_dest_path); |
|
| 633 | + EEH_File::ensure_folder_exists_and_is_writable($folder); |
|
| 634 | + if (! EEH_File::verify_is_writable($folder, 'folder')) { |
|
| 635 | + if (defined('WP_DEBUG') && WP_DEBUG) { |
|
| 636 | + $msg = sprintf(__('The file located at "%2$s" is not writable.', 'event_espresso'), $full_dest_path); |
|
| 637 | + $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_dest_path); |
|
| 638 | + throw new EE_Error($msg); |
|
| 639 | + } |
|
| 640 | + return false; |
|
| 641 | + } |
|
| 642 | + |
|
| 643 | + // load WP_Filesystem and set file permissions |
|
| 644 | + $wp_filesystem = EEH_File::_get_wp_filesystem($destination_file); |
|
| 645 | + // write the file |
|
| 646 | + if (! $wp_filesystem->copy( |
|
| 647 | + EEH_File::convert_local_filepath_to_remote_filepath($full_source_path), |
|
| 648 | + EEH_File::convert_local_filepath_to_remote_filepath($full_dest_path), |
|
| 649 | + $overwrite |
|
| 650 | + )) { |
|
| 651 | + if (defined('WP_DEBUG') && WP_DEBUG) { |
|
| 652 | + $msg = sprintf(__('Attempted writing to file %1$s, but could not, probably because of permissions issues', 'event_espresso'), $full_source_path); |
|
| 653 | + $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_source_path, 'f'); |
|
| 654 | + throw new EE_Error($msg); |
|
| 655 | + } |
|
| 656 | + return false; |
|
| 657 | + } |
|
| 658 | + return true; |
|
| 659 | + } |
|
| 660 | + |
|
| 661 | + /** |
|
| 662 | + * Reports whether or not the filepath is in the EE uploads folder or not |
|
| 663 | + * @param string $filepath |
|
| 664 | + * @return boolean |
|
| 665 | + */ |
|
| 666 | + public static function is_in_uploads_folder($filepath) |
|
| 667 | + { |
|
| 668 | + $uploads = wp_upload_dir(); |
|
| 669 | + return strpos($filepath, $uploads['basedir']) === 0 ? true : false; |
|
| 670 | + } |
|
| 671 | + |
|
| 672 | + /** |
|
| 673 | + * Given a "local" filepath (what you probably thought was the only filepath), |
|
| 674 | + * converts it into a "remote" filepath (the filepath the currently-in-use |
|
| 675 | + * $wp_filesystem needs to use access the folder or file). |
|
| 676 | + * See http://wordpress.stackexchange.com/questions/124900/using-wp-filesystem-in-plugins |
|
| 677 | + * @param WP_Filesystem_Base $wp_filesystem we aren't initially sure which one |
|
| 678 | + * is in use, so you need to provide it |
|
| 679 | + * @param string $local_filepath the filepath to the folder/file locally |
|
| 680 | + * @throws EE_Error if filesystem credentials are required |
|
| 681 | + * @return string the remote filepath (eg the filepath the filesystem method, eg |
|
| 682 | + * ftp or ssh, will use to access the folder |
|
| 683 | + */ |
|
| 684 | + public static function convert_local_filepath_to_remote_filepath($local_filepath) |
|
| 685 | + { |
|
| 686 | + $wp_filesystem = EEH_File::_get_wp_filesystem($local_filepath); |
|
| 687 | + return str_replace(WP_CONTENT_DIR . DS, $wp_filesystem->wp_content_dir(), $local_filepath); |
|
| 688 | + } |
|
| 689 | 689 | } |
@@ -44,17 +44,17 @@ discard block |
||
| 44 | 44 | 'FHEE__EEH_File___get_wp_filesystem__allow_using_filesystem_direct', |
| 45 | 45 | $filepath && EEH_File::is_in_uploads_folder($filepath), |
| 46 | 46 | $filepath |
| 47 | - ) ) { |
|
| 48 | - if (! EEH_File::$_wp_filesystem_direct instanceof WP_Filesystem_Direct) { |
|
| 49 | - require_once(ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php'); |
|
| 47 | + )) { |
|
| 48 | + if ( ! EEH_File::$_wp_filesystem_direct instanceof WP_Filesystem_Direct) { |
|
| 49 | + require_once(ABSPATH.'wp-admin/includes/class-wp-filesystem-base.php'); |
|
| 50 | 50 | $method = 'direct'; |
| 51 | - $wp_filesystem_direct_file = apply_filters('filesystem_method_file', ABSPATH . 'wp-admin/includes/class-wp-filesystem-' . $method . '.php', $method); |
|
| 51 | + $wp_filesystem_direct_file = apply_filters('filesystem_method_file', ABSPATH.'wp-admin/includes/class-wp-filesystem-'.$method.'.php', $method); |
|
| 52 | 52 | // check constants defined, just like in wp-admin/includes/file.php's WP_Filesystem() |
| 53 | - if (! defined('FS_CHMOD_DIR')) { |
|
| 54 | - define('FS_CHMOD_DIR', ( fileperms(ABSPATH) & 0777 | 0755 )); |
|
| 53 | + if ( ! defined('FS_CHMOD_DIR')) { |
|
| 54 | + define('FS_CHMOD_DIR', (fileperms(ABSPATH) & 0777 | 0755)); |
|
| 55 | 55 | } |
| 56 | - if (! defined('FS_CHMOD_FILE')) { |
|
| 57 | - define('FS_CHMOD_FILE', ( fileperms(ABSPATH . 'index.php') & 0777 | 0644 )); |
|
| 56 | + if ( ! defined('FS_CHMOD_FILE')) { |
|
| 57 | + define('FS_CHMOD_FILE', (fileperms(ABSPATH.'index.php') & 0777 | 0644)); |
|
| 58 | 58 | } |
| 59 | 59 | require_once($wp_filesystem_direct_file); |
| 60 | 60 | EEH_File::$_wp_filesystem_direct = new WP_Filesystem_Direct(array()); |
@@ -63,7 +63,7 @@ discard block |
||
| 63 | 63 | } |
| 64 | 64 | global $wp_filesystem; |
| 65 | 65 | // no filesystem setup ??? |
| 66 | - if (! $wp_filesystem instanceof WP_Filesystem_Base) { |
|
| 66 | + if ( ! $wp_filesystem instanceof WP_Filesystem_Base) { |
|
| 67 | 67 | // if some eager beaver's just trying to get in there too early... |
| 68 | 68 | // let them do it, because we are one of those eager beavers! :P |
| 69 | 69 | /** |
@@ -79,14 +79,14 @@ discard block |
||
| 79 | 79 | if (false && ! did_action('wp_loaded')) { |
| 80 | 80 | $msg = __('An attempt to access and/or write to a file on the server could not be completed due to a lack of sufficient credentials.', 'event_espresso'); |
| 81 | 81 | if (WP_DEBUG) { |
| 82 | - $msg .= '<br />' . __('The WP Filesystem can not be accessed until after the "wp_loaded" hook has run, so it\'s best not to attempt access until the "admin_init" hookpoint.', 'event_espresso'); |
|
| 82 | + $msg .= '<br />'.__('The WP Filesystem can not be accessed until after the "wp_loaded" hook has run, so it\'s best not to attempt access until the "admin_init" hookpoint.', 'event_espresso'); |
|
| 83 | 83 | } |
| 84 | 84 | throw new EE_Error($msg); |
| 85 | 85 | } else { |
| 86 | 86 | // should be loaded if we are past the wp_loaded hook... |
| 87 | - if (! function_exists('WP_Filesystem')) { |
|
| 88 | - require_once(ABSPATH . 'wp-admin/includes/file.php'); |
|
| 89 | - require_once(ABSPATH . 'wp-admin/includes/template.php'); |
|
| 87 | + if ( ! function_exists('WP_Filesystem')) { |
|
| 88 | + require_once(ABSPATH.'wp-admin/includes/file.php'); |
|
| 89 | + require_once(ABSPATH.'wp-admin/includes/template.php'); |
|
| 90 | 90 | } |
| 91 | 91 | // turn on output buffering so that we can capture the credentials form |
| 92 | 92 | ob_start(); |
@@ -94,13 +94,13 @@ discard block |
||
| 94 | 94 | // store credentials form for the time being |
| 95 | 95 | EEH_File::$_credentials_form = ob_get_clean(); |
| 96 | 96 | // basically check for direct or previously configured access |
| 97 | - if (! WP_Filesystem($credentials)) { |
|
| 97 | + if ( ! WP_Filesystem($credentials)) { |
|
| 98 | 98 | // if credentials do NOT exist |
| 99 | 99 | if ($credentials === false) { |
| 100 | - add_action('admin_notices', array( 'EEH_File', 'display_request_filesystem_credentials_form' ), 999); |
|
| 100 | + add_action('admin_notices', array('EEH_File', 'display_request_filesystem_credentials_form'), 999); |
|
| 101 | 101 | throw new EE_Error(__('An attempt to access and/or write to a file on the server could not be completed due to a lack of sufficient credentials.', 'event_espresso')); |
| 102 | 102 | } elseif (is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code()) { |
| 103 | - add_action('admin_notices', array( 'EEH_File', 'display_request_filesystem_credentials_form' ), 999); |
|
| 103 | + add_action('admin_notices', array('EEH_File', 'display_request_filesystem_credentials_form'), 999); |
|
| 104 | 104 | throw new EE_Error( |
| 105 | 105 | sprintf( |
| 106 | 106 | __('WP Filesystem Error: $1%s', 'event_espresso'), |
@@ -119,8 +119,8 @@ discard block |
||
| 119 | 119 | */ |
| 120 | 120 | public static function display_request_filesystem_credentials_form() |
| 121 | 121 | { |
| 122 | - if (! empty(EEH_File::$_credentials_form)) { |
|
| 123 | - echo '<div class="updated espresso-notices-attention"><p>' . EEH_File::$_credentials_form . '</p></div>'; |
|
| 122 | + if ( ! empty(EEH_File::$_credentials_form)) { |
|
| 123 | + echo '<div class="updated espresso-notices-attention"><p>'.EEH_File::$_credentials_form.'</p></div>'; |
|
| 124 | 124 | } |
| 125 | 125 | } |
| 126 | 126 | |
@@ -143,8 +143,8 @@ discard block |
||
| 143 | 143 | // load WP_Filesystem and set file permissions |
| 144 | 144 | $wp_filesystem = EEH_File::_get_wp_filesystem($full_file_path); |
| 145 | 145 | $full_file_path = EEH_File::standardise_directory_separators($full_file_path); |
| 146 | - if (! $wp_filesystem->is_readable(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path))) { |
|
| 147 | - $file_name = ! empty($type_of_file) ? $file_name . ' ' . $type_of_file : $file_name; |
|
| 146 | + if ( ! $wp_filesystem->is_readable(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path))) { |
|
| 147 | + $file_name = ! empty($type_of_file) ? $file_name.' '.$type_of_file : $file_name; |
|
| 148 | 148 | $file_name .= ! empty($file_ext) ? ' file' : ' folder'; |
| 149 | 149 | $msg = sprintf( |
| 150 | 150 | __('The requested %1$s could not be found or is not readable, possibly due to an incorrect filepath, or incorrect file permissions.%2$s', 'event_espresso'), |
@@ -161,7 +161,7 @@ discard block |
||
| 161 | 161 | ); |
| 162 | 162 | } |
| 163 | 163 | if (defined('WP_DEBUG') && WP_DEBUG) { |
| 164 | - throw new EE_Error($msg . '||' . $msg); |
|
| 164 | + throw new EE_Error($msg.'||'.$msg); |
|
| 165 | 165 | } |
| 166 | 166 | return false; |
| 167 | 167 | } |
@@ -187,7 +187,7 @@ discard block |
||
| 187 | 187 | $perms = $wp_filesystem->getchmod(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path)); |
| 188 | 188 | if ($perms) { |
| 189 | 189 | // file permissions exist, but way be set incorrectly |
| 190 | - $type_of_file = ! empty($type_of_file) ? $type_of_file . ' ' : ''; |
|
| 190 | + $type_of_file = ! empty($type_of_file) ? $type_of_file.' ' : ''; |
|
| 191 | 191 | $type_of_file .= ! empty($type_of_file) ? 'file' : 'folder'; |
| 192 | 192 | return sprintf( |
| 193 | 193 | __('File permissions for the requested %1$s are currently set at "%2$s". The recommended permissions are 644 for files and 755 for folders.', 'event_espresso'), |
@@ -226,15 +226,15 @@ discard block |
||
| 226 | 226 | // add DS to folder |
| 227 | 227 | $folder = EEH_File::end_with_directory_separator($folder); |
| 228 | 228 | $wp_filesystem = EEH_File::_get_wp_filesystem($folder); |
| 229 | - if (! $wp_filesystem->is_dir(EEH_File::convert_local_filepath_to_remote_filepath($folder))) { |
|
| 229 | + if ( ! $wp_filesystem->is_dir(EEH_File::convert_local_filepath_to_remote_filepath($folder))) { |
|
| 230 | 230 | // ok so it doesn't exist. Does its parent? Can we write to it? |
| 231 | - if (! EEH_File::ensure_folder_exists_and_is_writable($parent_folder)) { |
|
| 231 | + if ( ! EEH_File::ensure_folder_exists_and_is_writable($parent_folder)) { |
|
| 232 | 232 | return false; |
| 233 | 233 | } |
| 234 | - if (! EEH_File::verify_is_writable($parent_folder, 'folder')) { |
|
| 234 | + if ( ! EEH_File::verify_is_writable($parent_folder, 'folder')) { |
|
| 235 | 235 | return false; |
| 236 | 236 | } else { |
| 237 | - if (! $wp_filesystem->mkdir(EEH_File::convert_local_filepath_to_remote_filepath($folder))) { |
|
| 237 | + if ( ! $wp_filesystem->mkdir(EEH_File::convert_local_filepath_to_remote_filepath($folder))) { |
|
| 238 | 238 | if (defined('WP_DEBUG') && WP_DEBUG) { |
| 239 | 239 | $msg = sprintf(__('"%s" could not be created.', 'event_espresso'), $folder); |
| 240 | 240 | $msg .= EEH_File::_permissions_error_for_unreadable_filepath($folder); |
@@ -244,7 +244,7 @@ discard block |
||
| 244 | 244 | } |
| 245 | 245 | EEH_File::add_index_file($folder); |
| 246 | 246 | } |
| 247 | - } elseif (! EEH_File::verify_is_writable($folder, 'folder')) { |
|
| 247 | + } elseif ( ! EEH_File::verify_is_writable($folder, 'folder')) { |
|
| 248 | 248 | return false; |
| 249 | 249 | } |
| 250 | 250 | return true; |
@@ -264,7 +264,7 @@ discard block |
||
| 264 | 264 | // load WP_Filesystem and set file permissions |
| 265 | 265 | $wp_filesystem = EEH_File::_get_wp_filesystem($full_path); |
| 266 | 266 | $full_path = EEH_File::standardise_directory_separators($full_path); |
| 267 | - if (! $wp_filesystem->is_writable(EEH_File::convert_local_filepath_to_remote_filepath($full_path))) { |
|
| 267 | + if ( ! $wp_filesystem->is_writable(EEH_File::convert_local_filepath_to_remote_filepath($full_path))) { |
|
| 268 | 268 | if (defined('WP_DEBUG') && WP_DEBUG) { |
| 269 | 269 | $msg = sprintf(__('The "%1$s" %2$s is not writable.', 'event_espresso'), $full_path, $file_or_folder); |
| 270 | 270 | $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_path); |
@@ -291,11 +291,11 @@ discard block |
||
| 291 | 291 | $wp_filesystem = EEH_File::_get_wp_filesystem($full_file_path); |
| 292 | 292 | $full_file_path = EEH_File::standardise_directory_separators($full_file_path); |
| 293 | 293 | $parent_folder = EEH_File::get_parent_folder($full_file_path); |
| 294 | - if (! EEH_File::exists($full_file_path)) { |
|
| 295 | - if (! EEH_File::ensure_folder_exists_and_is_writable($parent_folder)) { |
|
| 294 | + if ( ! EEH_File::exists($full_file_path)) { |
|
| 295 | + if ( ! EEH_File::ensure_folder_exists_and_is_writable($parent_folder)) { |
|
| 296 | 296 | return false; |
| 297 | 297 | } |
| 298 | - if (! $wp_filesystem->touch(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path))) { |
|
| 298 | + if ( ! $wp_filesystem->touch(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path))) { |
|
| 299 | 299 | if (defined('WP_DEBUG') && WP_DEBUG) { |
| 300 | 300 | $msg = sprintf(__('The "%s" file could not be created.', 'event_espresso'), $full_file_path); |
| 301 | 301 | $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_file_path); |
@@ -304,7 +304,7 @@ discard block |
||
| 304 | 304 | return false; |
| 305 | 305 | } |
| 306 | 306 | } |
| 307 | - if (! EEH_File::verify_is_writable($full_file_path, 'file')) { |
|
| 307 | + if ( ! EEH_File::verify_is_writable($full_file_path, 'file')) { |
|
| 308 | 308 | return false; |
| 309 | 309 | } |
| 310 | 310 | return true; |
@@ -363,9 +363,9 @@ discard block |
||
| 363 | 363 | public static function write_to_file($full_file_path = '', $file_contents = '', $file_type = '') |
| 364 | 364 | { |
| 365 | 365 | $full_file_path = EEH_File::standardise_directory_separators($full_file_path); |
| 366 | - $file_type = ! empty($file_type) ? rtrim($file_type, ' ') . ' ' : ''; |
|
| 366 | + $file_type = ! empty($file_type) ? rtrim($file_type, ' ').' ' : ''; |
|
| 367 | 367 | $folder = EEH_File::remove_filename_from_filepath($full_file_path); |
| 368 | - if (! EEH_File::verify_is_writable($folder, 'folder')) { |
|
| 368 | + if ( ! EEH_File::verify_is_writable($folder, 'folder')) { |
|
| 369 | 369 | if (defined('WP_DEBUG') && WP_DEBUG) { |
| 370 | 370 | $msg = sprintf(__('The %1$sfile located at "%2$s" is not writable.', 'event_espresso'), $file_type, $full_file_path); |
| 371 | 371 | $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_file_path); |
@@ -376,7 +376,7 @@ discard block |
||
| 376 | 376 | // load WP_Filesystem and set file permissions |
| 377 | 377 | $wp_filesystem = EEH_File::_get_wp_filesystem($full_file_path); |
| 378 | 378 | // write the file |
| 379 | - if (! $wp_filesystem->put_contents(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path), $file_contents)) { |
|
| 379 | + if ( ! $wp_filesystem->put_contents(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path), $file_contents)) { |
|
| 380 | 380 | if (defined('WP_DEBUG') && WP_DEBUG) { |
| 381 | 381 | $msg = sprintf(__('The %1$sfile located at "%2$s" could not be written to.', 'event_espresso'), $file_type, $full_file_path); |
| 382 | 382 | $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_file_path, 'f'); |
@@ -486,8 +486,8 @@ discard block |
||
| 486 | 486 | public static function add_htaccess_deny_from_all($folder = '') |
| 487 | 487 | { |
| 488 | 488 | $folder = EEH_File::standardise_and_end_with_directory_separator($folder); |
| 489 | - if (! EEH_File::exists($folder . '.htaccess')) { |
|
| 490 | - if (! EEH_File::write_to_file($folder . '.htaccess', 'deny from all', '.htaccess')) { |
|
| 489 | + if ( ! EEH_File::exists($folder.'.htaccess')) { |
|
| 490 | + if ( ! EEH_File::write_to_file($folder.'.htaccess', 'deny from all', '.htaccess')) { |
|
| 491 | 491 | return false; |
| 492 | 492 | } |
| 493 | 493 | } |
@@ -504,8 +504,8 @@ discard block |
||
| 504 | 504 | public static function add_index_file($folder) |
| 505 | 505 | { |
| 506 | 506 | $folder = EEH_File::standardise_and_end_with_directory_separator($folder); |
| 507 | - if (! EEH_File::exists($folder . 'index.php')) { |
|
| 508 | - if (! EEH_File::write_to_file($folder . 'index.php', 'You are not permitted to read from this folder', '.php')) { |
|
| 507 | + if ( ! EEH_File::exists($folder.'index.php')) { |
|
| 508 | + if ( ! EEH_File::write_to_file($folder.'index.php', 'You are not permitted to read from this folder', '.php')) { |
|
| 509 | 509 | return false; |
| 510 | 510 | } |
| 511 | 511 | } |
@@ -539,7 +539,7 @@ discard block |
||
| 539 | 539 | */ |
| 540 | 540 | public static function standardise_directory_separators($file_path) |
| 541 | 541 | { |
| 542 | - return str_replace(array( '\\', '/' ), DS, $file_path); |
|
| 542 | + return str_replace(array('\\', '/'), DS, $file_path); |
|
| 543 | 543 | } |
| 544 | 544 | |
| 545 | 545 | |
@@ -552,7 +552,7 @@ discard block |
||
| 552 | 552 | */ |
| 553 | 553 | public static function end_with_directory_separator($file_path) |
| 554 | 554 | { |
| 555 | - return rtrim($file_path, '/\\') . DS; |
|
| 555 | + return rtrim($file_path, '/\\').DS; |
|
| 556 | 556 | } |
| 557 | 557 | |
| 558 | 558 | |
@@ -587,17 +587,17 @@ discard block |
||
| 587 | 587 | foreach ($folder_paths as $folder_path) { |
| 588 | 588 | $folder_path = self::standardise_and_end_with_directory_separator($folder_path); |
| 589 | 589 | // load WP_Filesystem and set file permissions |
| 590 | - $files_in_folder = glob($folder_path . '*'); |
|
| 590 | + $files_in_folder = glob($folder_path.'*'); |
|
| 591 | 591 | $class_to_folder_path = array(); |
| 592 | 592 | if ($files_in_folder) { |
| 593 | 593 | foreach ($files_in_folder as $file_path) { |
| 594 | 594 | // only add files, not folders |
| 595 | - if (! is_dir($file_path)) { |
|
| 595 | + if ( ! is_dir($file_path)) { |
|
| 596 | 596 | if ($index_numerically) { |
| 597 | 597 | $class_to_folder_path[] = $file_path; |
| 598 | 598 | } else { |
| 599 | 599 | $classname = self::get_classname_from_filepath_with_standard_filename($file_path); |
| 600 | - $class_to_folder_path[ $classname ] = $file_path; |
|
| 600 | + $class_to_folder_path[$classname] = $file_path; |
|
| 601 | 601 | } |
| 602 | 602 | } |
| 603 | 603 | } |
@@ -619,7 +619,7 @@ discard block |
||
| 619 | 619 | public static function copy($source_file, $destination_file, $overwrite = false) |
| 620 | 620 | { |
| 621 | 621 | $full_source_path = EEH_File::standardise_directory_separators($source_file); |
| 622 | - if (! EEH_File::exists($full_source_path)) { |
|
| 622 | + if ( ! EEH_File::exists($full_source_path)) { |
|
| 623 | 623 | if (defined('WP_DEBUG') && WP_DEBUG) { |
| 624 | 624 | $msg = sprintf(__('The file located at "%2$s" is not readable or doesn\'t exist.', 'event_espresso'), $full_source_path); |
| 625 | 625 | $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_source_path); |
@@ -631,7 +631,7 @@ discard block |
||
| 631 | 631 | $full_dest_path = EEH_File::standardise_directory_separators($destination_file); |
| 632 | 632 | $folder = EEH_File::remove_filename_from_filepath($full_dest_path); |
| 633 | 633 | EEH_File::ensure_folder_exists_and_is_writable($folder); |
| 634 | - if (! EEH_File::verify_is_writable($folder, 'folder')) { |
|
| 634 | + if ( ! EEH_File::verify_is_writable($folder, 'folder')) { |
|
| 635 | 635 | if (defined('WP_DEBUG') && WP_DEBUG) { |
| 636 | 636 | $msg = sprintf(__('The file located at "%2$s" is not writable.', 'event_espresso'), $full_dest_path); |
| 637 | 637 | $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_dest_path); |
@@ -643,7 +643,7 @@ discard block |
||
| 643 | 643 | // load WP_Filesystem and set file permissions |
| 644 | 644 | $wp_filesystem = EEH_File::_get_wp_filesystem($destination_file); |
| 645 | 645 | // write the file |
| 646 | - if (! $wp_filesystem->copy( |
|
| 646 | + if ( ! $wp_filesystem->copy( |
|
| 647 | 647 | EEH_File::convert_local_filepath_to_remote_filepath($full_source_path), |
| 648 | 648 | EEH_File::convert_local_filepath_to_remote_filepath($full_dest_path), |
| 649 | 649 | $overwrite |
@@ -684,6 +684,6 @@ discard block |
||
| 684 | 684 | public static function convert_local_filepath_to_remote_filepath($local_filepath) |
| 685 | 685 | { |
| 686 | 686 | $wp_filesystem = EEH_File::_get_wp_filesystem($local_filepath); |
| 687 | - return str_replace(WP_CONTENT_DIR . DS, $wp_filesystem->wp_content_dir(), $local_filepath); |
|
| 687 | + return str_replace(WP_CONTENT_DIR.DS, $wp_filesystem->wp_content_dir(), $local_filepath); |
|
| 688 | 688 | } |
| 689 | 689 | } |
@@ -16,39 +16,39 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class JsonWpOptionManager |
| 18 | 18 | { |
| 19 | - /** |
|
| 20 | - * Updates the object with what's in the DB (specifically, the wp_options table). If nothing is in the DB, leaves |
|
| 21 | - * the object alone and returns false. |
|
| 22 | - * @since $VID:$ |
|
| 23 | - * @param JsonWpOptionSerializableInterface $obj |
|
| 24 | - * @return bool |
|
| 25 | - */ |
|
| 26 | - public function populateFromDb(JsonWpOptionSerializableInterface $obj) |
|
| 27 | - { |
|
| 28 | - $option = get_option($obj->getWpOptionName()); |
|
| 29 | - if ($option) { |
|
| 30 | - $json = json_decode($option); |
|
| 31 | - if (is_array($json) || $json instanceof stdClass) { |
|
| 32 | - return $obj->fromJsonSerializedData($json); |
|
| 33 | - } |
|
| 34 | - } |
|
| 35 | - return false; |
|
| 36 | - } |
|
| 19 | + /** |
|
| 20 | + * Updates the object with what's in the DB (specifically, the wp_options table). If nothing is in the DB, leaves |
|
| 21 | + * the object alone and returns false. |
|
| 22 | + * @since $VID:$ |
|
| 23 | + * @param JsonWpOptionSerializableInterface $obj |
|
| 24 | + * @return bool |
|
| 25 | + */ |
|
| 26 | + public function populateFromDb(JsonWpOptionSerializableInterface $obj) |
|
| 27 | + { |
|
| 28 | + $option = get_option($obj->getWpOptionName()); |
|
| 29 | + if ($option) { |
|
| 30 | + $json = json_decode($option); |
|
| 31 | + if (is_array($json) || $json instanceof stdClass) { |
|
| 32 | + return $obj->fromJsonSerializedData($json); |
|
| 33 | + } |
|
| 34 | + } |
|
| 35 | + return false; |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Saves the object's data to the wp_options table for later use. |
|
| 40 | - * @since $VID:$ |
|
| 41 | - * @param JsonWpOptionSerializableInterface $obj |
|
| 42 | - * @return bool |
|
| 43 | - */ |
|
| 44 | - public function saveToDb(JsonWpOptionSerializableInterface $obj) |
|
| 45 | - { |
|
| 46 | - return update_option( |
|
| 47 | - $obj->getWpOptionName(), |
|
| 48 | - wp_json_encode($obj->toJsonSerializableData()), |
|
| 49 | - false |
|
| 50 | - ); |
|
| 51 | - } |
|
| 38 | + /** |
|
| 39 | + * Saves the object's data to the wp_options table for later use. |
|
| 40 | + * @since $VID:$ |
|
| 41 | + * @param JsonWpOptionSerializableInterface $obj |
|
| 42 | + * @return bool |
|
| 43 | + */ |
|
| 44 | + public function saveToDb(JsonWpOptionSerializableInterface $obj) |
|
| 45 | + { |
|
| 46 | + return update_option( |
|
| 47 | + $obj->getWpOptionName(), |
|
| 48 | + wp_json_encode($obj->toJsonSerializableData()), |
|
| 49 | + false |
|
| 50 | + ); |
|
| 51 | + } |
|
| 52 | 52 | } |
| 53 | 53 | // End of file JsonWpOptionManager.php |
| 54 | 54 | // Location: EventEspresso\core\services\options/JsonWpOptionManager.php |
@@ -19,164 +19,164 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class FileSubmission implements FileSubmissionInterface |
| 21 | 21 | { |
| 22 | - /** |
|
| 23 | - * @var string original name on the client machine |
|
| 24 | - */ |
|
| 25 | - protected $name; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * @var string mime type |
|
| 29 | - */ |
|
| 30 | - protected $type; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @var string file extension |
|
| 34 | - */ |
|
| 35 | - protected $extension; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @var int in bytes |
|
| 39 | - */ |
|
| 40 | - protected $size; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * @var string local filepath to the temporary file |
|
| 44 | - */ |
|
| 45 | - protected $tmp_file; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * @var int one of UPLOAD_ERR_OK, UPLOAD_ERR_NO_FILE, UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE or other values |
|
| 49 | - * although those aren't expected. |
|
| 50 | - */ |
|
| 51 | - protected $error_code; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * FileSubmission constructor. |
|
| 55 | - * @param $name |
|
| 56 | - * @param $tmp_file |
|
| 57 | - * @param $size |
|
| 58 | - * @param null $error_code |
|
| 59 | - * @throws InvalidArgumentException |
|
| 60 | - */ |
|
| 61 | - public function __construct($name, $tmp_file, $size, $error_code = null) |
|
| 62 | - { |
|
| 63 | - $this->name = basename($name); |
|
| 64 | - $scheme = parse_url($tmp_file, PHP_URL_SCHEME); |
|
| 65 | - if (in_array($scheme, ['http', 'https'])) { |
|
| 66 | - // Wait a minute- just local filepaths please, no URL schemes allowed! |
|
| 67 | - throw new InvalidArgumentException( |
|
| 68 | - sprintf( |
|
| 69 | - // @codingStandardsIgnoreStart |
|
| 70 | - esc_html__('The scheme ("%1$s") on the temporary file ("%2$s") indicates is located elsewhere, that’s not ok!', 'event_espresso'), |
|
| 71 | - // @codingStandardsIgnoreEnd |
|
| 72 | - $scheme, |
|
| 73 | - $tmp_file |
|
| 74 | - ) |
|
| 75 | - ); |
|
| 76 | - } |
|
| 77 | - $this->tmp_file = (string) $tmp_file; |
|
| 78 | - $this->size = (int) $size; |
|
| 79 | - $this->error_code = (int) $error_code; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * @return string |
|
| 84 | - */ |
|
| 85 | - public function getName() |
|
| 86 | - { |
|
| 87 | - return $this->name; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Gets the file's mime type |
|
| 92 | - * @return string |
|
| 93 | - */ |
|
| 94 | - public function getType() |
|
| 95 | - { |
|
| 96 | - if (!$this->type) { |
|
| 97 | - $this->type = $this->determineType(); |
|
| 98 | - } |
|
| 99 | - return $this->type; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * @since $VID:$ |
|
| 104 | - * @return string |
|
| 105 | - */ |
|
| 106 | - protected function determineType() |
|
| 107 | - { |
|
| 108 | - if (!$this->getTmpFile()) { |
|
| 109 | - return ''; |
|
| 110 | - } |
|
| 111 | - $finfo = new finfo(FILEINFO_MIME_TYPE); |
|
| 112 | - return $finfo->file($this->getTmpFile()); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Gets the file's extension. |
|
| 117 | - * @since $VID:$ |
|
| 118 | - * @return string |
|
| 119 | - */ |
|
| 120 | - public function getExtension() |
|
| 121 | - { |
|
| 122 | - if (!$this->extension) { |
|
| 123 | - $this->extension = $this->determineExtension(); |
|
| 124 | - } |
|
| 125 | - return $this->extension; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Determine's the file's extension given the temporary file. |
|
| 130 | - * @since $VID:$ |
|
| 131 | - * @return string |
|
| 132 | - */ |
|
| 133 | - protected function determineExtension() |
|
| 134 | - { |
|
| 135 | - $position_of_period = strrpos($this->getName(), '.'); |
|
| 136 | - if ($position_of_period === false) { |
|
| 137 | - return ''; |
|
| 138 | - } |
|
| 139 | - return mb_substr( |
|
| 140 | - $this->getName(), |
|
| 141 | - $position_of_period + 1 |
|
| 142 | - ); |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Gets the size of the file |
|
| 147 | - * @return int |
|
| 148 | - */ |
|
| 149 | - public function getSize() |
|
| 150 | - { |
|
| 151 | - return $this->size; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * Gets the path to the temporary file which was uploaded. |
|
| 156 | - * @return string |
|
| 157 | - */ |
|
| 158 | - public function getTmpFile() |
|
| 159 | - { |
|
| 160 | - return $this->tmp_file; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * @since $VID:$ |
|
| 165 | - * @return string |
|
| 166 | - */ |
|
| 167 | - public function __toString() |
|
| 168 | - { |
|
| 169 | - return $this->getName(); |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * Gets the error code PHP reported for the file upload. |
|
| 174 | - * @return string |
|
| 175 | - */ |
|
| 176 | - public function getErrorCode() |
|
| 177 | - { |
|
| 178 | - return $this->error_code; |
|
| 179 | - } |
|
| 22 | + /** |
|
| 23 | + * @var string original name on the client machine |
|
| 24 | + */ |
|
| 25 | + protected $name; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * @var string mime type |
|
| 29 | + */ |
|
| 30 | + protected $type; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @var string file extension |
|
| 34 | + */ |
|
| 35 | + protected $extension; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @var int in bytes |
|
| 39 | + */ |
|
| 40 | + protected $size; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * @var string local filepath to the temporary file |
|
| 44 | + */ |
|
| 45 | + protected $tmp_file; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * @var int one of UPLOAD_ERR_OK, UPLOAD_ERR_NO_FILE, UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE or other values |
|
| 49 | + * although those aren't expected. |
|
| 50 | + */ |
|
| 51 | + protected $error_code; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * FileSubmission constructor. |
|
| 55 | + * @param $name |
|
| 56 | + * @param $tmp_file |
|
| 57 | + * @param $size |
|
| 58 | + * @param null $error_code |
|
| 59 | + * @throws InvalidArgumentException |
|
| 60 | + */ |
|
| 61 | + public function __construct($name, $tmp_file, $size, $error_code = null) |
|
| 62 | + { |
|
| 63 | + $this->name = basename($name); |
|
| 64 | + $scheme = parse_url($tmp_file, PHP_URL_SCHEME); |
|
| 65 | + if (in_array($scheme, ['http', 'https'])) { |
|
| 66 | + // Wait a minute- just local filepaths please, no URL schemes allowed! |
|
| 67 | + throw new InvalidArgumentException( |
|
| 68 | + sprintf( |
|
| 69 | + // @codingStandardsIgnoreStart |
|
| 70 | + esc_html__('The scheme ("%1$s") on the temporary file ("%2$s") indicates is located elsewhere, that’s not ok!', 'event_espresso'), |
|
| 71 | + // @codingStandardsIgnoreEnd |
|
| 72 | + $scheme, |
|
| 73 | + $tmp_file |
|
| 74 | + ) |
|
| 75 | + ); |
|
| 76 | + } |
|
| 77 | + $this->tmp_file = (string) $tmp_file; |
|
| 78 | + $this->size = (int) $size; |
|
| 79 | + $this->error_code = (int) $error_code; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * @return string |
|
| 84 | + */ |
|
| 85 | + public function getName() |
|
| 86 | + { |
|
| 87 | + return $this->name; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Gets the file's mime type |
|
| 92 | + * @return string |
|
| 93 | + */ |
|
| 94 | + public function getType() |
|
| 95 | + { |
|
| 96 | + if (!$this->type) { |
|
| 97 | + $this->type = $this->determineType(); |
|
| 98 | + } |
|
| 99 | + return $this->type; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * @since $VID:$ |
|
| 104 | + * @return string |
|
| 105 | + */ |
|
| 106 | + protected function determineType() |
|
| 107 | + { |
|
| 108 | + if (!$this->getTmpFile()) { |
|
| 109 | + return ''; |
|
| 110 | + } |
|
| 111 | + $finfo = new finfo(FILEINFO_MIME_TYPE); |
|
| 112 | + return $finfo->file($this->getTmpFile()); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Gets the file's extension. |
|
| 117 | + * @since $VID:$ |
|
| 118 | + * @return string |
|
| 119 | + */ |
|
| 120 | + public function getExtension() |
|
| 121 | + { |
|
| 122 | + if (!$this->extension) { |
|
| 123 | + $this->extension = $this->determineExtension(); |
|
| 124 | + } |
|
| 125 | + return $this->extension; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Determine's the file's extension given the temporary file. |
|
| 130 | + * @since $VID:$ |
|
| 131 | + * @return string |
|
| 132 | + */ |
|
| 133 | + protected function determineExtension() |
|
| 134 | + { |
|
| 135 | + $position_of_period = strrpos($this->getName(), '.'); |
|
| 136 | + if ($position_of_period === false) { |
|
| 137 | + return ''; |
|
| 138 | + } |
|
| 139 | + return mb_substr( |
|
| 140 | + $this->getName(), |
|
| 141 | + $position_of_period + 1 |
|
| 142 | + ); |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Gets the size of the file |
|
| 147 | + * @return int |
|
| 148 | + */ |
|
| 149 | + public function getSize() |
|
| 150 | + { |
|
| 151 | + return $this->size; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * Gets the path to the temporary file which was uploaded. |
|
| 156 | + * @return string |
|
| 157 | + */ |
|
| 158 | + public function getTmpFile() |
|
| 159 | + { |
|
| 160 | + return $this->tmp_file; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * @since $VID:$ |
|
| 165 | + * @return string |
|
| 166 | + */ |
|
| 167 | + public function __toString() |
|
| 168 | + { |
|
| 169 | + return $this->getName(); |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * Gets the error code PHP reported for the file upload. |
|
| 174 | + * @return string |
|
| 175 | + */ |
|
| 176 | + public function getErrorCode() |
|
| 177 | + { |
|
| 178 | + return $this->error_code; |
|
| 179 | + } |
|
| 180 | 180 | } |
| 181 | 181 | // End of file FileSubmission.php |
| 182 | 182 | // Location: EventEspresso\core\services\request\files/FileSubmission.php |
@@ -93,7 +93,7 @@ discard block |
||
| 93 | 93 | */ |
| 94 | 94 | public function getType() |
| 95 | 95 | { |
| 96 | - if (!$this->type) { |
|
| 96 | + if ( ! $this->type) { |
|
| 97 | 97 | $this->type = $this->determineType(); |
| 98 | 98 | } |
| 99 | 99 | return $this->type; |
@@ -105,7 +105,7 @@ discard block |
||
| 105 | 105 | */ |
| 106 | 106 | protected function determineType() |
| 107 | 107 | { |
| 108 | - if (!$this->getTmpFile()) { |
|
| 108 | + if ( ! $this->getTmpFile()) { |
|
| 109 | 109 | return ''; |
| 110 | 110 | } |
| 111 | 111 | $finfo = new finfo(FILEINFO_MIME_TYPE); |
@@ -119,7 +119,7 @@ discard block |
||
| 119 | 119 | */ |
| 120 | 120 | public function getExtension() |
| 121 | 121 | { |
| 122 | - if (!$this->extension) { |
|
| 122 | + if ( ! $this->extension) { |
|
| 123 | 123 | $this->extension = $this->determineExtension(); |
| 124 | 124 | } |
| 125 | 125 | return $this->extension; |
@@ -15,21 +15,21 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | interface JsonSerializableAndUnserializable |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * Creates a simple PHP array or stdClass from this object's properties, which can be easily serialized using |
|
| 20 | - * wp_json_serialize(). |
|
| 21 | - * @since $VID:$ |
|
| 22 | - * @return mixed |
|
| 23 | - */ |
|
| 24 | - public function toJsonSerializableData(); |
|
| 18 | + /** |
|
| 19 | + * Creates a simple PHP array or stdClass from this object's properties, which can be easily serialized using |
|
| 20 | + * wp_json_serialize(). |
|
| 21 | + * @since $VID:$ |
|
| 22 | + * @return mixed |
|
| 23 | + */ |
|
| 24 | + public function toJsonSerializableData(); |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Initializes this object from data |
|
| 28 | - * @since $VID:$ |
|
| 29 | - * @param mixed $data |
|
| 30 | - * @return boolean success |
|
| 31 | - */ |
|
| 32 | - public function fromJsonSerializedData($data); |
|
| 26 | + /** |
|
| 27 | + * Initializes this object from data |
|
| 28 | + * @since $VID:$ |
|
| 29 | + * @param mixed $data |
|
| 30 | + * @return boolean success |
|
| 31 | + */ |
|
| 32 | + public function fromJsonSerializedData($data); |
|
| 33 | 33 | } |
| 34 | 34 | // End of file JsonSerializableAndUnserializable.php |
| 35 | 35 | // Location: EventEspresso\core\services\json/JsonSerializableAndUnserializable.php |
@@ -71,7 +71,7 @@ |
||
| 71 | 71 | * given a valid namespace, will find all files that match the provided mask |
| 72 | 72 | * |
| 73 | 73 | * @access public |
| 74 | - * @param string|array $namespaces |
|
| 74 | + * @param string $namespaces |
|
| 75 | 75 | * @return array |
| 76 | 76 | * @throws InvalidClassException |
| 77 | 77 | * @throws InvalidDataTypeException |
@@ -18,156 +18,156 @@ |
||
| 18 | 18 | class FqcnLocator extends Locator |
| 19 | 19 | { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * @var array $FQCNs |
|
| 23 | - */ |
|
| 24 | - protected $FQCNs = array(); |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * @var array $namespaces |
|
| 28 | - */ |
|
| 29 | - protected $namespaces = array(); |
|
| 30 | - |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @access protected |
|
| 34 | - * @param string $namespace |
|
| 35 | - * @param string $namespace_base_dir |
|
| 36 | - * @throws InvalidDataTypeException |
|
| 37 | - */ |
|
| 38 | - protected function setNamespace($namespace, $namespace_base_dir) |
|
| 39 | - { |
|
| 40 | - if (! is_string($namespace)) { |
|
| 41 | - throw new InvalidDataTypeException('$namespace', $namespace, 'string'); |
|
| 42 | - } |
|
| 43 | - if (! is_string($namespace_base_dir)) { |
|
| 44 | - throw new InvalidDataTypeException('$namespace_base_dir', $namespace_base_dir, 'string'); |
|
| 45 | - } |
|
| 46 | - $this->namespaces[ $namespace ] = $namespace_base_dir; |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @access public |
|
| 52 | - * @return array |
|
| 53 | - */ |
|
| 54 | - public function getFQCNs() |
|
| 55 | - { |
|
| 56 | - return $this->FQCNs; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * @access public |
|
| 62 | - * @return int |
|
| 63 | - */ |
|
| 64 | - public function count() |
|
| 65 | - { |
|
| 66 | - return count($this->FQCNs); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * given a valid namespace, will find all files that match the provided mask |
|
| 72 | - * |
|
| 73 | - * @access public |
|
| 74 | - * @param string|array $namespaces |
|
| 75 | - * @return array |
|
| 76 | - * @throws InvalidClassException |
|
| 77 | - * @throws InvalidDataTypeException |
|
| 78 | - */ |
|
| 79 | - public function locate($namespaces) |
|
| 80 | - { |
|
| 81 | - if (! (is_string($namespaces) || is_array($namespaces))) { |
|
| 82 | - throw new InvalidDataTypeException('$namespaces', $namespaces, 'string or array'); |
|
| 83 | - } |
|
| 84 | - foreach ((array) $namespaces as $namespace) { |
|
| 85 | - foreach ($this->findFQCNsByNamespace($namespace) as $key => $file) { |
|
| 86 | - $this->FQCNs[ $key ] = $file; |
|
| 87 | - } |
|
| 88 | - } |
|
| 89 | - return $this->FQCNs; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * given a partial namespace, will find all files in that folder |
|
| 95 | - * ** PLZ NOTE ** |
|
| 96 | - * This assumes that all files within the specified folder should be loaded |
|
| 97 | - * |
|
| 98 | - * @access protected |
|
| 99 | - * @param string $partial_namespace |
|
| 100 | - * @return array |
|
| 101 | - * @throws InvalidClassException |
|
| 102 | - * @throws InvalidDataTypeException |
|
| 103 | - */ |
|
| 104 | - protected function findFQCNsByNamespace($partial_namespace) |
|
| 105 | - { |
|
| 106 | - $iterator = new FilesystemIterator( |
|
| 107 | - $this->getDirectoryFromPartialNamespace($partial_namespace) |
|
| 108 | - ); |
|
| 109 | - $iterator->setFlags(FilesystemIterator::CURRENT_AS_FILEINFO); |
|
| 110 | - $iterator->setFlags(FilesystemIterator::UNIX_PATHS); |
|
| 111 | - if (iterator_count($iterator) === 0) { |
|
| 112 | - return array(); |
|
| 113 | - } |
|
| 114 | - foreach ($iterator as $file) { |
|
| 115 | - if ($file->isFile() && $file->getExtension() === 'php') { |
|
| 116 | - $file = $file->getPath() . DS . $file->getBasename('.php'); |
|
| 117 | - foreach ($this->namespaces as $namespace => $base_dir) { |
|
| 118 | - $namespace .= Psr4Autoloader::NS; |
|
| 119 | - if (strpos($file, $base_dir) === 0) { |
|
| 120 | - $this->FQCNs[] = Psr4Autoloader::NS . str_replace( |
|
| 121 | - array($base_dir, DS), |
|
| 122 | - array($namespace, Psr4Autoloader::NS), |
|
| 123 | - $file |
|
| 124 | - ); |
|
| 125 | - } |
|
| 126 | - } |
|
| 127 | - } |
|
| 128 | - } |
|
| 129 | - return $this->FQCNs; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * getDirectoryFromPartialNamespace |
|
| 135 | - * |
|
| 136 | - * @access protected |
|
| 137 | - * @param string $partial_namespace almost fully qualified class name ? |
|
| 138 | - * @return string |
|
| 139 | - * @throws InvalidDataTypeException |
|
| 140 | - * @throws InvalidClassException |
|
| 141 | - */ |
|
| 142 | - protected function getDirectoryFromPartialNamespace($partial_namespace) |
|
| 143 | - { |
|
| 144 | - if (empty($partial_namespace)) { |
|
| 145 | - throw new InvalidClassException($partial_namespace); |
|
| 146 | - } |
|
| 147 | - // load our PSR-4 Autoloader so we can get the list of registered namespaces from it |
|
| 148 | - $psr4_loader = \EE_Psr4AutoloaderInit::psr4_loader(); |
|
| 149 | - // breakup the incoming namespace into segments so we can loop thru them |
|
| 150 | - $namespace_segments = explode(Psr4Autoloader::NS, trim($partial_namespace, Psr4Autoloader::NS)); |
|
| 151 | - // we're only interested in the Vendor and secondary base, so pull those from the array |
|
| 152 | - $vendor_base = array_slice($namespace_segments, 0, 2); |
|
| 153 | - $namespace = $prefix = null; |
|
| 154 | - while (! empty($vendor_base)) { |
|
| 155 | - $namespace = implode(Psr4Autoloader::NS, $vendor_base); |
|
| 156 | - // check if there's a base directory registered for that namespace |
|
| 157 | - $prefix = $psr4_loader->prefixes($namespace . Psr4Autoloader::NS); |
|
| 158 | - if (! empty($prefix) && ! empty($prefix[0])) { |
|
| 159 | - // found one! |
|
| 160 | - break; |
|
| 161 | - } |
|
| 162 | - // remove base and try vendor only portion of namespace |
|
| 163 | - array_pop($vendor_base); |
|
| 164 | - } |
|
| 165 | - // nope? then the incoming namespace is invalid |
|
| 166 | - if (empty($prefix) || empty($prefix[0])) { |
|
| 167 | - throw new InvalidClassException($partial_namespace); |
|
| 168 | - } |
|
| 169 | - $this->setNamespace($namespace, $prefix[0]); |
|
| 170 | - // but if it's good, add that base directory to the rest of the path, and return it |
|
| 171 | - return $prefix[0] . implode(DS, array_diff($namespace_segments, $vendor_base)) . DS; |
|
| 172 | - } |
|
| 21 | + /** |
|
| 22 | + * @var array $FQCNs |
|
| 23 | + */ |
|
| 24 | + protected $FQCNs = array(); |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * @var array $namespaces |
|
| 28 | + */ |
|
| 29 | + protected $namespaces = array(); |
|
| 30 | + |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @access protected |
|
| 34 | + * @param string $namespace |
|
| 35 | + * @param string $namespace_base_dir |
|
| 36 | + * @throws InvalidDataTypeException |
|
| 37 | + */ |
|
| 38 | + protected function setNamespace($namespace, $namespace_base_dir) |
|
| 39 | + { |
|
| 40 | + if (! is_string($namespace)) { |
|
| 41 | + throw new InvalidDataTypeException('$namespace', $namespace, 'string'); |
|
| 42 | + } |
|
| 43 | + if (! is_string($namespace_base_dir)) { |
|
| 44 | + throw new InvalidDataTypeException('$namespace_base_dir', $namespace_base_dir, 'string'); |
|
| 45 | + } |
|
| 46 | + $this->namespaces[ $namespace ] = $namespace_base_dir; |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @access public |
|
| 52 | + * @return array |
|
| 53 | + */ |
|
| 54 | + public function getFQCNs() |
|
| 55 | + { |
|
| 56 | + return $this->FQCNs; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * @access public |
|
| 62 | + * @return int |
|
| 63 | + */ |
|
| 64 | + public function count() |
|
| 65 | + { |
|
| 66 | + return count($this->FQCNs); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * given a valid namespace, will find all files that match the provided mask |
|
| 72 | + * |
|
| 73 | + * @access public |
|
| 74 | + * @param string|array $namespaces |
|
| 75 | + * @return array |
|
| 76 | + * @throws InvalidClassException |
|
| 77 | + * @throws InvalidDataTypeException |
|
| 78 | + */ |
|
| 79 | + public function locate($namespaces) |
|
| 80 | + { |
|
| 81 | + if (! (is_string($namespaces) || is_array($namespaces))) { |
|
| 82 | + throw new InvalidDataTypeException('$namespaces', $namespaces, 'string or array'); |
|
| 83 | + } |
|
| 84 | + foreach ((array) $namespaces as $namespace) { |
|
| 85 | + foreach ($this->findFQCNsByNamespace($namespace) as $key => $file) { |
|
| 86 | + $this->FQCNs[ $key ] = $file; |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | + return $this->FQCNs; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * given a partial namespace, will find all files in that folder |
|
| 95 | + * ** PLZ NOTE ** |
|
| 96 | + * This assumes that all files within the specified folder should be loaded |
|
| 97 | + * |
|
| 98 | + * @access protected |
|
| 99 | + * @param string $partial_namespace |
|
| 100 | + * @return array |
|
| 101 | + * @throws InvalidClassException |
|
| 102 | + * @throws InvalidDataTypeException |
|
| 103 | + */ |
|
| 104 | + protected function findFQCNsByNamespace($partial_namespace) |
|
| 105 | + { |
|
| 106 | + $iterator = new FilesystemIterator( |
|
| 107 | + $this->getDirectoryFromPartialNamespace($partial_namespace) |
|
| 108 | + ); |
|
| 109 | + $iterator->setFlags(FilesystemIterator::CURRENT_AS_FILEINFO); |
|
| 110 | + $iterator->setFlags(FilesystemIterator::UNIX_PATHS); |
|
| 111 | + if (iterator_count($iterator) === 0) { |
|
| 112 | + return array(); |
|
| 113 | + } |
|
| 114 | + foreach ($iterator as $file) { |
|
| 115 | + if ($file->isFile() && $file->getExtension() === 'php') { |
|
| 116 | + $file = $file->getPath() . DS . $file->getBasename('.php'); |
|
| 117 | + foreach ($this->namespaces as $namespace => $base_dir) { |
|
| 118 | + $namespace .= Psr4Autoloader::NS; |
|
| 119 | + if (strpos($file, $base_dir) === 0) { |
|
| 120 | + $this->FQCNs[] = Psr4Autoloader::NS . str_replace( |
|
| 121 | + array($base_dir, DS), |
|
| 122 | + array($namespace, Psr4Autoloader::NS), |
|
| 123 | + $file |
|
| 124 | + ); |
|
| 125 | + } |
|
| 126 | + } |
|
| 127 | + } |
|
| 128 | + } |
|
| 129 | + return $this->FQCNs; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * getDirectoryFromPartialNamespace |
|
| 135 | + * |
|
| 136 | + * @access protected |
|
| 137 | + * @param string $partial_namespace almost fully qualified class name ? |
|
| 138 | + * @return string |
|
| 139 | + * @throws InvalidDataTypeException |
|
| 140 | + * @throws InvalidClassException |
|
| 141 | + */ |
|
| 142 | + protected function getDirectoryFromPartialNamespace($partial_namespace) |
|
| 143 | + { |
|
| 144 | + if (empty($partial_namespace)) { |
|
| 145 | + throw new InvalidClassException($partial_namespace); |
|
| 146 | + } |
|
| 147 | + // load our PSR-4 Autoloader so we can get the list of registered namespaces from it |
|
| 148 | + $psr4_loader = \EE_Psr4AutoloaderInit::psr4_loader(); |
|
| 149 | + // breakup the incoming namespace into segments so we can loop thru them |
|
| 150 | + $namespace_segments = explode(Psr4Autoloader::NS, trim($partial_namespace, Psr4Autoloader::NS)); |
|
| 151 | + // we're only interested in the Vendor and secondary base, so pull those from the array |
|
| 152 | + $vendor_base = array_slice($namespace_segments, 0, 2); |
|
| 153 | + $namespace = $prefix = null; |
|
| 154 | + while (! empty($vendor_base)) { |
|
| 155 | + $namespace = implode(Psr4Autoloader::NS, $vendor_base); |
|
| 156 | + // check if there's a base directory registered for that namespace |
|
| 157 | + $prefix = $psr4_loader->prefixes($namespace . Psr4Autoloader::NS); |
|
| 158 | + if (! empty($prefix) && ! empty($prefix[0])) { |
|
| 159 | + // found one! |
|
| 160 | + break; |
|
| 161 | + } |
|
| 162 | + // remove base and try vendor only portion of namespace |
|
| 163 | + array_pop($vendor_base); |
|
| 164 | + } |
|
| 165 | + // nope? then the incoming namespace is invalid |
|
| 166 | + if (empty($prefix) || empty($prefix[0])) { |
|
| 167 | + throw new InvalidClassException($partial_namespace); |
|
| 168 | + } |
|
| 169 | + $this->setNamespace($namespace, $prefix[0]); |
|
| 170 | + // but if it's good, add that base directory to the rest of the path, and return it |
|
| 171 | + return $prefix[0] . implode(DS, array_diff($namespace_segments, $vendor_base)) . DS; |
|
| 172 | + } |
|
| 173 | 173 | } |
@@ -37,13 +37,13 @@ discard block |
||
| 37 | 37 | */ |
| 38 | 38 | protected function setNamespace($namespace, $namespace_base_dir) |
| 39 | 39 | { |
| 40 | - if (! is_string($namespace)) { |
|
| 40 | + if ( ! is_string($namespace)) { |
|
| 41 | 41 | throw new InvalidDataTypeException('$namespace', $namespace, 'string'); |
| 42 | 42 | } |
| 43 | - if (! is_string($namespace_base_dir)) { |
|
| 43 | + if ( ! is_string($namespace_base_dir)) { |
|
| 44 | 44 | throw new InvalidDataTypeException('$namespace_base_dir', $namespace_base_dir, 'string'); |
| 45 | 45 | } |
| 46 | - $this->namespaces[ $namespace ] = $namespace_base_dir; |
|
| 46 | + $this->namespaces[$namespace] = $namespace_base_dir; |
|
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | |
@@ -78,12 +78,12 @@ discard block |
||
| 78 | 78 | */ |
| 79 | 79 | public function locate($namespaces) |
| 80 | 80 | { |
| 81 | - if (! (is_string($namespaces) || is_array($namespaces))) { |
|
| 81 | + if ( ! (is_string($namespaces) || is_array($namespaces))) { |
|
| 82 | 82 | throw new InvalidDataTypeException('$namespaces', $namespaces, 'string or array'); |
| 83 | 83 | } |
| 84 | 84 | foreach ((array) $namespaces as $namespace) { |
| 85 | 85 | foreach ($this->findFQCNsByNamespace($namespace) as $key => $file) { |
| 86 | - $this->FQCNs[ $key ] = $file; |
|
| 86 | + $this->FQCNs[$key] = $file; |
|
| 87 | 87 | } |
| 88 | 88 | } |
| 89 | 89 | return $this->FQCNs; |
@@ -113,11 +113,11 @@ discard block |
||
| 113 | 113 | } |
| 114 | 114 | foreach ($iterator as $file) { |
| 115 | 115 | if ($file->isFile() && $file->getExtension() === 'php') { |
| 116 | - $file = $file->getPath() . DS . $file->getBasename('.php'); |
|
| 116 | + $file = $file->getPath().DS.$file->getBasename('.php'); |
|
| 117 | 117 | foreach ($this->namespaces as $namespace => $base_dir) { |
| 118 | 118 | $namespace .= Psr4Autoloader::NS; |
| 119 | 119 | if (strpos($file, $base_dir) === 0) { |
| 120 | - $this->FQCNs[] = Psr4Autoloader::NS . str_replace( |
|
| 120 | + $this->FQCNs[] = Psr4Autoloader::NS.str_replace( |
|
| 121 | 121 | array($base_dir, DS), |
| 122 | 122 | array($namespace, Psr4Autoloader::NS), |
| 123 | 123 | $file |
@@ -151,11 +151,11 @@ discard block |
||
| 151 | 151 | // we're only interested in the Vendor and secondary base, so pull those from the array |
| 152 | 152 | $vendor_base = array_slice($namespace_segments, 0, 2); |
| 153 | 153 | $namespace = $prefix = null; |
| 154 | - while (! empty($vendor_base)) { |
|
| 154 | + while ( ! empty($vendor_base)) { |
|
| 155 | 155 | $namespace = implode(Psr4Autoloader::NS, $vendor_base); |
| 156 | 156 | // check if there's a base directory registered for that namespace |
| 157 | - $prefix = $psr4_loader->prefixes($namespace . Psr4Autoloader::NS); |
|
| 158 | - if (! empty($prefix) && ! empty($prefix[0])) { |
|
| 157 | + $prefix = $psr4_loader->prefixes($namespace.Psr4Autoloader::NS); |
|
| 158 | + if ( ! empty($prefix) && ! empty($prefix[0])) { |
|
| 159 | 159 | // found one! |
| 160 | 160 | break; |
| 161 | 161 | } |
@@ -168,6 +168,6 @@ discard block |
||
| 168 | 168 | } |
| 169 | 169 | $this->setNamespace($namespace, $prefix[0]); |
| 170 | 170 | // but if it's good, add that base directory to the rest of the path, and return it |
| 171 | - return $prefix[0] . implode(DS, array_diff($namespace_segments, $vendor_base)) . DS; |
|
| 171 | + return $prefix[0].implode(DS, array_diff($namespace_segments, $vendor_base)).DS; |
|
| 172 | 172 | } |
| 173 | 173 | } |
@@ -12,121 +12,121 @@ |
||
| 12 | 12 | class EE_Register_Model_Extensions implements EEI_Plugin_API |
| 13 | 13 | { |
| 14 | 14 | |
| 15 | - protected static $_registry; |
|
| 16 | - protected static $_extensions = array(); |
|
| 15 | + protected static $_registry; |
|
| 16 | + protected static $_extensions = array(); |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * register method for setting up model extensions |
|
| 20 | - * |
|
| 21 | - * @param string $model_id unique id for the extensions being setup |
|
| 22 | - * @param array $config { |
|
| 23 | - * @throws EE_Error |
|
| 24 | - * @type array $model_extension_paths array of folders containing DB model extensions, where each file follows |
|
| 25 | - * the models naming convention, which is: EEME_{your_plugin_slug}_model_name_extended}.model_ext.php. Where |
|
| 26 | - * your_plugin_slug} is really anything you want (but something having to do with your addon, like |
|
| 27 | - * 'Calendar' or '3D_View') and model_name_extended} is the model extended. The class contained in teh file |
|
| 28 | - * should extend EEME_Base_{model_name_extended}.model_ext.php. Where {your_plugin_slug} is really anything |
|
| 29 | - * you want (but something having to do with your addon, like 'Calendar' or '3D_View') and |
|
| 30 | - * {model_name_extended} is the model extended. The class contained in teh file should extend EEME_Base |
|
| 31 | - * @type array $class_extension_paths array of folders containing DB class extensions, where each file follows |
|
| 32 | - * the model class extension naming convention, which is: |
|
| 33 | - * EEE_{your_plugin_slug}_model_name_extended}.class_ext.php. Where your_plugin_slug} is something like |
|
| 34 | - * 'Calendar','MailChimp',etc, and model_name_extended} is the name of the model extended, eg |
|
| 35 | - * 'Attendee','Event',etc. The class contained in the file should extend |
|
| 36 | - * EEE_Base_Class._{model_name_extended}.class_ext.php. Where {your_plugin_slug} is something like |
|
| 37 | - * 'Calendar','MailChimp',etc, and {model_name_extended} is the name of the model extended, eg |
|
| 38 | - * 'Attendee','Event',etc. The class contained in the file should extend EEE_Base_Class. |
|
| 39 | - * } |
|
| 40 | - * |
|
| 41 | - * @return void |
|
| 42 | - */ |
|
| 43 | - public static function register($model_id = null, $config = array()) |
|
| 44 | - { |
|
| 45 | - // required fields MUST be present, so let's make sure they are. |
|
| 46 | - if (empty($model_id) |
|
| 47 | - || ! is_array($config) |
|
| 48 | - || (empty($config['model_extension_paths']) && empty($config['class_extension_paths'])) |
|
| 49 | - ) { |
|
| 50 | - throw new EE_Error( |
|
| 51 | - __( |
|
| 52 | - 'In order to register Model extensions with EE_Register_Model_Extensions::register(), you must include a "model_id" (a unique identifier for this set of models), and an array containing the following keys: "model_extension_paths" (an array of full server paths to folders that contain model extensions), and "class_extension_paths" (an array of full server paths to folders that contain class extensions)', |
|
| 53 | - 'event_espresso' |
|
| 54 | - ) |
|
| 55 | - ); |
|
| 56 | - } |
|
| 18 | + /** |
|
| 19 | + * register method for setting up model extensions |
|
| 20 | + * |
|
| 21 | + * @param string $model_id unique id for the extensions being setup |
|
| 22 | + * @param array $config { |
|
| 23 | + * @throws EE_Error |
|
| 24 | + * @type array $model_extension_paths array of folders containing DB model extensions, where each file follows |
|
| 25 | + * the models naming convention, which is: EEME_{your_plugin_slug}_model_name_extended}.model_ext.php. Where |
|
| 26 | + * your_plugin_slug} is really anything you want (but something having to do with your addon, like |
|
| 27 | + * 'Calendar' or '3D_View') and model_name_extended} is the model extended. The class contained in teh file |
|
| 28 | + * should extend EEME_Base_{model_name_extended}.model_ext.php. Where {your_plugin_slug} is really anything |
|
| 29 | + * you want (but something having to do with your addon, like 'Calendar' or '3D_View') and |
|
| 30 | + * {model_name_extended} is the model extended. The class contained in teh file should extend EEME_Base |
|
| 31 | + * @type array $class_extension_paths array of folders containing DB class extensions, where each file follows |
|
| 32 | + * the model class extension naming convention, which is: |
|
| 33 | + * EEE_{your_plugin_slug}_model_name_extended}.class_ext.php. Where your_plugin_slug} is something like |
|
| 34 | + * 'Calendar','MailChimp',etc, and model_name_extended} is the name of the model extended, eg |
|
| 35 | + * 'Attendee','Event',etc. The class contained in the file should extend |
|
| 36 | + * EEE_Base_Class._{model_name_extended}.class_ext.php. Where {your_plugin_slug} is something like |
|
| 37 | + * 'Calendar','MailChimp',etc, and {model_name_extended} is the name of the model extended, eg |
|
| 38 | + * 'Attendee','Event',etc. The class contained in the file should extend EEE_Base_Class. |
|
| 39 | + * } |
|
| 40 | + * |
|
| 41 | + * @return void |
|
| 42 | + */ |
|
| 43 | + public static function register($model_id = null, $config = array()) |
|
| 44 | + { |
|
| 45 | + // required fields MUST be present, so let's make sure they are. |
|
| 46 | + if (empty($model_id) |
|
| 47 | + || ! is_array($config) |
|
| 48 | + || (empty($config['model_extension_paths']) && empty($config['class_extension_paths'])) |
|
| 49 | + ) { |
|
| 50 | + throw new EE_Error( |
|
| 51 | + __( |
|
| 52 | + 'In order to register Model extensions with EE_Register_Model_Extensions::register(), you must include a "model_id" (a unique identifier for this set of models), and an array containing the following keys: "model_extension_paths" (an array of full server paths to folders that contain model extensions), and "class_extension_paths" (an array of full server paths to folders that contain class extensions)', |
|
| 53 | + 'event_espresso' |
|
| 54 | + ) |
|
| 55 | + ); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - // make sure we don't register twice |
|
| 59 | - if (isset(self::$_registry[ $model_id ])) { |
|
| 60 | - return; |
|
| 61 | - } |
|
| 62 | - // check correct loading |
|
| 63 | - if (! did_action('AHEE__EE_System__load_espresso_addons') || did_action('AHEE__EE_Admin__loaded')) { |
|
| 64 | - EE_Error::doing_it_wrong( |
|
| 65 | - __METHOD__, |
|
| 66 | - sprintf( |
|
| 67 | - __( |
|
| 68 | - 'An attempt was made to register "%1$s" as a Model extension has failed because it was not registered at the correct time. Please use the "AHEE__EE_System__load_espresso_addons" hook to register models.%2$s Hook Status: %2$s "AHEE__EE_System__load_espresso_addons" : %3$s %2$s "AHEE__EE_Admin__loaded" : %4$s%2$s', |
|
| 69 | - 'event_espresso' |
|
| 70 | - ), |
|
| 71 | - $model_id, |
|
| 72 | - '<br />', |
|
| 73 | - did_action('AHEE__EE_System__load_espresso_addons') ? 'action done' : 'action NOT done', |
|
| 74 | - did_action('AHEE__EE_Admin__loaded') ? 'action done' : 'action NOT done' |
|
| 75 | - ), |
|
| 76 | - '4.3' |
|
| 77 | - ); |
|
| 78 | - } |
|
| 58 | + // make sure we don't register twice |
|
| 59 | + if (isset(self::$_registry[ $model_id ])) { |
|
| 60 | + return; |
|
| 61 | + } |
|
| 62 | + // check correct loading |
|
| 63 | + if (! did_action('AHEE__EE_System__load_espresso_addons') || did_action('AHEE__EE_Admin__loaded')) { |
|
| 64 | + EE_Error::doing_it_wrong( |
|
| 65 | + __METHOD__, |
|
| 66 | + sprintf( |
|
| 67 | + __( |
|
| 68 | + 'An attempt was made to register "%1$s" as a Model extension has failed because it was not registered at the correct time. Please use the "AHEE__EE_System__load_espresso_addons" hook to register models.%2$s Hook Status: %2$s "AHEE__EE_System__load_espresso_addons" : %3$s %2$s "AHEE__EE_Admin__loaded" : %4$s%2$s', |
|
| 69 | + 'event_espresso' |
|
| 70 | + ), |
|
| 71 | + $model_id, |
|
| 72 | + '<br />', |
|
| 73 | + did_action('AHEE__EE_System__load_espresso_addons') ? 'action done' : 'action NOT done', |
|
| 74 | + did_action('AHEE__EE_Admin__loaded') ? 'action done' : 'action NOT done' |
|
| 75 | + ), |
|
| 76 | + '4.3' |
|
| 77 | + ); |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - self::$_registry[ $model_id ] = $config; |
|
| 81 | - self::$_extensions[ $model_id ] = array(); |
|
| 80 | + self::$_registry[ $model_id ] = $config; |
|
| 81 | + self::$_extensions[ $model_id ] = array(); |
|
| 82 | 82 | |
| 83 | - if (isset($config['model_extension_paths'])) { |
|
| 84 | - require_once(EE_LIBRARIES . 'plugin_api/db/EEME_Base.lib.php'); |
|
| 85 | - $class_to_filepath_map = EEH_File::get_contents_of_folders($config['model_extension_paths']); |
|
| 86 | - // remove all files that are not PHP |
|
| 87 | - foreach ($class_to_filepath_map as $class => $path) { |
|
| 88 | - if (substr($path, strlen($path) - 3) !== 'php') { |
|
| 89 | - unset($class_to_filepath_map[ $class ]); |
|
| 90 | - continue; |
|
| 91 | - } |
|
| 92 | - } |
|
| 93 | - EEH_Autoloader::register_autoloader($class_to_filepath_map); |
|
| 94 | - foreach (array_keys($class_to_filepath_map) as $classname) { |
|
| 95 | - self::$_extensions[ $model_id ]['models'][ $classname ] = new $classname; |
|
| 96 | - } |
|
| 97 | - unset($config['model_extension_paths']); |
|
| 98 | - } |
|
| 99 | - if (isset($config['class_extension_paths'])) { |
|
| 100 | - require_once(EE_LIBRARIES . 'plugin_api/db/EEE_Base_Class.lib.php'); |
|
| 101 | - $class_to_filepath_map = EEH_File::get_contents_of_folders($config['class_extension_paths']); |
|
| 102 | - EEH_Autoloader::register_autoloader($class_to_filepath_map); |
|
| 103 | - foreach (array_keys($class_to_filepath_map) as $classname) { |
|
| 104 | - self::$_extensions[ $model_id ]['classes'][ $classname ] = new $classname; |
|
| 105 | - } |
|
| 106 | - unset($config['class_extension_paths']); |
|
| 107 | - } |
|
| 108 | - foreach ($config as $unknown_key => $unknown_config) { |
|
| 109 | - throw new EE_Error( |
|
| 110 | - sprintf(__("The key '%s' is not a known key for registering a model", "event_espresso"), $unknown_key) |
|
| 111 | - ); |
|
| 112 | - } |
|
| 113 | - } |
|
| 83 | + if (isset($config['model_extension_paths'])) { |
|
| 84 | + require_once(EE_LIBRARIES . 'plugin_api/db/EEME_Base.lib.php'); |
|
| 85 | + $class_to_filepath_map = EEH_File::get_contents_of_folders($config['model_extension_paths']); |
|
| 86 | + // remove all files that are not PHP |
|
| 87 | + foreach ($class_to_filepath_map as $class => $path) { |
|
| 88 | + if (substr($path, strlen($path) - 3) !== 'php') { |
|
| 89 | + unset($class_to_filepath_map[ $class ]); |
|
| 90 | + continue; |
|
| 91 | + } |
|
| 92 | + } |
|
| 93 | + EEH_Autoloader::register_autoloader($class_to_filepath_map); |
|
| 94 | + foreach (array_keys($class_to_filepath_map) as $classname) { |
|
| 95 | + self::$_extensions[ $model_id ]['models'][ $classname ] = new $classname; |
|
| 96 | + } |
|
| 97 | + unset($config['model_extension_paths']); |
|
| 98 | + } |
|
| 99 | + if (isset($config['class_extension_paths'])) { |
|
| 100 | + require_once(EE_LIBRARIES . 'plugin_api/db/EEE_Base_Class.lib.php'); |
|
| 101 | + $class_to_filepath_map = EEH_File::get_contents_of_folders($config['class_extension_paths']); |
|
| 102 | + EEH_Autoloader::register_autoloader($class_to_filepath_map); |
|
| 103 | + foreach (array_keys($class_to_filepath_map) as $classname) { |
|
| 104 | + self::$_extensions[ $model_id ]['classes'][ $classname ] = new $classname; |
|
| 105 | + } |
|
| 106 | + unset($config['class_extension_paths']); |
|
| 107 | + } |
|
| 108 | + foreach ($config as $unknown_key => $unknown_config) { |
|
| 109 | + throw new EE_Error( |
|
| 110 | + sprintf(__("The key '%s' is not a known key for registering a model", "event_espresso"), $unknown_key) |
|
| 111 | + ); |
|
| 112 | + } |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | 115 | |
| 116 | - /** |
|
| 117 | - * deregister |
|
| 118 | - * |
|
| 119 | - * @param string $model_id |
|
| 120 | - */ |
|
| 121 | - public static function deregister($model_id = null) |
|
| 122 | - { |
|
| 123 | - if (isset(self::$_registry[ $model_id ])) { |
|
| 124 | - unset(self::$_registry[ $model_id ]); |
|
| 125 | - foreach (self::$_extensions[ $model_id ] as $extension_of_type) { |
|
| 126 | - foreach ($extension_of_type as $extension) { |
|
| 127 | - $extension->deregister(); |
|
| 128 | - } |
|
| 129 | - } |
|
| 130 | - } |
|
| 131 | - } |
|
| 116 | + /** |
|
| 117 | + * deregister |
|
| 118 | + * |
|
| 119 | + * @param string $model_id |
|
| 120 | + */ |
|
| 121 | + public static function deregister($model_id = null) |
|
| 122 | + { |
|
| 123 | + if (isset(self::$_registry[ $model_id ])) { |
|
| 124 | + unset(self::$_registry[ $model_id ]); |
|
| 125 | + foreach (self::$_extensions[ $model_id ] as $extension_of_type) { |
|
| 126 | + foreach ($extension_of_type as $extension) { |
|
| 127 | + $extension->deregister(); |
|
| 128 | + } |
|
| 129 | + } |
|
| 130 | + } |
|
| 131 | + } |
|
| 132 | 132 | } |
@@ -56,11 +56,11 @@ discard block |
||
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | // make sure we don't register twice |
| 59 | - if (isset(self::$_registry[ $model_id ])) { |
|
| 59 | + if (isset(self::$_registry[$model_id])) { |
|
| 60 | 60 | return; |
| 61 | 61 | } |
| 62 | 62 | // check correct loading |
| 63 | - if (! did_action('AHEE__EE_System__load_espresso_addons') || did_action('AHEE__EE_Admin__loaded')) { |
|
| 63 | + if ( ! did_action('AHEE__EE_System__load_espresso_addons') || did_action('AHEE__EE_Admin__loaded')) { |
|
| 64 | 64 | EE_Error::doing_it_wrong( |
| 65 | 65 | __METHOD__, |
| 66 | 66 | sprintf( |
@@ -77,31 +77,31 @@ discard block |
||
| 77 | 77 | ); |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | - self::$_registry[ $model_id ] = $config; |
|
| 81 | - self::$_extensions[ $model_id ] = array(); |
|
| 80 | + self::$_registry[$model_id] = $config; |
|
| 81 | + self::$_extensions[$model_id] = array(); |
|
| 82 | 82 | |
| 83 | 83 | if (isset($config['model_extension_paths'])) { |
| 84 | - require_once(EE_LIBRARIES . 'plugin_api/db/EEME_Base.lib.php'); |
|
| 84 | + require_once(EE_LIBRARIES.'plugin_api/db/EEME_Base.lib.php'); |
|
| 85 | 85 | $class_to_filepath_map = EEH_File::get_contents_of_folders($config['model_extension_paths']); |
| 86 | 86 | // remove all files that are not PHP |
| 87 | 87 | foreach ($class_to_filepath_map as $class => $path) { |
| 88 | 88 | if (substr($path, strlen($path) - 3) !== 'php') { |
| 89 | - unset($class_to_filepath_map[ $class ]); |
|
| 89 | + unset($class_to_filepath_map[$class]); |
|
| 90 | 90 | continue; |
| 91 | 91 | } |
| 92 | 92 | } |
| 93 | 93 | EEH_Autoloader::register_autoloader($class_to_filepath_map); |
| 94 | 94 | foreach (array_keys($class_to_filepath_map) as $classname) { |
| 95 | - self::$_extensions[ $model_id ]['models'][ $classname ] = new $classname; |
|
| 95 | + self::$_extensions[$model_id]['models'][$classname] = new $classname; |
|
| 96 | 96 | } |
| 97 | 97 | unset($config['model_extension_paths']); |
| 98 | 98 | } |
| 99 | 99 | if (isset($config['class_extension_paths'])) { |
| 100 | - require_once(EE_LIBRARIES . 'plugin_api/db/EEE_Base_Class.lib.php'); |
|
| 100 | + require_once(EE_LIBRARIES.'plugin_api/db/EEE_Base_Class.lib.php'); |
|
| 101 | 101 | $class_to_filepath_map = EEH_File::get_contents_of_folders($config['class_extension_paths']); |
| 102 | 102 | EEH_Autoloader::register_autoloader($class_to_filepath_map); |
| 103 | 103 | foreach (array_keys($class_to_filepath_map) as $classname) { |
| 104 | - self::$_extensions[ $model_id ]['classes'][ $classname ] = new $classname; |
|
| 104 | + self::$_extensions[$model_id]['classes'][$classname] = new $classname; |
|
| 105 | 105 | } |
| 106 | 106 | unset($config['class_extension_paths']); |
| 107 | 107 | } |
@@ -120,9 +120,9 @@ discard block |
||
| 120 | 120 | */ |
| 121 | 121 | public static function deregister($model_id = null) |
| 122 | 122 | { |
| 123 | - if (isset(self::$_registry[ $model_id ])) { |
|
| 124 | - unset(self::$_registry[ $model_id ]); |
|
| 125 | - foreach (self::$_extensions[ $model_id ] as $extension_of_type) { |
|
| 123 | + if (isset(self::$_registry[$model_id])) { |
|
| 124 | + unset(self::$_registry[$model_id]); |
|
| 125 | + foreach (self::$_extensions[$model_id] as $extension_of_type) { |
|
| 126 | 126 | foreach ($extension_of_type as $extension) { |
| 127 | 127 | $extension->deregister(); |
| 128 | 128 | } |
@@ -17,189 +17,189 @@ |
||
| 17 | 17 | class EE_Select_Ajax_Model_Rest_Input extends EE_Form_Input_With_Options_Base |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * @var string $_model_name |
|
| 22 | - */ |
|
| 23 | - protected $_model_name; |
|
| 20 | + /** |
|
| 21 | + * @var string $_model_name |
|
| 22 | + */ |
|
| 23 | + protected $_model_name; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * @var string $_display_field_name |
|
| 27 | - */ |
|
| 28 | - protected $_display_field_name; |
|
| 25 | + /** |
|
| 26 | + * @var string $_display_field_name |
|
| 27 | + */ |
|
| 28 | + protected $_display_field_name; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * @var string $_value_field_name |
|
| 32 | - */ |
|
| 33 | - protected $_value_field_name; |
|
| 30 | + /** |
|
| 31 | + * @var string $_value_field_name |
|
| 32 | + */ |
|
| 33 | + protected $_value_field_name; |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * @var array $_extra_select_columns |
|
| 37 | - */ |
|
| 38 | - protected $_extra_select_columns = array(); |
|
| 35 | + /** |
|
| 36 | + * @var array $_extra_select_columns |
|
| 37 | + */ |
|
| 38 | + protected $_extra_select_columns = array(); |
|
| 39 | 39 | |
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * @param array $input_settings { |
|
| 43 | - * @type string $model_name the name of model to be used for searching, both via the REST API and server-side model queries |
|
| 44 | - * @type array $query_params default query parameters which will apply to both REST API queries and server-side queries. This should be |
|
| 45 | - * in the exact format that will be used for server-side model usage (eg use index 0 for where conditions, not |
|
| 46 | - * the string "where") |
|
| 47 | - * @type string $value_field_name the name of the model field on this model to |
|
| 48 | - * be used for the HTML select's option's values |
|
| 49 | - * @type string $display_field_name the name of the model field on this model |
|
| 50 | - * to be used for the HTML select's option's display text |
|
| 51 | - * @type array $select2_args arguments to be passed directly into the select2's JS constructor |
|
| 52 | - * } |
|
| 53 | - * And the arguments accepted by EE_Form_Input_With_Options_Base |
|
| 54 | - * } |
|
| 55 | - * @throws EE_Error |
|
| 56 | - * @throws InvalidArgumentException |
|
| 57 | - * @throws InvalidDataTypeException |
|
| 58 | - * @throws InvalidInterfaceException |
|
| 59 | - */ |
|
| 60 | - public function __construct($input_settings = array()) |
|
| 61 | - { |
|
| 62 | - // needed input settings: |
|
| 63 | - // select2_args |
|
| 64 | - $this->_model_name = EEH_Array::is_set( |
|
| 65 | - $input_settings, |
|
| 66 | - 'model_name', |
|
| 67 | - null |
|
| 68 | - ); |
|
| 69 | - $model = $this->_get_model(); |
|
| 70 | - $query_params = EEH_Array::is_set( |
|
| 71 | - $input_settings, |
|
| 72 | - 'query_params', |
|
| 73 | - array() |
|
| 74 | - ); |
|
| 75 | - // make sure limit and caps are always set |
|
| 76 | - $query_params = array_merge( |
|
| 77 | - array( 'limit' => 10, 'caps' => EEM_Base::caps_read_admin ), |
|
| 78 | - $query_params |
|
| 79 | - ); |
|
| 80 | - $this->_value_field_name = EEH_Array::is_set( |
|
| 81 | - $input_settings, |
|
| 82 | - 'value_field_name', |
|
| 83 | - $model->primary_key_name() |
|
| 84 | - ); |
|
| 85 | - $this->_display_field_name = EEH_Array::is_set( |
|
| 86 | - $input_settings, |
|
| 87 | - 'display_field_name', |
|
| 88 | - $model->get_a_field_of_type('EE_Text_Field_Base')->get_name() |
|
| 89 | - ); |
|
| 90 | - $this->_extra_select_columns = EEH_Array::is_set( |
|
| 91 | - $input_settings, |
|
| 92 | - 'extra_select_columns', |
|
| 93 | - array() |
|
| 94 | - ); |
|
| 95 | - $this->_add_validation_strategy( |
|
| 96 | - new EE_Model_Matching_Query_Validation_Strategy( |
|
| 97 | - '', |
|
| 98 | - $this->_model_name, |
|
| 99 | - $query_params, |
|
| 100 | - $this->_value_field_name |
|
| 101 | - ) |
|
| 102 | - ); |
|
| 103 | - // get resource endpoint |
|
| 104 | - $rest_controller = LoaderFactory::getLoader()->getNew( |
|
| 105 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Read' |
|
| 106 | - ); |
|
| 107 | - $rest_controller->setRequestedVersion(EED_Core_Rest_Api::latest_rest_api_version()); |
|
| 108 | - $default_select2_args = array( |
|
| 109 | - 'ajax' => array( |
|
| 110 | - 'url' => $rest_controller->getVersionedLinkTo( |
|
| 111 | - EEH_Inflector::pluralize_and_lower($this->_model_name) |
|
| 112 | - ), |
|
| 113 | - 'dataType' => 'json', |
|
| 114 | - 'delay' => '250', |
|
| 115 | - 'data_interface' => 'EE_Select2_REST_API_Interface', |
|
| 116 | - 'data_interface_args' => array( |
|
| 117 | - 'default_query_params' => (object) ModelDataTranslator::prepareQueryParamsForRestApi( |
|
| 118 | - $query_params, |
|
| 119 | - $model |
|
| 120 | - ), |
|
| 121 | - 'display_field' => $this->_display_field_name, |
|
| 122 | - 'value_field' => $this->_value_field_name, |
|
| 123 | - 'nonce' => wp_create_nonce('wp_rest'), |
|
| 124 | - 'locale' => str_replace('_', '-', strtolower(get_locale())) |
|
| 125 | - ), |
|
| 126 | - ), |
|
| 127 | - 'cache' => true, |
|
| 128 | - 'width' => 'resolve' |
|
| 129 | - ); |
|
| 130 | - $select2_args = array_replace_recursive( |
|
| 131 | - $default_select2_args, |
|
| 132 | - (array) EEH_Array::is_set($input_settings, 'select2_args', array()) |
|
| 133 | - ); |
|
| 134 | - $this->set_display_strategy(new EE_Select2_Display_Strategy($select2_args)); |
|
| 135 | - parent::__construct(array(), $input_settings); |
|
| 136 | - } |
|
| 41 | + /** |
|
| 42 | + * @param array $input_settings { |
|
| 43 | + * @type string $model_name the name of model to be used for searching, both via the REST API and server-side model queries |
|
| 44 | + * @type array $query_params default query parameters which will apply to both REST API queries and server-side queries. This should be |
|
| 45 | + * in the exact format that will be used for server-side model usage (eg use index 0 for where conditions, not |
|
| 46 | + * the string "where") |
|
| 47 | + * @type string $value_field_name the name of the model field on this model to |
|
| 48 | + * be used for the HTML select's option's values |
|
| 49 | + * @type string $display_field_name the name of the model field on this model |
|
| 50 | + * to be used for the HTML select's option's display text |
|
| 51 | + * @type array $select2_args arguments to be passed directly into the select2's JS constructor |
|
| 52 | + * } |
|
| 53 | + * And the arguments accepted by EE_Form_Input_With_Options_Base |
|
| 54 | + * } |
|
| 55 | + * @throws EE_Error |
|
| 56 | + * @throws InvalidArgumentException |
|
| 57 | + * @throws InvalidDataTypeException |
|
| 58 | + * @throws InvalidInterfaceException |
|
| 59 | + */ |
|
| 60 | + public function __construct($input_settings = array()) |
|
| 61 | + { |
|
| 62 | + // needed input settings: |
|
| 63 | + // select2_args |
|
| 64 | + $this->_model_name = EEH_Array::is_set( |
|
| 65 | + $input_settings, |
|
| 66 | + 'model_name', |
|
| 67 | + null |
|
| 68 | + ); |
|
| 69 | + $model = $this->_get_model(); |
|
| 70 | + $query_params = EEH_Array::is_set( |
|
| 71 | + $input_settings, |
|
| 72 | + 'query_params', |
|
| 73 | + array() |
|
| 74 | + ); |
|
| 75 | + // make sure limit and caps are always set |
|
| 76 | + $query_params = array_merge( |
|
| 77 | + array( 'limit' => 10, 'caps' => EEM_Base::caps_read_admin ), |
|
| 78 | + $query_params |
|
| 79 | + ); |
|
| 80 | + $this->_value_field_name = EEH_Array::is_set( |
|
| 81 | + $input_settings, |
|
| 82 | + 'value_field_name', |
|
| 83 | + $model->primary_key_name() |
|
| 84 | + ); |
|
| 85 | + $this->_display_field_name = EEH_Array::is_set( |
|
| 86 | + $input_settings, |
|
| 87 | + 'display_field_name', |
|
| 88 | + $model->get_a_field_of_type('EE_Text_Field_Base')->get_name() |
|
| 89 | + ); |
|
| 90 | + $this->_extra_select_columns = EEH_Array::is_set( |
|
| 91 | + $input_settings, |
|
| 92 | + 'extra_select_columns', |
|
| 93 | + array() |
|
| 94 | + ); |
|
| 95 | + $this->_add_validation_strategy( |
|
| 96 | + new EE_Model_Matching_Query_Validation_Strategy( |
|
| 97 | + '', |
|
| 98 | + $this->_model_name, |
|
| 99 | + $query_params, |
|
| 100 | + $this->_value_field_name |
|
| 101 | + ) |
|
| 102 | + ); |
|
| 103 | + // get resource endpoint |
|
| 104 | + $rest_controller = LoaderFactory::getLoader()->getNew( |
|
| 105 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Read' |
|
| 106 | + ); |
|
| 107 | + $rest_controller->setRequestedVersion(EED_Core_Rest_Api::latest_rest_api_version()); |
|
| 108 | + $default_select2_args = array( |
|
| 109 | + 'ajax' => array( |
|
| 110 | + 'url' => $rest_controller->getVersionedLinkTo( |
|
| 111 | + EEH_Inflector::pluralize_and_lower($this->_model_name) |
|
| 112 | + ), |
|
| 113 | + 'dataType' => 'json', |
|
| 114 | + 'delay' => '250', |
|
| 115 | + 'data_interface' => 'EE_Select2_REST_API_Interface', |
|
| 116 | + 'data_interface_args' => array( |
|
| 117 | + 'default_query_params' => (object) ModelDataTranslator::prepareQueryParamsForRestApi( |
|
| 118 | + $query_params, |
|
| 119 | + $model |
|
| 120 | + ), |
|
| 121 | + 'display_field' => $this->_display_field_name, |
|
| 122 | + 'value_field' => $this->_value_field_name, |
|
| 123 | + 'nonce' => wp_create_nonce('wp_rest'), |
|
| 124 | + 'locale' => str_replace('_', '-', strtolower(get_locale())) |
|
| 125 | + ), |
|
| 126 | + ), |
|
| 127 | + 'cache' => true, |
|
| 128 | + 'width' => 'resolve' |
|
| 129 | + ); |
|
| 130 | + $select2_args = array_replace_recursive( |
|
| 131 | + $default_select2_args, |
|
| 132 | + (array) EEH_Array::is_set($input_settings, 'select2_args', array()) |
|
| 133 | + ); |
|
| 134 | + $this->set_display_strategy(new EE_Select2_Display_Strategy($select2_args)); |
|
| 135 | + parent::__construct(array(), $input_settings); |
|
| 136 | + } |
|
| 137 | 137 | |
| 138 | 138 | |
| 139 | 139 | |
| 140 | - /** |
|
| 141 | - * Before setting the raw value (usually because we're setting the default, |
|
| 142 | - * or we've received a form submission and this might be re-displayed to the user), |
|
| 143 | - * sets the options so that the current selections appear on initial display. |
|
| 144 | - * |
|
| 145 | - * Note: because this input uses EE_Model_Matching_Query_Validation_Strategy |
|
| 146 | - * for validation, this input's options only affect DISPLAY and NOT validation, |
|
| 147 | - * which is why its ok to just assume the provided $value to be in the list of acceptable values |
|
| 148 | - * |
|
| 149 | - * @param mixed $value |
|
| 150 | - * @return void |
|
| 151 | - * @throws \EE_Error |
|
| 152 | - */ |
|
| 153 | - public function _set_raw_value($value) |
|
| 154 | - { |
|
| 155 | - $values_for_options = (array) $value; |
|
| 156 | - $value_field = $this->_get_model()->field_settings_for($this->_value_field_name); |
|
| 157 | - $display_field = $this->_get_model()->field_settings_for($this->_display_field_name); |
|
| 158 | - $this->_extra_select_columns[] = $value_field->get_qualified_column() . ' AS ' . $this->_value_field_name; |
|
| 159 | - $this->_extra_select_columns[] = $display_field->get_qualified_column() . ' AS ' . $this->_display_field_name; |
|
| 160 | - $display_values = $this->_get_model()->get_all_wpdb_results( |
|
| 161 | - array( |
|
| 162 | - array( |
|
| 163 | - $this->_value_field_name => array( 'IN', $values_for_options ) |
|
| 164 | - ) |
|
| 165 | - ), |
|
| 166 | - ARRAY_A, |
|
| 167 | - implode(',', $this->_extra_select_columns) |
|
| 168 | - ); |
|
| 169 | - $select_options = array(); |
|
| 170 | - if (is_array($select_options)) { |
|
| 171 | - foreach ($display_values as $db_rows) { |
|
| 172 | - $db_rows = (array) $db_rows; |
|
| 173 | - $select_options[ $db_rows[ $this->_value_field_name ] ] = apply_filters( |
|
| 174 | - 'FHEE__EE_Select_Ajax_Model_Rest_Input___set_raw_value__select_option_value', |
|
| 175 | - $db_rows[ $this->_display_field_name ], |
|
| 176 | - $db_rows |
|
| 177 | - ); |
|
| 178 | - } |
|
| 179 | - } |
|
| 180 | - $this->set_select_options($select_options); |
|
| 181 | - parent::_set_raw_value($value); |
|
| 182 | - } |
|
| 140 | + /** |
|
| 141 | + * Before setting the raw value (usually because we're setting the default, |
|
| 142 | + * or we've received a form submission and this might be re-displayed to the user), |
|
| 143 | + * sets the options so that the current selections appear on initial display. |
|
| 144 | + * |
|
| 145 | + * Note: because this input uses EE_Model_Matching_Query_Validation_Strategy |
|
| 146 | + * for validation, this input's options only affect DISPLAY and NOT validation, |
|
| 147 | + * which is why its ok to just assume the provided $value to be in the list of acceptable values |
|
| 148 | + * |
|
| 149 | + * @param mixed $value |
|
| 150 | + * @return void |
|
| 151 | + * @throws \EE_Error |
|
| 152 | + */ |
|
| 153 | + public function _set_raw_value($value) |
|
| 154 | + { |
|
| 155 | + $values_for_options = (array) $value; |
|
| 156 | + $value_field = $this->_get_model()->field_settings_for($this->_value_field_name); |
|
| 157 | + $display_field = $this->_get_model()->field_settings_for($this->_display_field_name); |
|
| 158 | + $this->_extra_select_columns[] = $value_field->get_qualified_column() . ' AS ' . $this->_value_field_name; |
|
| 159 | + $this->_extra_select_columns[] = $display_field->get_qualified_column() . ' AS ' . $this->_display_field_name; |
|
| 160 | + $display_values = $this->_get_model()->get_all_wpdb_results( |
|
| 161 | + array( |
|
| 162 | + array( |
|
| 163 | + $this->_value_field_name => array( 'IN', $values_for_options ) |
|
| 164 | + ) |
|
| 165 | + ), |
|
| 166 | + ARRAY_A, |
|
| 167 | + implode(',', $this->_extra_select_columns) |
|
| 168 | + ); |
|
| 169 | + $select_options = array(); |
|
| 170 | + if (is_array($select_options)) { |
|
| 171 | + foreach ($display_values as $db_rows) { |
|
| 172 | + $db_rows = (array) $db_rows; |
|
| 173 | + $select_options[ $db_rows[ $this->_value_field_name ] ] = apply_filters( |
|
| 174 | + 'FHEE__EE_Select_Ajax_Model_Rest_Input___set_raw_value__select_option_value', |
|
| 175 | + $db_rows[ $this->_display_field_name ], |
|
| 176 | + $db_rows |
|
| 177 | + ); |
|
| 178 | + } |
|
| 179 | + } |
|
| 180 | + $this->set_select_options($select_options); |
|
| 181 | + parent::_set_raw_value($value); |
|
| 182 | + } |
|
| 183 | 183 | |
| 184 | - /** |
|
| 185 | - * Returns the model, or throws an exception if the model name provided in constructor doesn't exist |
|
| 186 | - * @return EEM_Base |
|
| 187 | - * @throws EE_Error |
|
| 188 | - */ |
|
| 189 | - protected function _get_model() |
|
| 190 | - { |
|
| 191 | - if (! EE_Registry::instance()->is_model_name($this->_model_name)) { |
|
| 192 | - throw new EE_Error( |
|
| 193 | - sprintf( |
|
| 194 | - __( |
|
| 195 | - '%1$s is not a proper model name. Please provide a model name in the "model_name" form input argument', |
|
| 196 | - 'event_espresso' |
|
| 197 | - ), |
|
| 198 | - $this->_model_name |
|
| 199 | - ) |
|
| 200 | - ); |
|
| 201 | - } else { |
|
| 202 | - return EE_Registry::instance()->load_model($this->_model_name); |
|
| 203 | - } |
|
| 204 | - } |
|
| 184 | + /** |
|
| 185 | + * Returns the model, or throws an exception if the model name provided in constructor doesn't exist |
|
| 186 | + * @return EEM_Base |
|
| 187 | + * @throws EE_Error |
|
| 188 | + */ |
|
| 189 | + protected function _get_model() |
|
| 190 | + { |
|
| 191 | + if (! EE_Registry::instance()->is_model_name($this->_model_name)) { |
|
| 192 | + throw new EE_Error( |
|
| 193 | + sprintf( |
|
| 194 | + __( |
|
| 195 | + '%1$s is not a proper model name. Please provide a model name in the "model_name" form input argument', |
|
| 196 | + 'event_espresso' |
|
| 197 | + ), |
|
| 198 | + $this->_model_name |
|
| 199 | + ) |
|
| 200 | + ); |
|
| 201 | + } else { |
|
| 202 | + return EE_Registry::instance()->load_model($this->_model_name); |
|
| 203 | + } |
|
| 204 | + } |
|
| 205 | 205 | } |
@@ -14,78 +14,78 @@ |
||
| 14 | 14 | class EE_Select_Display_Strategy extends EE_Display_Strategy_Base |
| 15 | 15 | { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * |
|
| 19 | - * @throws EE_Error |
|
| 20 | - * @return string of html to display the field |
|
| 21 | - */ |
|
| 22 | - public function display() |
|
| 23 | - { |
|
| 24 | - if (! $this->_input instanceof EE_Form_Input_With_Options_Base) { |
|
| 25 | - throw new EE_Error(sprintf(__('Cannot use Select Display Strategy with an input that doesn\'t have options', 'event_espresso'))); |
|
| 26 | - } |
|
| 17 | + /** |
|
| 18 | + * |
|
| 19 | + * @throws EE_Error |
|
| 20 | + * @return string of html to display the field |
|
| 21 | + */ |
|
| 22 | + public function display() |
|
| 23 | + { |
|
| 24 | + if (! $this->_input instanceof EE_Form_Input_With_Options_Base) { |
|
| 25 | + throw new EE_Error(sprintf(__('Cannot use Select Display Strategy with an input that doesn\'t have options', 'event_espresso'))); |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - $html = EEH_HTML::nl(0, 'select'); |
|
| 29 | - $html .= '<select'; |
|
| 30 | - $html .= $this->_attributes_string( |
|
| 31 | - $this->_standard_attributes_array() |
|
| 32 | - ); |
|
| 33 | - $html .= '>'; |
|
| 28 | + $html = EEH_HTML::nl(0, 'select'); |
|
| 29 | + $html .= '<select'; |
|
| 30 | + $html .= $this->_attributes_string( |
|
| 31 | + $this->_standard_attributes_array() |
|
| 32 | + ); |
|
| 33 | + $html .= '>'; |
|
| 34 | 34 | |
| 35 | - if (EEH_Array::is_multi_dimensional_array($this->_input->options())) { |
|
| 36 | - EEH_HTML::indent(1, 'optgroup'); |
|
| 37 | - foreach ($this->_input->options() as $opt_group_label => $opt_group) { |
|
| 38 | - if (! empty($opt_group_label)) { |
|
| 39 | - $html .= EEH_HTML::nl(0, 'optgroup') . '<optgroup label="' . esc_attr($opt_group_label) . '">'; |
|
| 40 | - } |
|
| 41 | - EEH_HTML::indent(1, 'option'); |
|
| 42 | - $html .= $this->_display_options($opt_group); |
|
| 43 | - EEH_HTML::indent(-1, 'option'); |
|
| 44 | - if (! empty($opt_group_label)) { |
|
| 45 | - $html .= EEH_HTML::nl(0, 'optgroup') . '</optgroup>'; |
|
| 46 | - } |
|
| 47 | - } |
|
| 48 | - EEH_HTML::indent(-1, 'optgroup'); |
|
| 49 | - } else { |
|
| 50 | - $html.=$this->_display_options($this->_input->options()); |
|
| 51 | - } |
|
| 35 | + if (EEH_Array::is_multi_dimensional_array($this->_input->options())) { |
|
| 36 | + EEH_HTML::indent(1, 'optgroup'); |
|
| 37 | + foreach ($this->_input->options() as $opt_group_label => $opt_group) { |
|
| 38 | + if (! empty($opt_group_label)) { |
|
| 39 | + $html .= EEH_HTML::nl(0, 'optgroup') . '<optgroup label="' . esc_attr($opt_group_label) . '">'; |
|
| 40 | + } |
|
| 41 | + EEH_HTML::indent(1, 'option'); |
|
| 42 | + $html .= $this->_display_options($opt_group); |
|
| 43 | + EEH_HTML::indent(-1, 'option'); |
|
| 44 | + if (! empty($opt_group_label)) { |
|
| 45 | + $html .= EEH_HTML::nl(0, 'optgroup') . '</optgroup>'; |
|
| 46 | + } |
|
| 47 | + } |
|
| 48 | + EEH_HTML::indent(-1, 'optgroup'); |
|
| 49 | + } else { |
|
| 50 | + $html.=$this->_display_options($this->_input->options()); |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - $html.= EEH_HTML::nl(0, 'select') . '</select>'; |
|
| 54 | - return $html; |
|
| 55 | - } |
|
| 53 | + $html.= EEH_HTML::nl(0, 'select') . '</select>'; |
|
| 54 | + return $html; |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | 57 | |
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Displays a flat list of options as option tags |
|
| 61 | - * @param array $options |
|
| 62 | - * @return string |
|
| 63 | - */ |
|
| 64 | - protected function _display_options($options) |
|
| 65 | - { |
|
| 66 | - $html = ''; |
|
| 67 | - EEH_HTML::indent(1, 'option'); |
|
| 68 | - foreach ($options as $value => $display_text) { |
|
| 69 | - // even if this input uses EE_Text_Normalization if one of the array keys is a numeric string, like "123", |
|
| 70 | - // PHP will have converted it to a PHP integer (eg 123). So we need to make sure it's a string |
|
| 71 | - $unnormalized_value = $this->_input->get_normalization_strategy()->unnormalize_one($value); |
|
| 72 | - $selected = $this->_check_if_option_selected($unnormalized_value) ? ' selected="selected"' : ''; |
|
| 73 | - $html.= EEH_HTML::nl(0, 'option') . '<option value="' . esc_attr($unnormalized_value) . '"' . $selected . '>' . $display_text . '</option>'; |
|
| 74 | - } |
|
| 75 | - EEH_HTML::indent(-1, 'option'); |
|
| 76 | - return $html; |
|
| 77 | - } |
|
| 59 | + /** |
|
| 60 | + * Displays a flat list of options as option tags |
|
| 61 | + * @param array $options |
|
| 62 | + * @return string |
|
| 63 | + */ |
|
| 64 | + protected function _display_options($options) |
|
| 65 | + { |
|
| 66 | + $html = ''; |
|
| 67 | + EEH_HTML::indent(1, 'option'); |
|
| 68 | + foreach ($options as $value => $display_text) { |
|
| 69 | + // even if this input uses EE_Text_Normalization if one of the array keys is a numeric string, like "123", |
|
| 70 | + // PHP will have converted it to a PHP integer (eg 123). So we need to make sure it's a string |
|
| 71 | + $unnormalized_value = $this->_input->get_normalization_strategy()->unnormalize_one($value); |
|
| 72 | + $selected = $this->_check_if_option_selected($unnormalized_value) ? ' selected="selected"' : ''; |
|
| 73 | + $html.= EEH_HTML::nl(0, 'option') . '<option value="' . esc_attr($unnormalized_value) . '"' . $selected . '>' . $display_text . '</option>'; |
|
| 74 | + } |
|
| 75 | + EEH_HTML::indent(-1, 'option'); |
|
| 76 | + return $html; |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | 79 | |
| 80 | 80 | |
| 81 | - /** |
|
| 82 | - * Checks if that value is the one selected |
|
| 83 | - * |
|
| 84 | - * @param string|int $option_value unnormalized value option (string). How it will appear in the HTML. |
|
| 85 | - * @return string |
|
| 86 | - */ |
|
| 87 | - protected function _check_if_option_selected($option_value) |
|
| 88 | - { |
|
| 89 | - return $option_value === $this->_input->raw_value(); |
|
| 90 | - } |
|
| 81 | + /** |
|
| 82 | + * Checks if that value is the one selected |
|
| 83 | + * |
|
| 84 | + * @param string|int $option_value unnormalized value option (string). How it will appear in the HTML. |
|
| 85 | + * @return string |
|
| 86 | + */ |
|
| 87 | + protected function _check_if_option_selected($option_value) |
|
| 88 | + { |
|
| 89 | + return $option_value === $this->_input->raw_value(); |
|
| 90 | + } |
|
| 91 | 91 | } |
@@ -21,7 +21,7 @@ discard block |
||
| 21 | 21 | */ |
| 22 | 22 | public function display() |
| 23 | 23 | { |
| 24 | - if (! $this->_input instanceof EE_Form_Input_With_Options_Base) { |
|
| 24 | + if ( ! $this->_input instanceof EE_Form_Input_With_Options_Base) { |
|
| 25 | 25 | throw new EE_Error(sprintf(__('Cannot use Select Display Strategy with an input that doesn\'t have options', 'event_espresso'))); |
| 26 | 26 | } |
| 27 | 27 | |
@@ -35,22 +35,22 @@ discard block |
||
| 35 | 35 | if (EEH_Array::is_multi_dimensional_array($this->_input->options())) { |
| 36 | 36 | EEH_HTML::indent(1, 'optgroup'); |
| 37 | 37 | foreach ($this->_input->options() as $opt_group_label => $opt_group) { |
| 38 | - if (! empty($opt_group_label)) { |
|
| 39 | - $html .= EEH_HTML::nl(0, 'optgroup') . '<optgroup label="' . esc_attr($opt_group_label) . '">'; |
|
| 38 | + if ( ! empty($opt_group_label)) { |
|
| 39 | + $html .= EEH_HTML::nl(0, 'optgroup').'<optgroup label="'.esc_attr($opt_group_label).'">'; |
|
| 40 | 40 | } |
| 41 | 41 | EEH_HTML::indent(1, 'option'); |
| 42 | 42 | $html .= $this->_display_options($opt_group); |
| 43 | 43 | EEH_HTML::indent(-1, 'option'); |
| 44 | - if (! empty($opt_group_label)) { |
|
| 45 | - $html .= EEH_HTML::nl(0, 'optgroup') . '</optgroup>'; |
|
| 44 | + if ( ! empty($opt_group_label)) { |
|
| 45 | + $html .= EEH_HTML::nl(0, 'optgroup').'</optgroup>'; |
|
| 46 | 46 | } |
| 47 | 47 | } |
| 48 | 48 | EEH_HTML::indent(-1, 'optgroup'); |
| 49 | 49 | } else { |
| 50 | - $html.=$this->_display_options($this->_input->options()); |
|
| 50 | + $html .= $this->_display_options($this->_input->options()); |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | - $html.= EEH_HTML::nl(0, 'select') . '</select>'; |
|
| 53 | + $html .= EEH_HTML::nl(0, 'select').'</select>'; |
|
| 54 | 54 | return $html; |
| 55 | 55 | } |
| 56 | 56 | |
@@ -70,7 +70,7 @@ discard block |
||
| 70 | 70 | // PHP will have converted it to a PHP integer (eg 123). So we need to make sure it's a string |
| 71 | 71 | $unnormalized_value = $this->_input->get_normalization_strategy()->unnormalize_one($value); |
| 72 | 72 | $selected = $this->_check_if_option_selected($unnormalized_value) ? ' selected="selected"' : ''; |
| 73 | - $html.= EEH_HTML::nl(0, 'option') . '<option value="' . esc_attr($unnormalized_value) . '"' . $selected . '>' . $display_text . '</option>'; |
|
| 73 | + $html .= EEH_HTML::nl(0, 'option').'<option value="'.esc_attr($unnormalized_value).'"'.$selected.'>'.$display_text.'</option>'; |
|
| 74 | 74 | } |
| 75 | 75 | EEH_HTML::indent(-1, 'option'); |
| 76 | 76 | return $html; |
@@ -15,80 +15,80 @@ |
||
| 15 | 15 | class EE_Select2_Display_Strategy extends EE_Select_Display_Strategy |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Arguments that will be passed into the select2 javascript constructor |
|
| 20 | - * @var array |
|
| 21 | - */ |
|
| 22 | - protected $_select2_js_args = array(); |
|
| 18 | + /** |
|
| 19 | + * Arguments that will be passed into the select2 javascript constructor |
|
| 20 | + * @var array |
|
| 21 | + */ |
|
| 22 | + protected $_select2_js_args = array(); |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * |
|
| 26 | - * @param array $select2_js_args pass in the EXACT array of JS arguments you want |
|
| 27 | - * to pass into the select2 js/html input. See https://select2.github.io |
|
| 28 | - */ |
|
| 29 | - public function __construct($select2_js_args = array()) |
|
| 30 | - { |
|
| 31 | - $this->_select2_js_args = $select2_js_args; |
|
| 32 | - parent::__construct(); |
|
| 33 | - } |
|
| 24 | + /** |
|
| 25 | + * |
|
| 26 | + * @param array $select2_js_args pass in the EXACT array of JS arguments you want |
|
| 27 | + * to pass into the select2 js/html input. See https://select2.github.io |
|
| 28 | + */ |
|
| 29 | + public function __construct($select2_js_args = array()) |
|
| 30 | + { |
|
| 31 | + $this->_select2_js_args = $select2_js_args; |
|
| 32 | + parent::__construct(); |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * enqueues the select2 initializing js (which depends on the select2 js) and |
|
| 37 | - * the select2 css |
|
| 38 | - */ |
|
| 39 | - public function enqueue_js() |
|
| 40 | - { |
|
| 41 | - // need to first deregister the select2 script in case some other plugin **cough cough Toolset Types cough** |
|
| 42 | - // is carelessly registering an older version of Select2 on admin pages that don't even belong to them |
|
| 43 | - wp_deregister_script('select2'); |
|
| 44 | - wp_deregister_style('select2'); |
|
| 45 | - wp_register_script('select2', EE_GLOBAL_ASSETS_URL . 'scripts/select2.min.js', array(), '4.0.2', true); |
|
| 46 | - wp_register_style('select2', EE_GLOBAL_ASSETS_URL . 'css/select2.min.css', array(), '4.0.2', 'all'); |
|
| 47 | - wp_enqueue_script('form_section_select2_init', EE_GLOBAL_ASSETS_URL . 'scripts/form_section_select2_init.js', array( 'select2' ), '1.0.0', true); |
|
| 48 | - wp_enqueue_style('select2', EE_GLOBAL_ASSETS_URL . 'css/select2.min.css', array(), '4.0.2', 'all'); |
|
| 49 | - } |
|
| 35 | + /** |
|
| 36 | + * enqueues the select2 initializing js (which depends on the select2 js) and |
|
| 37 | + * the select2 css |
|
| 38 | + */ |
|
| 39 | + public function enqueue_js() |
|
| 40 | + { |
|
| 41 | + // need to first deregister the select2 script in case some other plugin **cough cough Toolset Types cough** |
|
| 42 | + // is carelessly registering an older version of Select2 on admin pages that don't even belong to them |
|
| 43 | + wp_deregister_script('select2'); |
|
| 44 | + wp_deregister_style('select2'); |
|
| 45 | + wp_register_script('select2', EE_GLOBAL_ASSETS_URL . 'scripts/select2.min.js', array(), '4.0.2', true); |
|
| 46 | + wp_register_style('select2', EE_GLOBAL_ASSETS_URL . 'css/select2.min.css', array(), '4.0.2', 'all'); |
|
| 47 | + wp_enqueue_script('form_section_select2_init', EE_GLOBAL_ASSETS_URL . 'scripts/form_section_select2_init.js', array( 'select2' ), '1.0.0', true); |
|
| 48 | + wp_enqueue_style('select2', EE_GLOBAL_ASSETS_URL . 'css/select2.min.css', array(), '4.0.2', 'all'); |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * Gets the javascript args which will be localized and passed into the select2 js/html input |
|
| 53 | - * @return array |
|
| 54 | - */ |
|
| 55 | - public function get_js_args() |
|
| 56 | - { |
|
| 57 | - return $this->_select2_js_args; |
|
| 58 | - } |
|
| 51 | + /** |
|
| 52 | + * Gets the javascript args which will be localized and passed into the select2 js/html input |
|
| 53 | + * @return array |
|
| 54 | + */ |
|
| 55 | + public function get_js_args() |
|
| 56 | + { |
|
| 57 | + return $this->_select2_js_args; |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Sets the exact js args which will be passed into the select2 js/html input |
|
| 62 | - * @param array $js_args |
|
| 63 | - */ |
|
| 64 | - public function set_js_args($js_args) |
|
| 65 | - { |
|
| 66 | - $this->_select2_js_args = $js_args; |
|
| 67 | - } |
|
| 60 | + /** |
|
| 61 | + * Sets the exact js args which will be passed into the select2 js/html input |
|
| 62 | + * @param array $js_args |
|
| 63 | + */ |
|
| 64 | + public function set_js_args($js_args) |
|
| 65 | + { |
|
| 66 | + $this->_select2_js_args = $js_args; |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * Adds select2 data for localization |
|
| 71 | - * @param array $other_js_data |
|
| 72 | - * @return array |
|
| 73 | - */ |
|
| 74 | - public function get_other_js_data($other_js_data = array()) |
|
| 75 | - { |
|
| 76 | - $other_js_data = parent::get_other_js_data($other_js_data); |
|
| 77 | - if (! isset($other_js_data['select2s'])) { |
|
| 78 | - $other_js_data['select2s'] = array(); |
|
| 79 | - } |
|
| 80 | - $other_js_data['select2s'][ $this->_input->html_id() ] = $this->get_js_args(); |
|
| 81 | - return $other_js_data; |
|
| 82 | - } |
|
| 69 | + /** |
|
| 70 | + * Adds select2 data for localization |
|
| 71 | + * @param array $other_js_data |
|
| 72 | + * @return array |
|
| 73 | + */ |
|
| 74 | + public function get_other_js_data($other_js_data = array()) |
|
| 75 | + { |
|
| 76 | + $other_js_data = parent::get_other_js_data($other_js_data); |
|
| 77 | + if (! isset($other_js_data['select2s'])) { |
|
| 78 | + $other_js_data['select2s'] = array(); |
|
| 79 | + } |
|
| 80 | + $other_js_data['select2s'][ $this->_input->html_id() ] = $this->get_js_args(); |
|
| 81 | + return $other_js_data; |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * Overrides standard attributes array to add the CSS class "ee-select2" |
|
| 86 | - * @return array |
|
| 87 | - */ |
|
| 88 | - protected function _standard_attributes_array() |
|
| 89 | - { |
|
| 90 | - $standard_attributes = parent::_standard_attributes_array(); |
|
| 91 | - $standard_attributes['class'] .= ' ee-select2'; |
|
| 92 | - return $standard_attributes; |
|
| 93 | - } |
|
| 84 | + /** |
|
| 85 | + * Overrides standard attributes array to add the CSS class "ee-select2" |
|
| 86 | + * @return array |
|
| 87 | + */ |
|
| 88 | + protected function _standard_attributes_array() |
|
| 89 | + { |
|
| 90 | + $standard_attributes = parent::_standard_attributes_array(); |
|
| 91 | + $standard_attributes['class'] .= ' ee-select2'; |
|
| 92 | + return $standard_attributes; |
|
| 93 | + } |
|
| 94 | 94 | } |
@@ -42,10 +42,10 @@ discard block |
||
| 42 | 42 | // is carelessly registering an older version of Select2 on admin pages that don't even belong to them |
| 43 | 43 | wp_deregister_script('select2'); |
| 44 | 44 | wp_deregister_style('select2'); |
| 45 | - wp_register_script('select2', EE_GLOBAL_ASSETS_URL . 'scripts/select2.min.js', array(), '4.0.2', true); |
|
| 46 | - wp_register_style('select2', EE_GLOBAL_ASSETS_URL . 'css/select2.min.css', array(), '4.0.2', 'all'); |
|
| 47 | - wp_enqueue_script('form_section_select2_init', EE_GLOBAL_ASSETS_URL . 'scripts/form_section_select2_init.js', array( 'select2' ), '1.0.0', true); |
|
| 48 | - wp_enqueue_style('select2', EE_GLOBAL_ASSETS_URL . 'css/select2.min.css', array(), '4.0.2', 'all'); |
|
| 45 | + wp_register_script('select2', EE_GLOBAL_ASSETS_URL.'scripts/select2.min.js', array(), '4.0.2', true); |
|
| 46 | + wp_register_style('select2', EE_GLOBAL_ASSETS_URL.'css/select2.min.css', array(), '4.0.2', 'all'); |
|
| 47 | + wp_enqueue_script('form_section_select2_init', EE_GLOBAL_ASSETS_URL.'scripts/form_section_select2_init.js', array('select2'), '1.0.0', true); |
|
| 48 | + wp_enqueue_style('select2', EE_GLOBAL_ASSETS_URL.'css/select2.min.css', array(), '4.0.2', 'all'); |
|
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | /** |
@@ -74,10 +74,10 @@ discard block |
||
| 74 | 74 | public function get_other_js_data($other_js_data = array()) |
| 75 | 75 | { |
| 76 | 76 | $other_js_data = parent::get_other_js_data($other_js_data); |
| 77 | - if (! isset($other_js_data['select2s'])) { |
|
| 77 | + if ( ! isset($other_js_data['select2s'])) { |
|
| 78 | 78 | $other_js_data['select2s'] = array(); |
| 79 | 79 | } |
| 80 | - $other_js_data['select2s'][ $this->_input->html_id() ] = $this->get_js_args(); |
|
| 80 | + $other_js_data['select2s'][$this->_input->html_id()] = $this->get_js_args(); |
|
| 81 | 81 | return $other_js_data; |
| 82 | 82 | } |
| 83 | 83 | |