| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions |
| 4 |
* |
| 5 |
* @package AutomatorWP\WordPress\Functions |
| 6 |
* @since 1.0.0 |
| 7 |
*/ |
| 8 |
// Exit if accessed directly |
| 9 |
if( !defined( 'ABSPATH' ) ) exit; |
| 10 |
|
| 11 |
/** |
| 12 |
* Get taxonomies |
| 13 |
* |
| 14 |
* @since 1.0.0 |
| 15 |
* |
| 16 |
* @return array |
| 17 |
*/ |
| 18 |
function automatorwp_wordpress_get_public_taxonomies() { |
| 19 |
|
| 20 |
$taxonomies = array(); |
| 21 |
|
| 22 |
$all_taxonomies = get_taxonomies( array( 'public' => true ), 'objects' ); |
| 23 |
|
| 24 |
foreach ( $all_taxonomies as $taxonomy ) { |
| 25 |
|
| 26 |
$taxonomies[] = array( |
| 27 |
'id' => $taxonomy->name, |
| 28 |
'name' => $taxonomy->labels->name, |
| 29 |
); |
| 30 |
|
| 31 |
} |
| 32 |
|
| 33 |
return $taxonomies; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Get taxonomy |
| 38 |
* |
| 39 |
* @since 1.0.0 |
| 40 |
* |
| 41 |
* @param stdClass $field |
| 42 |
* |
| 43 |
* @return array |
| 44 |
*/ |
| 45 |
function automatorwp_wordpress_options_cb_taxonomy( $field ) { |
| 46 |
|
| 47 |
// Setup vars |
| 48 |
$value = $field->escaped_value; |
| 49 |
$none_value = 'any'; |
| 50 |
$none_label = __( 'any taxonomy', 'automatorwp' ); |
| 51 |
$options = automatorwp_options_cb_none_option( $field, $none_value, $none_label ); |
| 52 |
|
| 53 |
if( ! empty( $value ) ) { |
| 54 |
if( ! is_array( $value ) ) { |
| 55 |
$value = array( $value ); |
| 56 |
} |
| 57 |
|
| 58 |
foreach( $value as $taxonomy_id ) { |
| 59 |
|
| 60 |
// Skip option none |
| 61 |
if( $taxonomy_id === $none_value ) { |
| 62 |
continue; |
| 63 |
} |
| 64 |
|
| 65 |
$options[$taxonomy_id] = automatorwp_wordpress_get_taxonomy_name( $taxonomy_id ); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
return $options; |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Get the taxonomy name |
| 75 |
* |
| 76 |
* @since 1.0.0 |
| 77 |
* |
| 78 |
* @param string $taxonomy_id |
| 79 |
* |
| 80 |
* @return array |
| 81 |
*/ |
| 82 |
function automatorwp_wordpress_get_taxonomy_name( $taxonomy_id ) { |
| 83 |
|
| 84 |
// Empty title if no ID provided |
| 85 |
if( empty( $taxonomy_id ) ) { |
| 86 |
return ''; |
| 87 |
} |
| 88 |
|
| 89 |
$taxonomy = get_taxonomy( $taxonomy_id ); |
| 90 |
$taxonomy_name = $taxonomy->labels->name; |
| 91 |
|
| 92 |
return $taxonomy_name; |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Get terms |
| 98 |
* |
| 99 |
* @since 1.0.0 |
| 100 |
* |
| 101 |
* @param stdClass $field |
| 102 |
* |
| 103 |
* @return array |
| 104 |
*/ |
| 105 |
function automatorwp_wordpress_options_cb_term( $field ) { |
| 106 |
|
| 107 |
// Setup vars |
| 108 |
$value = $field->escaped_value; |
| 109 |
$none_value = 'any'; |
| 110 |
$none_label = __( 'any term', 'automatorwp' ); |
| 111 |
$options = automatorwp_options_cb_none_option( $field, $none_value, $none_label ); |
| 112 |
|
| 113 |
$taxonomy_id = ct_get_object_meta( $field->object_id, 'taxonomy', true ); |
| 114 |
|
| 115 |
if( ! empty( $value ) ) { |
| 116 |
if( ! is_array( $value ) ) { |
| 117 |
$value = array( $value ); |
| 118 |
} |
| 119 |
|
| 120 |
foreach( $value as $term_id ) { |
| 121 |
|
| 122 |
// Skip option none |
| 123 |
if( $term_id === $none_value ) { |
| 124 |
continue; |
| 125 |
} |
| 126 |
|
| 127 |
$options[$term_id] = automatorwp_wordpress_get_term_name( $taxonomy_id, $term_id ); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
return $options; |
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Get the terms |
| 137 |
* |
| 138 |
* @since 1.0.0 |
| 139 |
* |
| 140 |
* @return array |
| 141 |
*/ |
| 142 |
function automatorwp_wordpress_get_taxonomy_terms( $taxonomy_id ) { |
| 143 |
|
| 144 |
$terms = array(); |
| 145 |
$query_args = array( 'hide_empty' => false ); |
| 146 |
|
| 147 |
if( ! empty( $taxonomy_id ) ) { |
| 148 |
$query_args['taxonomy'] = $taxonomy_id; |
| 149 |
} |
| 150 |
|
| 151 |
$terms_taxonomy = get_terms( $query_args ); |
| 152 |
|
| 153 |
// Add terms to array |
| 154 |
foreach ( $terms_taxonomy as $term ) { |
| 155 |
$terms[] = array( |
| 156 |
'id' => $term->term_id, |
| 157 |
'name' => $term->name, |
| 158 |
); |
| 159 |
} |
| 160 |
|
| 161 |
return $terms; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Get the term name |
| 166 |
* |
| 167 |
* @since 1.0.0 |
| 168 |
* |
| 169 |
* @param string $term_id |
| 170 |
* |
| 171 |
* @return array |
| 172 |
*/ |
| 173 |
function automatorwp_wordpress_get_term_name( $taxonomy_id, $term_id ) { |
| 174 |
|
| 175 |
// Empty title if no ID provided |
| 176 |
if( absint( $term_id ) === 0 ) { |
| 177 |
return ''; |
| 178 |
} |
| 179 |
|
| 180 |
$term = get_term( $term_id ); |
| 181 |
$term_name = $term->name; |
| 182 |
|
| 183 |
return $term_name; |
| 184 |
|
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Validates an option name to prevent modifications to protected/critical options |
| 189 |
* |
| 190 |
* @since 1.0.0 |
| 191 |
* |
| 192 |
* @param string $option_name The option name to validate |
| 193 |
* |
| 194 |
* @return bool True if the option name is protected, false otherwise |
| 195 |
*/ |
| 196 |
function automatorwp_is_protected_option( $option_name ) { |
| 197 |
|
| 198 |
$protected_options = automatorwp_get_protected_options(); |
| 199 |
|
| 200 |
return in_array( $option_name, $protected_options, true ); |
| 201 |
|
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Validates an option name to prevent modifications to protected/critical options |
| 206 |
* |
| 207 |
* @since 1.0.0 |
| 208 |
* |
| 209 |
* @param string $option_name The option name to validate |
| 210 |
* |
| 211 |
* @return array List of protected options |
| 212 |
*/ |
| 213 |
function automatorwp_get_protected_options() { |
| 214 |
|
| 215 |
// List of protected core options that should not be modified |
| 216 |
$protected_options = array( |
| 217 |
'siteurl', |
| 218 |
'home', |
| 219 |
'admin_email', |
| 220 |
'users_can_register', |
| 221 |
'default_role', |
| 222 |
'db_version', |
| 223 |
'initial_db_version', |
| 224 |
'wp_user_roles', |
| 225 |
'auth_key', |
| 226 |
'secure_auth_key', |
| 227 |
'logged_in_key', |
| 228 |
'nonce_key', |
| 229 |
'auth_salt', |
| 230 |
'secure_auth_salt', |
| 231 |
'logged_in_salt', |
| 232 |
'nonce_salt', |
| 233 |
'active_plugins', |
| 234 |
'template', |
| 235 |
'stylesheet', |
| 236 |
'current_theme', |
| 237 |
'upload_path', |
| 238 |
); |
| 239 |
|
| 240 |
/** |
| 241 |
* Filter to modify the list of protected options |
| 242 |
* |
| 243 |
* @since 1.0.0 |
| 244 |
* |
| 245 |
* @param array $protected_options List of protected option names |
| 246 |
*/ |
| 247 |
$protected_options = apply_filters( 'automatorwp_protected_options', $protected_options ); |
| 248 |
|
| 249 |
return $protected_options; |
| 250 |
|
| 251 |
} |
| 252 |
|