| 1 |
<?php |
| 2 |
/** |
| 3 |
* Notifier Helper Functions |
| 4 |
* |
| 5 |
* @package Wa_Notifier |
| 6 |
*/ |
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; // Exit if accessed directly |
| 9 |
} |
| 10 |
|
| 11 |
/** |
| 12 |
* Create new post type |
| 13 |
* |
| 14 |
* @return null |
| 15 |
*/ |
| 16 |
function notifier_register_post_type($cpt_slug, $cpt_name, $cpt_name_plural, $args = []) { |
| 17 |
$labels = array ( |
| 18 |
'name' => $cpt_name_plural, |
| 19 |
'singular_name' => $cpt_name, |
| 20 |
'menu_name' => $cpt_name_plural, |
| 21 |
'parent_item_colon' => "Parent {$cpt_name}", |
| 22 |
'all_items' => "All {$cpt_name_plural}", |
| 23 |
'view_item' => "View {$cpt_name}", |
| 24 |
'add_new_item' => "Add New {$cpt_name}", |
| 25 |
'add_new' => 'Add New', |
| 26 |
'edit_item' => "Edit {$cpt_name}", |
| 27 |
'update_item' => "Update {$cpt_name}", |
| 28 |
'search_items' => "Search {$cpt_name}", |
| 29 |
'not_found' => "No {$cpt_name_plural} found", |
| 30 |
'not_found_in_trash' => "No {$cpt_name_plural} found in Trash" |
| 31 |
); |
| 32 |
$args = wp_parse_args($args, array ( |
| 33 |
'label' => $cpt_slug, |
| 34 |
'description' => "{$cpt_name_plural} Post Type", |
| 35 |
'labels' => $labels, |
| 36 |
'supports' => array( 'title'), |
| 37 |
'hierarchical' => false, |
| 38 |
'public' => false, |
| 39 |
'show_ui' => true, |
| 40 |
'show_in_menu' => false, |
| 41 |
'show_in_nav_menus' => false, |
| 42 |
'show_in_admin_bar' => false, |
| 43 |
'menu_position' => 5, |
| 44 |
'can_export' => true, |
| 45 |
'has_archive' => false, |
| 46 |
'capability_type' => 'post', |
| 47 |
'show_in_rest' => true, |
| 48 |
)); |
| 49 |
register_post_type($cpt_slug, $args); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Create new taxonomy for a post type |
| 54 |
* |
| 55 |
* @return null |
| 56 |
*/ |
| 57 |
function notifier_register_taxonomy($taxonomy_slug, $taxonomy_name, $taxonomy_name_plural, $cpt_slug, $args = array()) { |
| 58 |
$labels = array ( |
| 59 |
'name' => $taxonomy_name_plural, |
| 60 |
'singular_name' => $taxonomy_name, |
| 61 |
'search_items' => "Search {$taxonomy_name_plural}", |
| 62 |
'all_items' => "All {$taxonomy_name_plural}", |
| 63 |
'parent_item' => "Parent {$taxonomy_name}", |
| 64 |
'parent_item_colon' => "Parent {$taxonomy_name}:", |
| 65 |
'edit_item' => "Edit {$taxonomy_name}", |
| 66 |
'update_item' => "Update {$taxonomy_name}", |
| 67 |
'add_new_item' => "Add New {$taxonomy_name}", |
| 68 |
'new_item_name' => "New {$taxonomy_name} Name", |
| 69 |
'menu_name' => $taxonomy_name_plural, |
| 70 |
); |
| 71 |
|
| 72 |
$args = wp_parse_args( $args, array ( |
| 73 |
'hierarchical' => false, |
| 74 |
'labels' => $labels, |
| 75 |
'show_ui' => true, |
| 76 |
'show_admin_column' => true, |
| 77 |
'query_var' => true, |
| 78 |
'show_in_rest' => true, |
| 79 |
'rewrite' => array( 'slug' => $taxonomy_slug ) |
| 80 |
)); |
| 81 |
|
| 82 |
register_taxonomy($taxonomy_slug, [ $cpt_slug ], $args); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Sanitize array |
| 87 |
* |
| 88 |
* @return Array |
| 89 |
*/ |
| 90 |
function notifier_sanitize_array ( $array ) { |
| 91 |
foreach ( $array as $key => &$value ) { |
| 92 |
if ( is_array( $value ) ) { |
| 93 |
$value = notifier_sanitize_array($value); |
| 94 |
} else { |
| 95 |
$value = sanitize_text_field( $value ); |
| 96 |
} |
| 97 |
} |
| 98 |
return $array; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Upload image from URL programmatically |
| 103 |
*/ |
| 104 |
function notifier_upload_file_by_url( $image_url ) { |
| 105 |
|
| 106 |
// it allows us to use download_url() and wp_handle_sideload() functions |
| 107 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 108 |
|
| 109 |
// download to temp dir |
| 110 |
$temp_file = download_url( $image_url ); |
| 111 |
|
| 112 |
if ( is_wp_error( $temp_file ) ) { |
| 113 |
return false; |
| 114 |
} |
| 115 |
|
| 116 |
// move the temp file into the uploads directory |
| 117 |
$file = array( |
| 118 |
'name' => basename( parse_url($image_url, PHP_URL_PATH) ), |
| 119 |
'type' => mime_content_type( $temp_file ), |
| 120 |
'tmp_name' => $temp_file, |
| 121 |
'size' => filesize( $temp_file ), |
| 122 |
); |
| 123 |
|
| 124 |
$sideload = wp_handle_sideload( |
| 125 |
$file, |
| 126 |
array( |
| 127 |
'test_form' => false // no needs to check 'action' parameter |
| 128 |
) |
| 129 |
); |
| 130 |
|
| 131 |
if ( ! empty( $sideload[ 'error' ] ) ) { |
| 132 |
// you may return error message if you want |
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
// it is time to add our uploaded image into WordPress media library |
| 137 |
$attachment_id = wp_insert_attachment( |
| 138 |
array( |
| 139 |
'guid' => $sideload[ 'url' ], |
| 140 |
'post_mime_type' => $sideload[ 'type' ], |
| 141 |
'post_title' => basename( $sideload[ 'file' ] ), |
| 142 |
'post_content' => '', |
| 143 |
'post_status' => 'inherit', |
| 144 |
), |
| 145 |
$sideload[ 'file' ] |
| 146 |
); |
| 147 |
|
| 148 |
if ( is_wp_error( $attachment_id ) || ! $attachment_id ) { |
| 149 |
return false; |
| 150 |
} |
| 151 |
|
| 152 |
// update medatata, regenerate image sizes |
| 153 |
require_once ABSPATH . 'wp-admin/includes/image.php'; |
| 154 |
|
| 155 |
wp_update_attachment_metadata( |
| 156 |
$attachment_id, |
| 157 |
wp_generate_attachment_metadata( $attachment_id, $sideload[ 'file' ] ) |
| 158 |
); |
| 159 |
|
| 160 |
return $attachment_id; |
| 161 |
|
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Sanitize phone number |
| 166 |
*/ |
| 167 |
function notifier_sanitize_phone_number( $phone_number ) { |
| 168 |
return preg_replace( '/[^\d+]/', '', $phone_number ); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Validate phone number |
| 173 |
*/ |
| 174 |
function notifier_validate_phone_number( $phone_number ) { |
| 175 |
return preg_match( '/^\+[1-9]\d{8,15}$/', $phone_number ); |
| 176 |
} |
| 177 |
|
| 178 |
/* |
| 179 |
* Generate API key. |
| 180 |
*/ |
| 181 |
function notifier_generate_random_key( $length = 25 ) { |
| 182 |
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 183 |
$charactersLength = strlen($characters); |
| 184 |
$randomString = ''; |
| 185 |
for ( $i = 0; $i < $length; $i++ ) { |
| 186 |
$randomString .= $characters[rand(0, $charactersLength - 1)]; |
| 187 |
} |
| 188 |
return $randomString; |
| 189 |
} |
| 190 |
|