| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; |
| 4 |
} |
| 5 |
|
| 6 |
/** |
| 7 |
* GmediaProcessor_Terms |
| 8 |
*/ |
| 9 |
class GmediaProcessor_Terms extends GmediaProcessor { |
| 10 |
|
| 11 |
public static $cookie_key = false; |
| 12 |
|
| 13 |
private static $me = null; |
| 14 |
|
| 15 |
public $selected_items = array(); |
| 16 |
public $query_args = array(); |
| 17 |
|
| 18 |
/** |
| 19 |
* GmediaProcessor_Library constructor. |
| 20 |
*/ |
| 21 |
public function __construct() { |
| 22 |
parent::__construct(); |
| 23 |
|
| 24 |
self::$cookie_key = "gmedia_terms:{$this->taxterm}"; |
| 25 |
if ( ! isset( $_COOKIE[ self::$cookie_key ] ) ) { |
| 26 |
foreach ( $_COOKIE as $key => $value ) { |
| 27 |
if ( 'gmedia_terms' === substr( $key, 0, 12 ) ) { |
| 28 |
setcookie( $key, '', time() - 3600 ); |
| 29 |
unset( $_COOKIE[ $key ] ); |
| 30 |
} |
| 31 |
} |
| 32 |
} |
| 33 |
$this->selected_items = parent::selected_items( self::$cookie_key ); |
| 34 |
} |
| 35 |
|
| 36 |
public static function getMe() { |
| 37 |
if ( null === self::$me ) { |
| 38 |
self::$me = new GmediaProcessor_Terms(); |
| 39 |
} |
| 40 |
|
| 41 |
return self::$me; |
| 42 |
} |
| 43 |
|
| 44 |
protected function processor() { |
| 45 |
global $user_ID, $gmCore, $gmDB; |
| 46 |
|
| 47 |
if ( ! $gmCore->caps['gmedia_library'] ) { |
| 48 |
wp_die( esc_html__( 'You are not allowed to be here', 'grand-media' ) ); |
| 49 |
} |
| 50 |
|
| 51 |
include_once GMEDIA_ABSPATH . 'admin/pages/terms/functions.php'; |
| 52 |
|
| 53 |
$this->query_args = $this->query_args(); |
| 54 |
|
| 55 |
$taxonomy = $this->taxonomy; |
| 56 |
switch ( $taxonomy ) { |
| 57 |
case 'gmedia_album': |
| 58 |
if ( gm_user_can( 'album_manage' ) ) { |
| 59 |
add_action( 'gmedia_before_terms_list', 'gmedia_terms_create_album_tpl' ); |
| 60 |
} else { |
| 61 |
add_action( 'gmedia_before_terms_list', 'gmedia_terms_create_alert_tpl' ); |
| 62 |
} |
| 63 |
break; |
| 64 |
case 'gmedia_category': |
| 65 |
if ( gm_user_can( 'category_manage' ) ) { |
| 66 |
add_action( 'gmedia_before_terms_list', 'gmedia_terms_create_category_tpl' ); |
| 67 |
} else { |
| 68 |
add_action( 'gmedia_before_terms_list', 'gmedia_terms_create_alert_tpl' ); |
| 69 |
} |
| 70 |
break; |
| 71 |
case 'gmedia_tag': |
| 72 |
if ( gm_user_can( 'tag_manage' ) ) { |
| 73 |
add_action( 'gmedia_before_terms_list', 'gmedia_terms_create_tag_tpl' ); |
| 74 |
} else { |
| 75 |
add_action( 'gmedia_before_terms_list', 'gmedia_terms_create_alert_tpl' ); |
| 76 |
} |
| 77 |
break; |
| 78 |
} |
| 79 |
|
| 80 |
if ( isset( $_POST['gmedia_album_save'] ) ) { |
| 81 |
check_admin_referer( 'gmedia_terms', '_wpnonce_terms' ); |
| 82 |
$edit_term = (int) $gmCore->_get( 'edit_term' ); |
| 83 |
do { |
| 84 |
if ( ! $gmCore->caps['gmedia_album_manage'] ) { |
| 85 |
$this->error[] = __( 'You are not allowed to manage albums', 'grand-media' ); |
| 86 |
break; |
| 87 |
} |
| 88 |
$term = $gmCore->_post( 'term' ); |
| 89 |
$meta = $gmCore->_post( 'meta' ); |
| 90 |
if ( $meta ) { |
| 91 |
$term = array_merge_recursive( array( 'meta' => $meta ), $term ); |
| 92 |
} |
| 93 |
$term['name'] = trim( $term['name'] ); |
| 94 |
if ( empty( $term['name'] ) ) { |
| 95 |
$this->error[] = esc_html__( 'Term Name is not specified', 'grand-media' ); |
| 96 |
break; |
| 97 |
} |
| 98 |
if ( $gmCore->is_digit( $term['name'] ) ) { |
| 99 |
$this->error[] = esc_html__( "Term Name can't be only digits", 'grand-media' ); |
| 100 |
break; |
| 101 |
} |
| 102 |
$taxonomy = 'gmedia_album'; |
| 103 |
if ( $edit_term && ! $gmDB->term_exists( $edit_term ) ) { |
| 104 |
$this->error[] = esc_html__( 'A term with the id provided does not exists', 'grand-media' ); |
| 105 |
$edit_term = false; |
| 106 |
} |
| 107 |
$term_id = $gmDB->term_exists( $term['name'], $taxonomy, $term['global'] ); |
| 108 |
if ( $term_id && $term_id !== $edit_term ) { |
| 109 |
$this->error[] = esc_html__( 'A term with the name provided already exists', 'grand-media' ); |
| 110 |
break; |
| 111 |
} |
| 112 |
if ( $edit_term ) { |
| 113 |
$_term = $gmDB->get_term( $edit_term ); |
| 114 |
if ( ( (int) $_term->global !== (int) $user_ID ) && ! current_user_can( 'gmedia_edit_others_media' ) ) { |
| 115 |
$this->error[] = esc_html__( 'You are not allowed to edit others media', 'grand-media' ); |
| 116 |
break; |
| 117 |
} |
| 118 |
$term_id = $gmDB->update_term( $edit_term, $term ); |
| 119 |
} else { |
| 120 |
$term_id = $gmDB->insert_term( $term['name'], $term['taxonomy'], $term ); |
| 121 |
} |
| 122 |
if ( is_wp_error( $term_id ) ) { |
| 123 |
$this->error[] = $term_id->get_error_message(); |
| 124 |
break; |
| 125 |
} |
| 126 |
if ( isset( $term['reset_custom_order'] ) ) { |
| 127 |
$gmDB->update_term_sortorder( $term_id ); |
| 128 |
} |
| 129 |
|
| 130 |
// translators: album name. |
| 131 |
$this->msg[] = sprintf( esc_html__( 'Album `%s` successfully saved', 'grand-media' ), esc_html( $term['name'] ) ); |
| 132 |
|
| 133 |
} while ( 0 ); |
| 134 |
} elseif ( isset( $_POST['gmedia_category_save'] ) ) { |
| 135 |
check_admin_referer( 'gmedia_terms', '_wpnonce_terms' ); |
| 136 |
$edit_term = (int) $gmCore->_get( 'edit_term' ); |
| 137 |
do { |
| 138 |
if ( ! $gmCore->caps['gmedia_category_manage'] ) { |
| 139 |
$this->error[] = __( 'You are not allowed to manage categories', 'grand-media' ); |
| 140 |
break; |
| 141 |
} |
| 142 |
$term = $gmCore->_post( 'term' ); |
| 143 |
$meta = $gmCore->_post( 'meta' ); |
| 144 |
if ( $meta ) { |
| 145 |
$term = array_merge_recursive( array( 'meta' => $meta ), $term ); |
| 146 |
} |
| 147 |
$term['name'] = trim( $term['name'] ); |
| 148 |
if ( empty( $term['name'] ) ) { |
| 149 |
$this->error[] = esc_html__( 'Term Name is not specified', 'grand-media' ); |
| 150 |
break; |
| 151 |
} |
| 152 |
if ( $gmCore->is_digit( $term['name'] ) ) { |
| 153 |
$this->error[] = esc_html__( "Term Name can't be only digits", 'grand-media' ); |
| 154 |
break; |
| 155 |
} |
| 156 |
$taxonomy = 'gmedia_category'; |
| 157 |
if ( $edit_term && ! $gmDB->term_exists( $edit_term ) ) { |
| 158 |
$this->error[] = esc_html__( 'A term with the id provided does not exists', 'grand-media' ); |
| 159 |
$edit_term = false; |
| 160 |
} |
| 161 |
$term_id = $gmDB->term_exists( $term['name'], $taxonomy ); |
| 162 |
if ( $term_id && $term_id !== $edit_term ) { |
| 163 |
$this->error[] = esc_html__( 'A term with the name provided already exists', 'grand-media' ); |
| 164 |
break; |
| 165 |
} |
| 166 |
if ( $edit_term ) { |
| 167 |
if ( ! current_user_can( 'gmedia_edit_others_media' ) ) { |
| 168 |
$this->error[] = esc_html__( 'You are not allowed to edit others media', 'grand-media' ); |
| 169 |
break; |
| 170 |
} |
| 171 |
$term_id = $gmDB->update_term( $edit_term, $term ); |
| 172 |
} else { |
| 173 |
$term_id = $gmDB->insert_term( $term['name'], $term['taxonomy'], $term ); |
| 174 |
} |
| 175 |
if ( is_wp_error( $term_id ) ) { |
| 176 |
$this->error[] = $term_id->get_error_message(); |
| 177 |
break; |
| 178 |
} |
| 179 |
|
| 180 |
// translators: category name. |
| 181 |
$this->msg[] = sprintf( esc_html__( 'Category `%s` successfully saved', 'grand-media' ), esc_html( $term['name'] ) ); |
| 182 |
|
| 183 |
} while ( 0 ); |
| 184 |
} elseif ( isset( $_POST['gmedia_tag_add'] ) ) { |
| 185 |
if ( $gmCore->caps['gmedia_tag_manage'] ) { |
| 186 |
check_admin_referer( 'gmedia_terms', '_wpnonce_terms' ); |
| 187 |
$term = $gmCore->_post( 'term' ); |
| 188 |
$terms = array_filter( array_map( 'trim', explode( ',', $term['name'] ) ) ); |
| 189 |
$terms_added = 0; |
| 190 |
$terms_qty = count( $terms ); |
| 191 |
foreach ( $terms as $term_name ) { |
| 192 |
if ( $gmCore->is_digit( $term_name ) ) { |
| 193 |
$this->error['tag_name_digit'] = esc_html__( "Term Name can't be only digits", 'grand-media' ); |
| 194 |
continue; |
| 195 |
} |
| 196 |
|
| 197 |
if ( ! $gmDB->term_exists( $term_name, $term['taxonomy'] ) ) { |
| 198 |
$term_id = $gmDB->insert_term( $term_name, $term['taxonomy'] ); |
| 199 |
if ( is_wp_error( $term_id ) ) { |
| 200 |
$this->error[] = $term_id->get_error_message(); |
| 201 |
} else { |
| 202 |
// translators: 1 - number, 2 - number. |
| 203 |
$this->msg['tag_add'] = sprintf( esc_html__( '%1$d of %2$d tags successfully added', 'grand-media' ), ++ $terms_added, $terms_qty ); |
| 204 |
} |
| 205 |
} else { |
| 206 |
$this->error['tag_add'] = esc_html__( 'Some of provided tags are already exists', 'grand-media' ); |
| 207 |
} |
| 208 |
} |
| 209 |
} else { |
| 210 |
$this->error[] = esc_html__( 'You are not allowed to manage tags', 'grand-media' ); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
$do_gmedia_terms = $gmCore->_get( 'do_gmedia_terms' ); |
| 215 |
if ( 'delete' === $do_gmedia_terms ) { |
| 216 |
check_admin_referer( 'gmedia_delete', '_wpnonce_delete' ); |
| 217 |
if ( $gmCore->caps['gmedia_terms_delete'] ) { |
| 218 |
$ids = $gmCore->_get( 'ids', 'selected' ); |
| 219 |
$selected_items = ( 'selected' === $ids ) ? $this->selected_items : wp_parse_id_list( $ids ); |
| 220 |
if ( ! $gmCore->caps['gmedia_delete_others_media'] ) { |
| 221 |
$_selected_items = array(); |
| 222 |
if ( 'gmedia_album' === $taxonomy ) { |
| 223 |
$_selected_items = $gmDB->get_terms( $taxonomy, array( 'fields' => 'ids', 'global' => $user_ID, 'include' => $selected_items ) ); |
| 224 |
} |
| 225 |
if ( count( $_selected_items ) < count( $selected_items ) ) { |
| 226 |
$this->error[] = __( 'You are not allowed to delete others media', 'grand-media' ); |
| 227 |
} |
| 228 |
$selected_items = $_selected_items; |
| 229 |
} |
| 230 |
$count = count( $selected_items ); |
| 231 |
if ( $count ) { |
| 232 |
foreach ( $selected_items as $item ) { |
| 233 |
$delete = $gmDB->delete_term( $item ); |
| 234 |
if ( ! $delete ) { |
| 235 |
$count --; |
| 236 |
} elseif ( is_wp_error( $delete ) ) { |
| 237 |
$this->error[] = $delete->get_error_message(); |
| 238 |
$count --; |
| 239 |
} |
| 240 |
} |
| 241 |
if ( $count ) { |
| 242 |
// translators: number. |
| 243 |
$this->msg[] = sprintf( esc_html__( '%d item(s) deleted successfully', 'grand-media' ), $count ); |
| 244 |
} |
| 245 |
setcookie( self::$cookie_key, '', time() - 3600 ); |
| 246 |
unset( $_COOKIE[ self::$cookie_key ] ); |
| 247 |
$this->selected_items = array(); |
| 248 |
} |
| 249 |
} else { |
| 250 |
$this->error[] = esc_html__( 'You are not allowed to delete terms', 'grand-media' ); |
| 251 |
} |
| 252 |
if ( ! empty( $this->msg ) ) { |
| 253 |
set_transient( 'gmedia_action_msg', $this->msg, 30 ); |
| 254 |
} |
| 255 |
if ( ! empty( $this->error ) ) { |
| 256 |
set_transient( 'gmedia_action_error', $this->error, 30 ); |
| 257 |
} |
| 258 |
} |
| 259 |
if ( $do_gmedia_terms ) { |
| 260 |
$_wpnonce = array(); |
| 261 |
foreach ( $_GET as $key => $value ) { |
| 262 |
if ( strpos( $key, '_wpnonce' ) !== false ) { |
| 263 |
$_wpnonce[ $key ] = $value; |
| 264 |
} |
| 265 |
} |
| 266 |
$remove_args = array_merge( array( 'do_gmedia_terms', 'ids' ), $_wpnonce ); |
| 267 |
$location = remove_query_arg( $remove_args ); |
| 268 |
$location = add_query_arg( 'did_gmedia_terms', $do_gmedia_terms, $location ); |
| 269 |
wp_safe_redirect( $location ); |
| 270 |
exit; |
| 271 |
} |
| 272 |
if ( $gmCore->_get( 'did_gmedia_terms' ) ) { |
| 273 |
$msg = get_transient( 'gmedia_action_msg' ); |
| 274 |
if ( $msg ) { |
| 275 |
delete_transient( 'gmedia_action_msg' ); |
| 276 |
$this->msg = $msg; |
| 277 |
} |
| 278 |
$error = get_transient( 'gmedia_action_error' ); |
| 279 |
if ( $error ) { |
| 280 |
delete_transient( 'gmedia_action_error' ); |
| 281 |
$this->error = $error; |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* @return array |
| 288 |
*/ |
| 289 |
public function query_args() { |
| 290 |
global $gmCore; |
| 291 |
|
| 292 |
$args['status'] = $gmCore->_get( 'status' ); |
| 293 |
$args['page'] = $gmCore->_get( 'pager', 1 ); |
| 294 |
$args['number'] = $gmCore->_get( 'per_page', $this->user_options["per_page_{$this->taxonomy}"] ); |
| 295 |
$args['offset'] = ( $args['page'] - 1 ) * $args['number']; |
| 296 |
$args['global'] = parent::filter_by_author( $gmCore->_get( 'author' ) ); |
| 297 |
$args['include'] = $gmCore->_get( 'include' ); |
| 298 |
$args['search'] = $gmCore->_get( 's' ); |
| 299 |
$args['orderby'] = $gmCore->_get( 'orderby', $this->user_options["orderby_{$this->taxonomy}"] ); |
| 300 |
$args['order'] = $gmCore->_get( 'order', $this->user_options["sortorder_{$this->taxonomy}"] ); |
| 301 |
$args['hide_empty'] = $gmCore->_get( 'hide_empty' ); |
| 302 |
|
| 303 |
if ( $args['search'] && ( '#' === substr( $args['search'], 0, 1 ) ) ) { |
| 304 |
$args['include'] = substr( $args['search'], 1 ); |
| 305 |
$args['search'] = false; |
| 306 |
} |
| 307 |
|
| 308 |
if ( ( 'selected' === $gmCore->_req( 'filter' ) ) && ! empty( $this->selected_items ) ) { |
| 309 |
$args['include'] = $this->selected_items; |
| 310 |
$args['orderby'] = $gmCore->_get( 'orderby', 'include' ); |
| 311 |
$args['order'] = $gmCore->_get( 'order', 'ASC' ); |
| 312 |
} |
| 313 |
|
| 314 |
$query_args = apply_filters( 'gmedia_terms_query_args', $args ); |
| 315 |
|
| 316 |
foreach ( $query_args as $key => $val ) { |
| 317 |
if ( empty( $val ) && ( '0' !== $val ) && ( 0 !== $val ) ) { |
| 318 |
unset( $query_args[ $key ] ); |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
return $query_args; |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
global $gmProcessorTerms; |
| 327 |
$gmProcessorTerms = GmediaProcessor_Terms::getMe(); |
| 328 |
|