| 1 |
<?php |
| 2 |
/** |
| 3 |
* Misc Functions |
| 4 |
*/ |
| 5 |
|
| 6 |
// If this file is called directly, abort. |
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
die; |
| 9 |
} |
| 10 |
|
| 11 |
/** |
| 12 |
* Retrieve a post/page by its title. |
| 13 |
* @since 1.0 |
| 14 |
* @param string $title The title of the post/page to retrieve. |
| 15 |
* @return WP_Post|null The retrieved post/page object or null if not found. |
| 16 |
*/ |
| 17 |
if ( !function_exists( 'hootimport_get_post_type_by_title' ) ): |
| 18 |
function hootimport_get_post_type_by_title( $title, $type='post' ) { |
| 19 |
if ( ! $title || !is_string( $title ) ) { |
| 20 |
return null; |
| 21 |
} |
| 22 |
$query = new \WP_Query( |
| 23 |
array( |
| 24 |
'post_type' => $type, |
| 25 |
'title' => $title, |
| 26 |
'post_status' => 'all', |
| 27 |
'posts_per_page' => 1, |
| 28 |
'no_found_rows' => true, |
| 29 |
'ignore_sticky_posts' => true, |
| 30 |
'update_post_term_cache' => false, |
| 31 |
'update_post_meta_cache' => false, |
| 32 |
) |
| 33 |
); |
| 34 |
if ( ! $query->have_posts() ) { |
| 35 |
return null; |
| 36 |
} |
| 37 |
return current( $query->posts ); |
| 38 |
} |
| 39 |
endif; |
| 40 |
|
| 41 |
/** |
| 42 |
* Recursive wp_parse_args |
| 43 |
* Non Strict Mode: Use $args value if present ('', 0, '0', false allowed) |
| 44 |
* @since 1.0 |
| 45 |
* @return mixed |
| 46 |
*/ |
| 47 |
if ( !function_exists( 'hootimport_recursive_parse_args' ) ): |
| 48 |
function hootimport_recursive_parse_args( $args, $defaults ) { |
| 49 |
$return = (array) $defaults; |
| 50 |
foreach ( $args as $key => $value ) { |
| 51 |
if ( is_array( $value ) && isset( $return[ $key ] ) ) { |
| 52 |
$return[ $key ] = hootimport_recursive_parse_args( $value, $return[ $key ] ); |
| 53 |
} else { |
| 54 |
$return[ $key ] = $value; |
| 55 |
} |
| 56 |
} |
| 57 |
return $return; |
| 58 |
} |
| 59 |
endif; |
| 60 |
|
| 61 |
/** |
| 62 |
* sanitize_html_class works just fine for a single class |
| 63 |
* This is an extension to sanitize_html_class for sanitizing more than one class (with spaces or in an array) |
| 64 |
* |
| 65 |
* @param mixed string/array |
| 66 |
* @param mixed $fallback |
| 67 |
* @return mixed string / $fallback |
| 68 |
*/ |
| 69 |
if ( !function_exists( 'hootimport_sanitize_html_classes' ) ): |
| 70 |
function hootimport_sanitize_html_classes( $class, $fallback = null ) { |
| 71 |
if ( is_string( $class ) ) { |
| 72 |
$class = explode( " ", $class ); |
| 73 |
} |
| 74 |
if ( is_array( $class ) && count( $class ) > 0 ) { |
| 75 |
$class = array_map( "sanitize_html_class", $class ); |
| 76 |
return implode( " ", $class ); |
| 77 |
} |
| 78 |
else { |
| 79 |
return sanitize_html_class( $class, $fallback ); |
| 80 |
} |
| 81 |
} |
| 82 |
endif; |
| 83 |
|
| 84 |
/** |
| 85 |
* Cleanup |
| 86 |
* @since 1.0 |
| 87 |
* @param string $demopack_dir |
| 88 |
* @param bool $forcecleanup |
| 89 |
* @return void |
| 90 |
*/ |
| 91 |
if ( !function_exists( 'hootimport_cleanup' ) ): |
| 92 |
function hootimport_cleanup( $demopack_dir = '', $forcecleanup = false ) { |
| 93 |
if ( |
| 94 |
empty( $demopack_dir ) || !is_string( $demopack_dir ) |
| 95 |
|| !function_exists( 'current_user_can' ) |
| 96 |
|| !is_dir( $demopack_dir ) |
| 97 |
) |
| 98 |
return; |
| 99 |
|
| 100 |
// Cleanup demo pack directory |
| 101 |
$is_fresh = get_transient( 'hootimport_freshpack' ); |
| 102 |
if ( empty( $is_fresh ) || $forcecleanup ) { |
| 103 |
if ( current_user_can( 'manage_options' ) ) { |
| 104 |
|
| 105 |
// Initialize the WP Filesystem API |
| 106 |
if ( ! function_exists( 'wp_filesystem' ) ) { |
| 107 |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 108 |
} |
| 109 |
global $wp_filesystem; |
| 110 |
WP_Filesystem(); |
| 111 |
|
| 112 |
// Check if the directory exists and remove the directory and all its contents |
| 113 |
if ( $wp_filesystem->is_dir( $demopack_dir ) ) { |
| 114 |
$wp_filesystem->rmdir( $demopack_dir, true ); |
| 115 |
} |
| 116 |
|
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
} |
| 121 |
endif; |
| 122 |
|