| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; |
| 4 |
} |
| 5 |
|
| 6 |
/** |
| 7 |
* GmediaProcessor_Library |
| 8 |
*/ |
| 9 |
class GmediaProcessor_Library extends GmediaProcessor { |
| 10 |
|
| 11 |
public static $cookie_key = false; |
| 12 |
|
| 13 |
private static $me = null; |
| 14 |
|
| 15 |
public $mode = false; |
| 16 |
public $selected_items = array(); |
| 17 |
public $stack_items = array(); |
| 18 |
public $filters = array(); |
| 19 |
public $dbfilter; |
| 20 |
public $query_args; |
| 21 |
|
| 22 |
/** |
| 23 |
* GmediaProcessor_Library constructor. |
| 24 |
*/ |
| 25 |
public function __construct() { |
| 26 |
parent::__construct(); |
| 27 |
|
| 28 |
global $gmCore; |
| 29 |
|
| 30 |
$user_ID = get_current_user_id(); |
| 31 |
self::$cookie_key = 'gmedia_library'; |
| 32 |
$this->mode = $gmCore->_get( 'mode' ); |
| 33 |
$stack = 'show' === $gmCore->_get( 'stack' ) ? 'show' : false; |
| 34 |
$filter = 'selected' === $gmCore->_get( 'filter' ) ? 'selected' : false; |
| 35 |
if ( $this->edit_term ) { |
| 36 |
self::$cookie_key .= ':term' . $this->edit_term; |
| 37 |
if ( ! isset( $_COOKIE[ self::$cookie_key ] ) ) { |
| 38 |
foreach ( $_COOKIE as $key => $value ) { |
| 39 |
if ( 'gmedia_library:' === substr( $key, 0, 15 ) ) { |
| 40 |
setcookie( $key, '', time() - 3600 ); |
| 41 |
unset( $_COOKIE[ $key ] ); |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
} elseif ( $this->gmediablank && 'select_multiple' === $this->mode ) { |
| 46 |
self::$cookie_key .= ':frame'; |
| 47 |
} |
| 48 |
$this->url = add_query_arg( array( 'mode' => $this->mode, 'stack' => $stack, 'filter' => $filter, 'edit_term' => $this->edit_term ), $this->url ); |
| 49 |
$this->selected_items = parent::selected_items( self::$cookie_key ); |
| 50 |
$this->stack_items = parent::selected_items( "gmedia_{$user_ID}_libstack", 'stack_items' ); |
| 51 |
|
| 52 |
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); |
| 53 |
} |
| 54 |
|
| 55 |
public static function getMe() { |
| 56 |
if ( null === self::$me ) { |
| 57 |
self::$me = new GmediaProcessor_Library(); |
| 58 |
} |
| 59 |
|
| 60 |
return self::$me; |
| 61 |
} |
| 62 |
|
| 63 |
public function admin_enqueue_scripts() { |
| 64 |
if ( 'edit' === $this->mode ) { |
| 65 |
if ( 'false' === $this->user_options['library_edit_quicktags'] ) { |
| 66 |
wp_enqueue_script( 'wplink' ); |
| 67 |
wp_enqueue_style( 'editor-buttons' ); |
| 68 |
|
| 69 |
add_action( 'admin_footer', array( $this, 'wpLink' ) ); |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
public function wpLink() { |
| 75 |
if ( ! class_exists( '_WP_Editors', false ) ) { |
| 76 |
require_once ABSPATH . WPINC . '/class-wp-editor.php'; |
| 77 |
} |
| 78 |
_WP_Editors::wp_link_dialog(); |
| 79 |
} |
| 80 |
|
| 81 |
protected function processor() { |
| 82 |
global $user_ID, $gmCore, $gmDB, $gmGallery; |
| 83 |
|
| 84 |
if ( ! $gmCore->caps['gmedia_library'] ) { |
| 85 |
wp_die( esc_html__( 'You are not allowed to be here', 'grand-media' ) ); |
| 86 |
} |
| 87 |
|
| 88 |
include_once GMEDIA_ABSPATH . 'admin/pages/library/functions.php'; |
| 89 |
|
| 90 |
if ( in_array( $this->mode, array( 'select_single', 'select_multiple' ), true ) ) { |
| 91 |
$this->display_mode = 'grid'; |
| 92 |
} |
| 93 |
if ( isset( $_GET['display_mode'] ) ) { |
| 94 |
$display_mode = $gmCore->_get( 'display_mode' ); |
| 95 |
if ( in_array( $display_mode, array( 'grid', 'list' ), true ) ) { |
| 96 |
$view = $this->gmediablank ? '_frame' : ''; |
| 97 |
$this->user_options = array_merge( $this->user_options, array( "display_mode_gmedia{$view}" => $display_mode ) ); |
| 98 |
update_user_meta( $user_ID, 'gm_screen_options', $this->user_options ); |
| 99 |
} |
| 100 |
$location = remove_query_arg( 'display_mode' ); |
| 101 |
wp_safe_redirect( $location ); |
| 102 |
exit; |
| 103 |
} |
| 104 |
|
| 105 |
if ( isset( $_GET['grid_cell_fit'] ) ) { |
| 106 |
$this->user_options['grid_cell_fit_gmedia'] = ! $this->user_options['grid_cell_fit_gmedia']; |
| 107 |
update_user_meta( $user_ID, 'gm_screen_options', $this->user_options ); |
| 108 |
if ( isset( $_GET['ajaxload'] ) ) { |
| 109 |
exit; |
| 110 |
} |
| 111 |
$location = remove_query_arg( 'grid_cell_fit' ); |
| 112 |
wp_safe_redirect( $location ); |
| 113 |
exit; |
| 114 |
} |
| 115 |
|
| 116 |
if ( isset( $_GET['gallery'] ) ) { |
| 117 |
$location = $this->url; |
| 118 |
$gallery_id = $gmCore->_get( 'gallery' ); |
| 119 |
if ( $gallery_id ) { |
| 120 |
$gallery_query = $gmDB->get_metadata( 'gmedia_term', $gallery_id, '_query', true ); |
| 121 |
$location = add_query_arg( $gallery_query, $location ); |
| 122 |
} |
| 123 |
wp_safe_redirect( $location ); |
| 124 |
exit; |
| 125 |
} |
| 126 |
|
| 127 |
$this->query_args = $this->query_args(); |
| 128 |
|
| 129 |
if ( isset( $_POST['quick_gallery'] ) ) { |
| 130 |
check_admin_referer( 'gmedia_action', '_wpnonce_action' ); |
| 131 |
do { |
| 132 |
if ( ! $gmCore->caps['gmedia_gallery_manage'] ) { |
| 133 |
$this->error[] = esc_html__( 'You are not allowed to manage galleries', 'grand-media' ); |
| 134 |
break; |
| 135 |
} |
| 136 |
$gallery = $gmCore->_post( 'gallery' ); |
| 137 |
$gallery['name'] = trim( $gallery['name'] ); |
| 138 |
if ( empty( $gallery['name'] ) ) { |
| 139 |
$this->error[] = esc_html__( 'Gallery Name is not specified', 'grand-media' ); |
| 140 |
break; |
| 141 |
} |
| 142 |
if ( $gmCore->is_digit( $gallery['name'] ) ) { |
| 143 |
$this->error[] = esc_html__( "Gallery name can't be only digits", 'grand-media' ); |
| 144 |
break; |
| 145 |
} |
| 146 |
if ( empty( $gallery['query']['gmedia__in'] ) ) { |
| 147 |
$this->error[] = esc_html__( 'Choose gmedia from library for quick gallery', 'grand-media' ); |
| 148 |
break; |
| 149 |
} |
| 150 |
$taxonomy = 'gmedia_gallery'; |
| 151 |
$term_id = $gmDB->term_exists( $gallery['name'], $taxonomy ); |
| 152 |
if ( $term_id ) { |
| 153 |
$this->error[] = esc_html__( 'A term with the name provided already exists', 'grand-media' ); |
| 154 |
break; |
| 155 |
} |
| 156 |
$term_id = $gmDB->insert_term( $gallery['name'], $taxonomy ); |
| 157 |
if ( is_wp_error( $term_id ) ) { |
| 158 |
$this->error[] = $term_id->get_error_message(); |
| 159 |
break; |
| 160 |
} |
| 161 |
$getModulePreset = $gmCore->getModulePreset( $gallery['module'] ); |
| 162 |
$gallery['query'] = array_merge( $gallery['query'], array( 'order' => 'ASC', 'orderby' => 'gmedia__in' ) ); |
| 163 |
|
| 164 |
$gallery_meta = array( |
| 165 |
'_edited' => gmdate( 'Y-m-d H:i:s' ), |
| 166 |
'_query' => $gallery['query'], |
| 167 |
'_module' => $getModulePreset['module'], |
| 168 |
'_settings' => $getModulePreset['settings'], |
| 169 |
); |
| 170 |
foreach ( $gallery_meta as $key => $value ) { |
| 171 |
$gmDB->update_metadata( 'gmedia_term', $term_id, $key, $value ); |
| 172 |
} |
| 173 |
// translators: 1 - gallery name; 2 - ID. |
| 174 |
$this->msg[] = sprintf( esc_html__( 'Gallery "%1$s" successfully saved. Shortcode: [gmedia id=%2$d]', 'grand-media' ), esc_html( $gallery['name'] ), intval( $term_id ) ); |
| 175 |
} while ( 0 ); |
| 176 |
} |
| 177 |
|
| 178 |
if ( isset( $_POST['filter_categories'] ) ) { |
| 179 |
$term = $gmCore->_post( 'cat' ); |
| 180 |
if ( false !== $term ) { |
| 181 |
$location = add_query_arg( array( 'category__in' => implode( ',', $term ) ), $this->url ); |
| 182 |
wp_safe_redirect( $location ); |
| 183 |
exit; |
| 184 |
} |
| 185 |
} |
| 186 |
if ( isset( $_POST['filter_albums'] ) ) { |
| 187 |
$term = $gmCore->_post( 'alb' ); |
| 188 |
if ( false !== $term ) { |
| 189 |
$location = add_query_arg( array( 'album__in' => implode( ',', $term ) ), $this->url ); |
| 190 |
wp_safe_redirect( $location ); |
| 191 |
exit; |
| 192 |
} |
| 193 |
} |
| 194 |
if ( isset( $_POST['filter_tags'] ) ) { |
| 195 |
$term = $gmCore->_post( 'tag_ids' ); |
| 196 |
if ( false !== $term ) { |
| 197 |
$location = add_query_arg( array( 'tag__in' => $term ), $this->url ); |
| 198 |
wp_safe_redirect( $location ); |
| 199 |
exit; |
| 200 |
} |
| 201 |
} |
| 202 |
if ( isset( $_POST['filter_author'] ) ) { |
| 203 |
$authors = $gmCore->_post( 'author_ids' ); |
| 204 |
$location = add_query_arg( array( 'author' => (int) $authors ), $this->url ); |
| 205 |
wp_safe_redirect( $location ); |
| 206 |
exit; |
| 207 |
} |
| 208 |
|
| 209 |
$do_gmedia = $gmCore->_get( 'do_gmedia' ); |
| 210 |
if ( ! empty( $this->selected_items ) || isset( $_POST['cookie_key'] ) ) { |
| 211 |
if ( isset( $_POST['assign_album'] ) ) { |
| 212 |
check_admin_referer( 'gmedia_action', '_wpnonce_action' ); |
| 213 |
if ( $gmCore->caps['gmedia_terms'] ) { |
| 214 |
$cookie_key = $gmCore->_post( 'cookie_key', self::$cookie_key ); |
| 215 |
$ids = $this->selected_items( $cookie_key ); |
| 216 |
if ( ! $gmCore->caps['gmedia_edit_others_media'] ) { |
| 217 |
$selected_items = $gmDB->get_gmedias( array( 'fields' => 'ids', 'author' => $user_ID, 'gmedia__in' => $ids ) ); |
| 218 |
if ( count( $selected_items ) < count( $ids ) ) { |
| 219 |
$this->error[] = esc_html__( 'You are not allowed to edit others media', 'grand-media' ); |
| 220 |
} |
| 221 |
} else { |
| 222 |
$selected_items = $ids; |
| 223 |
} |
| 224 |
$term = $gmCore->_post( 'alb' ); |
| 225 |
$count = count( $selected_items ); |
| 226 |
if ( ( false !== $term ) && $count ) { |
| 227 |
if ( empty( $term ) ) { |
| 228 |
foreach ( $selected_items as $item ) { |
| 229 |
$gmDB->delete_gmedia_term_relationships( $item, 'gmedia_album' ); |
| 230 |
} |
| 231 |
// translators: number. |
| 232 |
$this->msg[] = sprintf( esc_html__( '%d item(s) updated with "No Album"', 'grand-media' ), intval( $count ) ); |
| 233 |
} else { |
| 234 |
$term_ids = array(); |
| 235 |
foreach ( $selected_items as $item ) { |
| 236 |
$result = $gmDB->set_gmedia_terms( $item, $term, 'gmedia_album', $append = 0 ); |
| 237 |
if ( is_wp_error( $result ) ) { |
| 238 |
$this->error[] = $result; |
| 239 |
} elseif ( $result ) { |
| 240 |
foreach ( $result as $t_id ) { |
| 241 |
$term_ids[ $t_id ][] = $item; |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
if ( ! empty( $term_ids ) ) { |
| 246 |
global $wpdb; |
| 247 |
|
| 248 |
foreach ( $term_ids as $term_id => $item_ids ) { |
| 249 |
$term = $gmDB->get_term( $term_id ); |
| 250 |
if ( isset( $_POST['status_global'] ) ) { |
| 251 |
$values = array(); |
| 252 |
foreach ( $selected_items as $item ) { |
| 253 |
$values[] = $wpdb->prepare( '%d', $item ); |
| 254 |
} |
| 255 |
if ( $values ) { |
| 256 |
$status = esc_sql( $term->status ); |
| 257 |
// phpcs:ignore |
| 258 |
if ( false === $wpdb->query( "UPDATE {$wpdb->prefix}gmedia SET status = '{$status}' WHERE ID IN (" . join( ',', $values ) . ')' ) ) { |
| 259 |
$this->error[] = esc_html__( 'Could not update statuses for gmedia items in the database' , 'grand-media'); |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
// translators: 1 - album name; 2 - number. |
| 264 |
$this->msg[] = sprintf( esc_html__( 'Album `%1$s` assigned to %2$d item(s)', 'grand-media' ), esc_html( $term->name ), count( $item_ids ) ); |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
$this->clear_selected_items( $cookie_key ); |
| 270 |
$this->selected_items = $this->selected_items( self::$cookie_key ); |
| 271 |
} |
| 272 |
} else { |
| 273 |
$this->error[] = esc_html__( 'You are not allowed to assign terms', 'grand-media' ); |
| 274 |
} |
| 275 |
} |
| 276 |
if ( isset( $_POST['assign_category'] ) ) { |
| 277 |
check_admin_referer( 'gmedia_action', '_wpnonce_action' ); |
| 278 |
if ( $gmCore->caps['gmedia_terms'] ) { |
| 279 |
$cookie_key = $gmCore->_post( 'cookie_key', self::$cookie_key ); |
| 280 |
$ids = $this->selected_items( $cookie_key ); |
| 281 |
if ( ! $gmCore->caps['gmedia_edit_others_media'] ) { |
| 282 |
$selected_items = $gmDB->get_gmedias( array( 'fields' => 'ids', 'author' => $user_ID, 'gmedia__in' => $ids ) ); |
| 283 |
if ( count( $selected_items ) < count( $ids ) ) { |
| 284 |
$this->error[] = esc_html__( 'You are not allowed to edit others media', 'grand-media' ); |
| 285 |
} |
| 286 |
} else { |
| 287 |
$selected_items = $ids; |
| 288 |
} |
| 289 |
$term = $gmCore->_post( 'cat_names' ); |
| 290 |
$term = explode( ',', $term ); |
| 291 |
$count = count( $selected_items ); |
| 292 |
if ( ! empty( $term ) && $count ) { |
| 293 |
foreach ( $selected_items as $item ) { |
| 294 |
$result = $gmDB->set_gmedia_terms( $item, $term, 'gmedia_category', $append = 1 ); |
| 295 |
if ( is_wp_error( $result ) ) { |
| 296 |
$this->error[] = $result; |
| 297 |
$count --; |
| 298 |
} elseif ( ! $result ) { |
| 299 |
$count --; |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
// translators: number. |
| 304 |
$this->msg[] = sprintf( esc_html__( 'Categories assigned to %d image(s).', 'grand-media' ), intval( $count ) ); |
| 305 |
|
| 306 |
$this->clear_selected_items( $cookie_key ); |
| 307 |
$this->selected_items = $this->selected_items( self::$cookie_key ); |
| 308 |
} |
| 309 |
} else { |
| 310 |
$this->error[] = esc_html__( 'You are not allowed to assign terms', 'grand-media' ); |
| 311 |
} |
| 312 |
} |
| 313 |
if ( isset( $_POST['unassign_category'] ) ) { |
| 314 |
check_admin_referer( 'gmedia_action', '_wpnonce_action' ); |
| 315 |
$term = $gmCore->_post( 'category_id' ); |
| 316 |
if ( $term && $gmCore->caps['gmedia_terms'] ) { |
| 317 |
$cookie_key = $gmCore->_post( 'cookie_key', self::$cookie_key ); |
| 318 |
$ids = $this->selected_items( $cookie_key ); |
| 319 |
if ( ! $gmCore->caps['gmedia_edit_others_media'] ) { |
| 320 |
$selected_items = $gmDB->get_gmedias( array( 'fields' => 'ids', 'author' => $user_ID, 'gmedia__in' => $ids ) ); |
| 321 |
if ( count( $selected_items ) < count( $ids ) ) { |
| 322 |
$this->error[] = esc_html__( 'You are not allowed to edit others media', 'grand-media' ); |
| 323 |
} |
| 324 |
} else { |
| 325 |
$selected_items = $ids; |
| 326 |
} |
| 327 |
$term = array_map( 'intval', $term ); |
| 328 |
$count = count( $selected_items ); |
| 329 |
if ( $count ) { |
| 330 |
foreach ( $selected_items as $item ) { |
| 331 |
$result = $gmDB->set_gmedia_terms( $item, $term, 'gmedia_category', $append = - 1 ); |
| 332 |
if ( is_wp_error( $result ) ) { |
| 333 |
$this->error[] = $result; |
| 334 |
$count --; |
| 335 |
} elseif ( ! $result ) { |
| 336 |
$count --; |
| 337 |
} |
| 338 |
} |
| 339 |
// translators: 1 - number; 2 - number. |
| 340 |
$this->msg[] = sprintf( esc_html__( '%1$d category(ies) deleted from %2$d item(s)', 'grand-media' ), count( $term ), (int) $count ); |
| 341 |
|
| 342 |
$this->clear_selected_items( $cookie_key ); |
| 343 |
$this->selected_items = $this->selected_items( self::$cookie_key ); |
| 344 |
} |
| 345 |
} else { |
| 346 |
$this->error[] = esc_html__( 'You are not allowed to assign terms', 'grand-media' ); |
| 347 |
} |
| 348 |
} |
| 349 |
if ( isset( $_POST['add_tags'] ) ) { |
| 350 |
check_admin_referer( 'gmedia_action', '_wpnonce_action' ); |
| 351 |
if ( ! $gmCore->caps['gmedia_terms'] ) { |
| 352 |
$this->error[] = esc_html__( 'You are not allowed to assign terms', 'grand-media' ); |
| 353 |
} else { |
| 354 |
$term = $gmCore->_post( 'tag_names' ); |
| 355 |
$iptc_tags = $gmCore->_post( 'iptc_tags' ); |
| 356 |
if ( $term || $iptc_tags ) { |
| 357 |
$cookie_key = $gmCore->_post( 'cookie_key', self::$cookie_key ); |
| 358 |
$ids = $this->selected_items( $cookie_key ); |
| 359 |
if ( ! $gmCore->caps['gmedia_edit_others_media'] ) { |
| 360 |
$selected_items = $gmDB->get_gmedias( array( 'fields' => 'ids', 'author' => $user_ID, 'gmedia__in' => $ids ) ); |
| 361 |
if ( count( $selected_items ) < count( $ids ) ) { |
| 362 |
$this->error[] = esc_html__( 'You are not allowed to edit others media', 'grand-media' ); |
| 363 |
} |
| 364 |
} else { |
| 365 |
$selected_items = $ids; |
| 366 |
} |
| 367 |
$term = explode( ',', $term ); |
| 368 |
$count = count( $selected_items ); |
| 369 |
if ( $count ) { |
| 370 |
foreach ( $selected_items as $item ) { |
| 371 |
$_term = $term; |
| 372 |
if ( $iptc_tags ) { |
| 373 |
$_metadata = $gmDB->get_metadata( 'gmedia', $item, '_metadata', true ); |
| 374 |
if ( isset( $_metadata['image_meta']['keywords'] ) && is_array( $_metadata['image_meta']['keywords'] ) ) { |
| 375 |
$_term = array_merge( $_metadata['image_meta']['keywords'], $term ); |
| 376 |
} |
| 377 |
} |
| 378 |
$result = $gmDB->set_gmedia_terms( $item, $_term, 'gmedia_tag', $append = 1 ); |
| 379 |
if ( is_wp_error( $result ) ) { |
| 380 |
$this->error[] = $result; |
| 381 |
$count --; |
| 382 |
} elseif ( ! $result ) { |
| 383 |
$count --; |
| 384 |
} |
| 385 |
} |
| 386 |
// translators: number. |
| 387 |
$this->msg[] = sprintf( esc_html__( 'Tags added to %d item(s)', 'grand-media' ), (int) $count ); |
| 388 |
|
| 389 |
$this->clear_selected_items( $cookie_key ); |
| 390 |
$this->selected_items = $this->selected_items( self::$cookie_key ); |
| 391 |
} |
| 392 |
} else { |
| 393 |
$this->error[] = esc_html__( 'No tags specified', 'grand-media' ); |
| 394 |
} |
| 395 |
} |
| 396 |
} |
| 397 |
if ( isset( $_POST['delete_tags'] ) ) { |
| 398 |
check_admin_referer( 'gmedia_action', '_wpnonce_action' ); |
| 399 |
$term = $gmCore->_post( 'tag_id' ); |
| 400 |
if ( $term && $gmCore->caps['gmedia_terms'] ) { |
| 401 |
$cookie_key = $gmCore->_post( 'cookie_key', self::$cookie_key ); |
| 402 |
$ids = $this->selected_items( $cookie_key ); |
| 403 |
if ( ! $gmCore->caps['gmedia_edit_others_media'] ) { |
| 404 |
$selected_items = $gmDB->get_gmedias( array( 'fields' => 'ids', 'author' => $user_ID, 'gmedia__in' => $ids ) ); |
| 405 |
if ( count( $selected_items ) < count( $ids ) ) { |
| 406 |
$this->error[] = esc_html__( 'You are not allowed to edit others media', 'grand-media' ); |
| 407 |
} |
| 408 |
} else { |
| 409 |
$selected_items = $ids; |
| 410 |
} |
| 411 |
$term = array_map( 'intval', $term ); |
| 412 |
$count = count( $selected_items ); |
| 413 |
if ( $count ) { |
| 414 |
foreach ( $selected_items as $item ) { |
| 415 |
$result = $gmDB->set_gmedia_terms( $item, $term, 'gmedia_tag', $append = - 1 ); |
| 416 |
if ( is_wp_error( $result ) ) { |
| 417 |
$this->error[] = $result; |
| 418 |
$count --; |
| 419 |
} elseif ( ! $result ) { |
| 420 |
$count --; |
| 421 |
} |
| 422 |
} |
| 423 |
// translators: 1 - number; 2 - number. |
| 424 |
$this->msg[] = sprintf( esc_html__( '%1$d tag(s) deleted from %2$d item(s)', 'grand-media' ), count( $term ), (int) $count ); |
| 425 |
|
| 426 |
$this->clear_selected_items( $cookie_key ); |
| 427 |
$this->selected_items = $this->selected_items( self::$cookie_key ); |
| 428 |
} |
| 429 |
} else { |
| 430 |
$this->error[] = esc_html__( 'You are not allowed to assign terms', 'grand-media' ); |
| 431 |
} |
| 432 |
} |
| 433 |
if ( isset( $_POST['batch_edit'] ) ) { |
| 434 |
check_admin_referer( 'gmedia_action', '_wpnonce_action' ); |
| 435 |
if ( $gmCore->caps['gmedia_edit_media'] ) { |
| 436 |
$cookie_key = $gmCore->_post( 'cookie_key', self::$cookie_key ); |
| 437 |
$ids = $this->selected_items( $cookie_key ); |
| 438 |
if ( ! $gmCore->caps['gmedia_edit_others_media'] ) { |
| 439 |
$selected_items = $gmDB->get_gmedias( array( 'fields' => 'ids', 'author' => $user_ID, 'gmedia__in' => $ids ) ); |
| 440 |
if ( count( $selected_items ) < count( $ids ) ) { |
| 441 |
$this->error[] = esc_html__( 'You are not allowed to edit others media', 'grand-media' ); |
| 442 |
} |
| 443 |
} else { |
| 444 |
$selected_items = $ids; |
| 445 |
} |
| 446 |
$count = count( $selected_items ); |
| 447 |
if ( $count ) { |
| 448 |
$batch_data = array(); |
| 449 |
$b_filename = $gmCore->_post( 'batch_filename' ); |
| 450 |
$b_title = $gmCore->_post( 'batch_title' ); |
| 451 |
$b_description = $gmCore->_post( 'batch_description' ); |
| 452 |
$b_link = $gmCore->_post( 'batch_link' ); |
| 453 |
$b_status = $gmCore->_post( 'batch_status' ); |
| 454 |
$b_comment_status = $gmCore->_post( 'batch_comment_status' ); |
| 455 |
if ( $b_status ) { |
| 456 |
$batch_data['status'] = $b_status; |
| 457 |
} |
| 458 |
if ( $b_comment_status ) { |
| 459 |
$batch_data['comment_status'] = $b_comment_status; |
| 460 |
} |
| 461 |
$b_author = $gmCore->_post( 'batch_author' ); |
| 462 |
if ( $b_author && ( '-1' !== $b_author ) ) { |
| 463 |
$batch_data['author'] = $b_author; |
| 464 |
} |
| 465 |
$i = 0; |
| 466 |
foreach ( $selected_items as $item ) { |
| 467 |
$id = (int) $item; |
| 468 |
$gmedia = $gmDB->get_gmedia( $id, ARRAY_A ); |
| 469 |
if ( ! $gmedia ) { |
| 470 |
continue; |
| 471 |
} |
| 472 |
$item_author = (int) $gmedia['author']; |
| 473 |
|
| 474 |
if ( 'custom' === $b_filename && ( $gmCore->caps['gmedia_delete_others_media'] || ( $item_author === $user_ID ) ) ) { |
| 475 |
$filename_custom = $gmCore->_post( 'batch_filename_custom' ); |
| 476 |
if ( ! empty( $filename_custom ) && ( '{filename}' !== $filename_custom ) ) { |
| 477 |
|
| 478 |
$gmuid = pathinfo( $gmedia['gmuid'] ); |
| 479 |
|
| 480 |
$filename_vars = array( '{filename}' => $gmuid['filename'], '{id}' => $gmedia['ID'] ); |
| 481 |
if ( preg_match_all( '/{index[:]?(\d+)?}/', $filename_custom, $matches_all ) ) { |
| 482 |
foreach ( $matches_all[0] as $key => $matches ) { |
| 483 |
$strlen = strlen( $matches_all[1][ $key ] ); |
| 484 |
$index = intval( $matches_all[1][ $key ] ) + $i; |
| 485 |
$index = sprintf( "%0{$strlen}d", $index ); |
| 486 |
$filename_vars[ $matches ] = $index; |
| 487 |
} |
| 488 |
} |
| 489 |
$filename_custom = strtr( $filename_custom, $filename_vars ); |
| 490 |
|
| 491 |
$filename_custom = preg_replace( '/[^a-z0-9_\.-]+/i', '_', $filename_custom ); |
| 492 |
if ( $filename_custom && $filename_custom !== $gmuid['filename'] ) { |
| 493 |
$fileinfo = $gmCore->fileinfo( $filename_custom . '.' . $gmuid['extension'] ); |
| 494 |
if ( false !== $fileinfo ) { |
| 495 |
if ( 'image' === $fileinfo['dirname'] ) { |
| 496 |
/** WordPress Image Administration API */ |
| 497 |
require_once ABSPATH . 'wp-admin/includes/image.php'; |
| 498 |
|
| 499 |
if ( file_is_displayable_image( $fileinfo['dirpath'] . '/' . $gmedia['gmuid'] ) ) { |
| 500 |
if ( is_file( $fileinfo['dirpath_original'] . '/' . $gmedia['gmuid'] ) ) { |
| 501 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.rename_rename -- Atomic local rename must preserve an existing destination on failure; WP_Filesystem::move deletes it first. |
| 502 |
@rename( $fileinfo['dirpath_original'] . '/' . $gmedia['gmuid'], $fileinfo['filepath_original'] ); |
| 503 |
} |
| 504 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.rename_rename -- Atomic local rename must preserve an existing destination on failure; WP_Filesystem::move deletes it first. |
| 505 |
@rename( $fileinfo['dirpath_thumb'] . '/' . $gmedia['gmuid'], $fileinfo['filepath_thumb'] ); |
| 506 |
} |
| 507 |
} |
| 508 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.rename_rename -- Atomic local rename must preserve an existing destination on failure; WP_Filesystem::move deletes it first. |
| 509 |
if ( @rename( $fileinfo['dirpath'] . '/' . $gmedia['gmuid'], $fileinfo['filepath'] ) ) { |
| 510 |
$gmedia['gmuid'] = $fileinfo['basename']; |
| 511 |
$batch_data['gmuid'] = $fileinfo['basename']; |
| 512 |
} |
| 513 |
} |
| 514 |
} |
| 515 |
} |
| 516 |
} |
| 517 |
switch ( $b_title ) { |
| 518 |
case 'empty': |
| 519 |
$batch_data['title'] = ''; |
| 520 |
break; |
| 521 |
case 'filename': |
| 522 |
$title = pathinfo( $gmedia['gmuid'], PATHINFO_FILENAME ); |
| 523 |
$batch_data['title'] = str_replace( '_', ' ', $title ); |
| 524 |
if ( $gmGallery->options['name2title_capitalize'] ) { |
| 525 |
$batch_data['title'] = $gmCore->mb_ucwords_utf8( $batch_data['title'] ); |
| 526 |
} |
| 527 |
break; |
| 528 |
case 'custom': |
| 529 |
$title_custom = $gmCore->_post( 'batch_title_custom' ); |
| 530 |
if ( false !== $title_custom ) { |
| 531 |
$batch_data['title'] = $title_custom; |
| 532 |
} |
| 533 |
break; |
| 534 |
} |
| 535 |
switch ( $b_description ) { |
| 536 |
case 'empty': |
| 537 |
$batch_data['description'] = ''; |
| 538 |
break; |
| 539 |
case 'metadata': |
| 540 |
$metatext = $gmCore->metadata_text( $id ); |
| 541 |
if ( $gmedia['description'] ) { |
| 542 |
$gmedia['description'] .= "\n"; |
| 543 |
} |
| 544 |
$batch_data['description'] = $gmedia['description'] . $metatext; |
| 545 |
break; |
| 546 |
case 'custom': |
| 547 |
$description_custom = $gmCore->_post( 'batch_description_custom' ); |
| 548 |
if ( false !== $description_custom ) { |
| 549 |
$what_description_custom = $gmCore->_post( 'what_description_custom' ); |
| 550 |
if ( 'replace' === $what_description_custom ) { |
| 551 |
$batch_data['description'] = $description_custom; |
| 552 |
} elseif ( 'append' === $what_description_custom ) { |
| 553 |
$batch_data['description'] = $gmedia['description'] . $description_custom; |
| 554 |
} elseif ( 'prepend' === $what_description_custom ) { |
| 555 |
$batch_data['description'] = $description_custom . $gmedia['description']; |
| 556 |
} |
| 557 |
} |
| 558 |
break; |
| 559 |
} |
| 560 |
switch ( $b_link ) { |
| 561 |
case 'empty': |
| 562 |
$batch_data['link'] = ''; |
| 563 |
break; |
| 564 |
case 'self': |
| 565 |
$fileinfo = $gmCore->fileinfo( $gmedia['gmuid'], false ); |
| 566 |
$fileurl = is_file( $fileinfo['filepath_original'] ) ? $fileinfo['fileurl_original'] : $fileinfo['fileurl']; |
| 567 |
$batch_data['link'] = $fileurl; |
| 568 |
break; |
| 569 |
case 'custom': |
| 570 |
$link_custom = $gmCore->_post( 'batch_link_custom' ); |
| 571 |
if ( false !== $link_custom ) { |
| 572 |
$batch_data['link'] = $link_custom; |
| 573 |
} |
| 574 |
break; |
| 575 |
} |
| 576 |
if ( ! empty( $batch_data ) ) { |
| 577 |
$batch_data['modified'] = current_time( 'mysql' ); |
| 578 |
$gmedia_data = array_merge( $gmedia, $batch_data ); |
| 579 |
$gmDB->insert_gmedia( $gmedia_data ); |
| 580 |
} else { |
| 581 |
$count --; |
| 582 |
} |
| 583 |
|
| 584 |
$i ++; |
| 585 |
} |
| 586 |
// translators: number. |
| 587 |
$this->msg[] = sprintf( esc_html__( '%d item(s) updated successfully', 'grand-media' ), $count ); |
| 588 |
|
| 589 |
$this->clear_selected_items( $cookie_key ); |
| 590 |
$this->selected_items = $this->selected_items( self::$cookie_key ); |
| 591 |
} |
| 592 |
} else { |
| 593 |
$this->error[] = esc_html__( 'You are not allowed to edit media', 'grand-media' ); |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
if ( $do_gmedia ) { |
| 598 |
if ( 'unassign_album' === $do_gmedia ) { |
| 599 |
check_admin_referer( 'gmedia_action', '_wpnonce_action' ); |
| 600 |
if ( $gmCore->caps['gmedia_terms'] ) { |
| 601 |
$cookie_key = $gmCore->_post( 'cookie_key', self::$cookie_key ); |
| 602 |
$ids = $this->selected_items( $cookie_key ); |
| 603 |
if ( ! $gmCore->caps['gmedia_edit_others_media'] ) { |
| 604 |
$selected_items = $gmDB->get_gmedias( array( 'fields' => 'ids', 'author' => $user_ID, 'gmedia__in' => $ids ) ); |
| 605 |
if ( count( $selected_items ) < count( $ids ) ) { |
| 606 |
$this->error[] = esc_html__( 'You are not allowed to edit others media', 'grand-media' ); |
| 607 |
} |
| 608 |
} else { |
| 609 |
$selected_items = $ids; |
| 610 |
} |
| 611 |
$count = count( $selected_items ); |
| 612 |
if ( $count ) { |
| 613 |
foreach ( $selected_items as $item ) { |
| 614 |
$gmDB->delete_gmedia_term_relationships( $item, 'gmedia_album' ); |
| 615 |
} |
| 616 |
// translators: number. |
| 617 |
$this->msg[] = sprintf( esc_html__( '%d item(s) updated with "No Album"', 'grand-media' ), (int) $count ); |
| 618 |
set_transient( 'gmedia_action_msg', $this->msg, 30 ); |
| 619 |
|
| 620 |
$this->clear_selected_items( $cookie_key ); |
| 621 |
$this->selected_items = $this->selected_items( self::$cookie_key ); |
| 622 |
} |
| 623 |
} else { |
| 624 |
$this->error[] = esc_html__( 'You are not allowed to assign terms', 'grand-media' ); |
| 625 |
set_transient( 'gmedia_action_error', $this->error, 30 ); |
| 626 |
} |
| 627 |
} |
| 628 |
if ( 'update_meta' === $do_gmedia ) { |
| 629 |
check_admin_referer( 'gmedia_action', '_wpnonce_action' ); |
| 630 |
if ( $gmCore->caps['gmedia_edit_media'] ) { |
| 631 |
$cookie_key = $gmCore->_post( 'cookie_key', self::$cookie_key ); |
| 632 |
$selected_items = $this->selected_items( $cookie_key ); |
| 633 |
$count = count( $selected_items ); |
| 634 |
if ( $count ) { |
| 635 |
foreach ( $selected_items as $item ) { |
| 636 |
$id = (int) $item; |
| 637 |
$media_metadata = $gmDB->generate_gmedia_metadata( $id ); |
| 638 |
$gmDB->update_metadata( 'gmedia', $id, '_metadata', $media_metadata ); |
| 639 |
if ( ! empty( $media_metadata['image_meta']['created_timestamp'] ) ) { |
| 640 |
$gmDB->update_metadata( 'gmedia', $id, '_created_timestamp', $media_metadata['image_meta']['created_timestamp'] ); |
| 641 |
} |
| 642 |
if ( ! empty( $media_metadata['image_meta']['GPS'] ) ) { |
| 643 |
$gmDB->update_metadata( 'gmedia', $id, '_gps', $media_metadata['image_meta']['GPS'] ); |
| 644 |
} |
| 645 |
} |
| 646 |
// translators: number. |
| 647 |
$this->msg[] = sprintf( esc_html__( '%d item(s) updated successfully', 'grand-media' ), (int) $count ); |
| 648 |
set_transient( 'gmedia_action_msg', $this->msg, 30 ); |
| 649 |
} |
| 650 |
$this->clear_selected_items( $cookie_key ); |
| 651 |
$this->selected_items = $this->selected_items( self::$cookie_key ); |
| 652 |
} else { |
| 653 |
$this->error[] = esc_html__( 'You are not allowed to edit media', 'grand-media' ); |
| 654 |
set_transient( 'gmedia_action_error', $this->error, 30 ); |
| 655 |
} |
| 656 |
} |
| 657 |
if ( 'recreate' === $do_gmedia ) { |
| 658 |
check_admin_referer( 'gmedia_action', '_wpnonce_action' ); |
| 659 |
if ( $gmCore->caps['gmedia_edit_media'] ) { |
| 660 |
$cookie_key = $gmCore->_post( 'cookie_key', self::$cookie_key ); |
| 661 |
$selected_items = $this->selected_items( $cookie_key ); |
| 662 |
$count = count( $selected_items ); |
| 663 |
if ( $count ) { |
| 664 |
if ( ! $gmCore->caps['gmedia_edit_others_media'] ) { |
| 665 |
$edit_items = $gmDB->get_gmedias( array( 'fields' => 'ids', 'author' => $user_ID, 'mime_type' => 'image', 'gmedia__in' => $selected_items ) ); |
| 666 |
$selected_items = $edit_items; |
| 667 |
} else { |
| 668 |
$selected_items = $gmDB->get_gmedias( array( 'fields' => 'ids', 'mime_type' => 'image', 'gmedia__in' => $selected_items ) ); |
| 669 |
} |
| 670 |
$count = count( $selected_items ); |
| 671 |
if ( $count ) { |
| 672 |
$ajax_operations = get_option( 'gmedia_ajax_long_operations', array() ); |
| 673 |
foreach ( $selected_items as $si ) { |
| 674 |
$ajax_operations['gmedia_recreate_images'][ $si ] = $si; |
| 675 |
} |
| 676 |
update_option( 'gmedia_ajax_long_operations', $ajax_operations ); |
| 677 |
// translators: number. |
| 678 |
$this->msg[] = sprintf( esc_html__( 'You\'ve added %d image(s) to the re-creation queue.', 'grand-media' ), (int) $count ); |
| 679 |
set_transient( 'gmedia_action_msg', $this->msg, 30 ); |
| 680 |
} |
| 681 |
} |
| 682 |
$this->clear_selected_items( $cookie_key ); |
| 683 |
$this->selected_items = $this->selected_items( self::$cookie_key ); |
| 684 |
} else { |
| 685 |
$this->error[] = esc_html__( 'You are not allowed to edit media', 'grand-media' ); |
| 686 |
set_transient( 'gmedia_action_error', $this->error, 30 ); |
| 687 |
} |
| 688 |
} |
| 689 |
} |
| 690 |
} |
| 691 |
if ( 'duplicate' === $do_gmedia ) { |
| 692 |
check_admin_referer( 'gmedia_action', '_wpnonce_action' ); |
| 693 |
if ( $gmCore->caps['gmedia_upload'] || $gmCore->caps['gmedia_import'] ) { |
| 694 |
$ids = $gmCore->_get( 'ids', 'selected' ); |
| 695 |
$cookie_key = $gmCore->_post( 'cookie_key', self::$cookie_key ); |
| 696 |
$selected_items = ( 'selected' === $ids ) ? $this->selected_items( $cookie_key ) : wp_parse_id_list( $ids ); |
| 697 |
if ( ! empty( $selected_items ) ) { |
| 698 |
$count = count( $selected_items ); |
| 699 |
if ( $count ) { |
| 700 |
foreach ( $selected_items as $gmid ) { |
| 701 |
$gmCore->duplicate_gmedia( $gmid ); |
| 702 |
} |
| 703 |
// translators: number. |
| 704 |
$this->msg[] = sprintf( esc_html__( '%d item was duplicated', 'grand-media' ), (int) $count ); |
| 705 |
set_transient( 'gmedia_action_msg', $this->msg, 30 ); |
| 706 |
} |
| 707 |
} |
| 708 |
} else { |
| 709 |
$this->error[] = esc_html__( 'You are not allowed to import files', 'grand-media' ); |
| 710 |
set_transient( 'gmedia_action_error', $this->error, 30 ); |
| 711 |
} |
| 712 |
} |
| 713 |
|
| 714 |
if ( 'delete' === $do_gmedia || 'delete__save_original' === $do_gmedia ) { |
| 715 |
check_admin_referer( 'gmedia_delete', '_wpnonce_delete' ); |
| 716 |
if ( $gmCore->caps['gmedia_delete_media'] ) { |
| 717 |
$ids = $gmCore->_get( 'ids', 'selected' ); |
| 718 |
$cookie_key = $gmCore->_post( 'cookie_key', self::$cookie_key ); |
| 719 |
$selected_items = ( 'selected' === $ids ) ? $this->selected_items( $cookie_key ) : wp_parse_id_list( $ids ); |
| 720 |
if ( ! empty( $selected_items ) ) { |
| 721 |
if ( ! $gmCore->caps['gmedia_delete_others_media'] ) { |
| 722 |
$delete_items = $gmDB->get_gmedias( array( 'fields' => 'ids', 'author' => $user_ID, 'gmedia__in' => $selected_items ) ); |
| 723 |
if ( count( $delete_items ) < count( $selected_items ) ) { |
| 724 |
$this->error[] = esc_html__( 'You are not allowed to delete others media', 'grand-media' ); |
| 725 |
} |
| 726 |
$selected_items = $delete_items; |
| 727 |
} |
| 728 |
$count = count( $selected_items ); |
| 729 |
if ( $count ) { |
| 730 |
$delete_original_file = ! ( 'delete__save_original' === $do_gmedia ); |
| 731 |
foreach ( $selected_items as $item ) { |
| 732 |
if ( ! $gmDB->delete_gmedia( (int) $item, $delete_original_file ) ) { |
| 733 |
$this->error[] = esc_html( "#{$item}: " . __( 'Error in deleting...', 'grand-media' ) ); |
| 734 |
$count --; |
| 735 |
} |
| 736 |
} |
| 737 |
if ( $count ) { |
| 738 |
if ( $delete_original_file ) { |
| 739 |
// translators: number. |
| 740 |
$this->msg[] = sprintf( esc_html__( '%d item(s) deleted successfully', 'grand-media' ), (int) $count ); |
| 741 |
} else { |
| 742 |
// translators: number. |
| 743 |
$this->msg[] = sprintf( esc_html__( '%d record(s) deleted from database successfully. Original file(s) safe', 'grand-media' ), (int) $count ); |
| 744 |
} |
| 745 |
} |
| 746 |
$this->selected_items = array_diff( $this->selected_items, $selected_items ); |
| 747 |
if ( empty( $this->selected_items ) ) { |
| 748 |
$this->clear_selected_items( self::$cookie_key ); |
| 749 |
} else { |
| 750 |
setcookie( self::$cookie_key, implode( '.', $this->selected_items ) ); |
| 751 |
} |
| 752 |
if ( $cookie_key !== self::$cookie_key ) { |
| 753 |
if ( 'selected' === $ids ) { |
| 754 |
$this->clear_selected_items( $cookie_key ); |
| 755 |
} else { |
| 756 |
$_selected_items = $this->selected_items( $cookie_key ); |
| 757 |
$_selected_items = array_diff( $_selected_items, $selected_items ); |
| 758 |
if ( empty( $_selected_items ) ) { |
| 759 |
$this->clear_selected_items( $cookie_key ); |
| 760 |
} else { |
| 761 |
setcookie( $cookie_key, implode( '.', $_selected_items ) ); |
| 762 |
} |
| 763 |
} |
| 764 |
} |
| 765 |
if ( ! empty( $this->stack_items ) ) { |
| 766 |
$this->stack_items = array_diff( $this->stack_items, $selected_items ); |
| 767 |
if ( empty( $this->stack_items ) ) { |
| 768 |
$this->clear_selected_items( "gmedia_{$user_ID}_libstack" ); |
| 769 |
|
| 770 |
} else { |
| 771 |
setcookie( "gmedia_{$user_ID}_libstack", implode( '.', $this->stack_items ) ); |
| 772 |
} |
| 773 |
} |
| 774 |
} |
| 775 |
} |
| 776 |
} else { |
| 777 |
$this->error[] = esc_html__( 'You are not allowed to delete files', 'grand-media' ); |
| 778 |
} |
| 779 |
if ( ! empty( $this->msg ) ) { |
| 780 |
set_transient( 'gmedia_action_msg', $this->msg, 30 ); |
| 781 |
} |
| 782 |
if ( ! empty( $this->error ) ) { |
| 783 |
set_transient( 'gmedia_action_error', $this->error, 30 ); |
| 784 |
} |
| 785 |
} |
| 786 |
if ( $do_gmedia ) { |
| 787 |
$_wpnonce = array(); |
| 788 |
foreach ( $_GET as $key => $value ) { |
| 789 |
if ( strpos( $key, '_wpnonce' ) !== false ) { |
| 790 |
$_wpnonce[ $key ] = $value; |
| 791 |
} |
| 792 |
} |
| 793 |
$remove_args = array_merge( array( 'do_gmedia', 'ids' ), $_wpnonce ); |
| 794 |
$location = remove_query_arg( $remove_args ); |
| 795 |
$location = add_query_arg( 'did_gmedia', $do_gmedia, $location ); |
| 796 |
wp_safe_redirect( $location ); |
| 797 |
exit; |
| 798 |
} |
| 799 |
if ( $gmCore->_get( 'did_gmedia' ) ) { |
| 800 |
$msg = get_transient( 'gmedia_action_msg' ); |
| 801 |
if ( $msg ) { |
| 802 |
delete_transient( 'gmedia_action_msg' ); |
| 803 |
$this->msg = (array) $msg; |
| 804 |
} |
| 805 |
$error = get_transient( 'gmedia_action_error' ); |
| 806 |
if ( $error ) { |
| 807 |
delete_transient( 'gmedia_action_error' ); |
| 808 |
$this->error = (array) $error; |
| 809 |
} |
| 810 |
} |
| 811 |
} |
| 812 |
|
| 813 |
/** |
| 814 |
* @return array |
| 815 |
*/ |
| 816 |
public function query_args() { |
| 817 |
global $gmCore, $gmDB, $gmGallery; |
| 818 |
|
| 819 |
if ( $this->edit_term ) { |
| 820 |
$per_page = $this->user_options["per_page_{$this->taxonomy}_edit"]; |
| 821 |
if ( 'album' === $this->taxterm ) { |
| 822 |
$alb_meta = $gmDB->get_metadata( 'gmedia_term', $this->edit_term ); |
| 823 |
$orderby = ! empty( $alb_meta['_orderby'][0] ) ? $alb_meta['_orderby'][0] : $gmGallery->options['in_album_orderby']; |
| 824 |
$order = ! empty( $alb_meta['_order'][0] ) ? $alb_meta['_order'][0] : $gmGallery->options['in_album_order']; |
| 825 |
} elseif ( 'category' === $this->taxterm ) { |
| 826 |
$cat_meta = $gmDB->get_metadata( 'gmedia_term', $this->edit_term ); |
| 827 |
$orderby = ! empty( $cat_meta['_orderby'][0] ) ? $cat_meta['_orderby'][0] : $gmGallery->options['in_category_orderby']; |
| 828 |
$order = ! empty( $cat_meta['_order'][0] ) ? $cat_meta['_order'][0] : $gmGallery->options['in_category_order']; |
| 829 |
} elseif ( 'tag' === $this->taxterm ) { |
| 830 |
$orderby = $gmGallery->options['in_tag_orderby']; |
| 831 |
$order = $gmGallery->options['in_tag_order']; |
| 832 |
} else { |
| 833 |
$orderby = $this->user_options['orderby_gmedia']; |
| 834 |
$order = $this->user_options['sortorder_gmedia']; |
| 835 |
} |
| 836 |
} else { |
| 837 |
$per_page = $this->user_options['per_page_gmedia']; |
| 838 |
$orderby = $this->user_options['orderby_gmedia']; |
| 839 |
$order = $this->user_options['sortorder_gmedia']; |
| 840 |
} |
| 841 |
|
| 842 |
$args['mime_type'] = $gmCore->_get( 'mime_type' ); |
| 843 |
// Anonymous callers (e.g. the nopriv gmedia_get_data endpoint) must never |
| 844 |
// reach draft/private media, regardless of a requested status parameter. |
| 845 |
$args['status'] = is_user_logged_in() ? $gmCore->_get( 'status' ) : 'publish'; |
| 846 |
$args['page'] = $gmCore->_get( 'pager' ); |
| 847 |
$args['per_page'] = $gmCore->_get( 'per_page', $per_page ); |
| 848 |
$args['author__in'] = parent::filter_by_author( $gmCore->_get( 'author__in', $gmCore->_get( 'author' ) ) ); |
| 849 |
$args['alb'] = $gmCore->_get( 'alb' ); |
| 850 |
$args['album__in'] = $gmCore->_get( 'album__in' ); |
| 851 |
$args['album__not_in'] = $gmCore->_get( 'album__not_in' ); |
| 852 |
$args['tag_id'] = $gmCore->_get( 'tag_id' ); |
| 853 |
$args['tag__in'] = $gmCore->_get( 'tag__in' ); |
| 854 |
$args['tag__and'] = $gmCore->_get( 'tag__and' ); |
| 855 |
$args['tag__not_in'] = $gmCore->_get( 'tag__not_in' ); |
| 856 |
$args['cat'] = $gmCore->_get( 'cat' ); |
| 857 |
$args['category__in'] = $gmCore->_get( 'category__in' ); |
| 858 |
$args['category__not_in'] = $gmCore->_get( 'category__not_in' ); |
| 859 |
$args['category__and'] = $gmCore->_get( 'category__and' ); |
| 860 |
$args['gmedia__in'] = $gmCore->_get( 'gmedia__in' ); |
| 861 |
$args['s'] = $gmCore->_get( 's' ); |
| 862 |
$args['orderby'] = $gmCore->_get( 'orderby', $orderby ); |
| 863 |
$args['order'] = $gmCore->_get( 'order', $order ); |
| 864 |
$args['terms_relation'] = $gmCore->_get( 'terms_relation' ); |
| 865 |
$args['limit'] = $gmCore->_get( 'limit' ); |
| 866 |
|
| 867 |
if ( 'duplicates' === $args['gmedia__in'] ) { |
| 868 |
$duplicates = $gmDB->get_duplicates(); |
| 869 |
if ( ! empty( $duplicates['duplicate_ids'] ) ) { |
| 870 |
$args['gmedia__in'] = $duplicates['duplicate_ids']; |
| 871 |
$args['orderby'] = 'gmedia__in'; |
| 872 |
|
| 873 |
setcookie( self::$cookie_key, implode( '.', $duplicates['duplicate_select'] ) ); |
| 874 |
$_COOKIE[ self::$cookie_key ] = implode( '.', $duplicates['duplicate_select'] ); |
| 875 |
$this->selected_items = $duplicates['duplicate_select']; |
| 876 |
} else { |
| 877 |
unset( $args['gmedia__in'] ); |
| 878 |
$this->msg[] = esc_html__( 'No duplicates in Gmedia Library', 'grand-media' ); |
| 879 |
} |
| 880 |
} |
| 881 |
|
| 882 |
if ( $args['s'] && ( '#' === substr( $args['s'], 0, 1 ) ) ) { |
| 883 |
$args['gmedia__in'] = substr( $args['s'], 1 ); |
| 884 |
$args['s'] = false; |
| 885 |
} |
| 886 |
|
| 887 |
$show_stack = false; |
| 888 |
if ( ( 'show' === $gmCore->_req( 'stack' ) ) && ! empty( $this->stack_items ) ) { |
| 889 |
$args['gmedia__in'] = $this->stack_items; |
| 890 |
$args['orderby'] = $gmCore->_get( 'orderby', 'gmedia__in' ); |
| 891 |
$args['order'] = $gmCore->_get( 'order', 'ASC' ); |
| 892 |
$show_stack = true; |
| 893 |
} |
| 894 |
if ( ( 'selected' === $gmCore->_req( 'filter' ) ) && ! empty( $this->selected_items ) ) { |
| 895 |
if ( $show_stack ) { |
| 896 |
$stack_items = wp_parse_id_list( $this->stack_items ); |
| 897 |
$selected_items = wp_parse_id_list( $this->selected_items ); |
| 898 |
$gmedia_in = array_intersect( $stack_items, $selected_items ); |
| 899 |
$args['gmedia__in'] = $gmedia_in; |
| 900 |
} else { |
| 901 |
$args['gmedia__in'] = $this->selected_items; |
| 902 |
$args['orderby'] = $gmCore->_get( 'orderby', 'gmedia__in' ); |
| 903 |
$args['order'] = $gmCore->_get( 'order', 'ASC' ); |
| 904 |
} |
| 905 |
} |
| 906 |
|
| 907 |
$query_args = apply_filters( 'gmedia_library_query_args', $args ); |
| 908 |
|
| 909 |
foreach ( $query_args as $key => $val ) { |
| 910 |
if ( empty( $val ) && ( '0' !== $val ) && ( 0 !== $val ) ) { |
| 911 |
unset( $query_args[ $key ] ); |
| 912 |
} |
| 913 |
} |
| 914 |
|
| 915 |
if ( ! empty( $query_args['author__in'] ) && $gmCore->caps['gmedia_show_others_media'] ) { |
| 916 |
$authors_names = $query_args['author__in']; |
| 917 |
foreach ( $authors_names as $i => $id ) { |
| 918 |
if ( (int) $id ) { |
| 919 |
$authors_names[ $i ] = get_the_author_meta( 'display_name', $id ); |
| 920 |
} |
| 921 |
} |
| 922 |
$this->filters['filter_author'] = array( |
| 923 |
'title' => __( 'Filter Author', 'grand-media' ), |
| 924 |
'filter' => $authors_names, |
| 925 |
); |
| 926 |
} |
| 927 |
|
| 928 |
$gmDB->gmedias_album_stuff( $query_args ); |
| 929 |
if ( ! empty( $query_args['album__in'] ) ) { |
| 930 |
if ( isset( $query_args['within_album'] ) ) { |
| 931 |
$filter_title = __( 'Exclude Album', 'grand-media' ); |
| 932 |
$albums_names = array(); |
| 933 |
if ( ! empty( $query_args['with_album__not_in'] ) ) { |
| 934 |
$albums_names = $gmDB->get_terms( 'gmedia_album', array( 'fields' => 'names', 'global' => $args['author__in'], 'include' => $query_args['with_album__not_in'] ) ); |
| 935 |
} |
| 936 |
$albums_names[] = __( 'Hide items without album', 'grand-media' ); |
| 937 |
} else { |
| 938 |
$filter_title = __( 'Filter Album', 'grand-media' ); |
| 939 |
$albums_names = $gmDB->get_terms( 'gmedia_album', array( 'fields' => 'names', 'global' => $args['author__in'], 'include' => $query_args['album__in'] ) ); |
| 940 |
} |
| 941 |
if ( ! empty( $albums_names ) ) { |
| 942 |
$this->filters['filter_albums'] = array( |
| 943 |
'title' => $filter_title, |
| 944 |
'filter' => $albums_names, |
| 945 |
); |
| 946 |
} |
| 947 |
} |
| 948 |
if ( ! empty( $query_args['album__not_in'] ) ) { |
| 949 |
if ( isset( $query_args['without_album'] ) ) { |
| 950 |
$filter_title = __( 'Filter Album', 'grand-media' ); |
| 951 |
$albums_names = array(); |
| 952 |
if ( ! empty( $query_args['with_album__in'] ) ) { |
| 953 |
$albums_names = $gmDB->get_terms( 'gmedia_album', array( 'fields' => 'names', 'global' => $args['author__in'], 'include' => $query_args['with_album__in'] ) ); |
| 954 |
} |
| 955 |
$albums_names[] = __( 'Show items without album', 'grand-media' ); |
| 956 |
} else { |
| 957 |
$filter_title = __( 'Exclude Album', 'grand-media' ); |
| 958 |
$albums_names = $gmDB->get_terms( 'gmedia_album', array( 'fields' => 'names', 'global' => $args['author__in'], 'include' => $query_args['album__not_in'] ) ); |
| 959 |
} |
| 960 |
if ( ! empty( $albums_names ) ) { |
| 961 |
$this->filters['exclude_albums'] = array( |
| 962 |
'title' => $filter_title, |
| 963 |
'filter' => $albums_names, |
| 964 |
); |
| 965 |
} |
| 966 |
} |
| 967 |
|
| 968 |
$gmDB->gmedias_category_stuff( $query_args ); |
| 969 |
if ( ! empty( $query_args['category__in'] ) ) { |
| 970 |
if ( isset( $query_args['within_category'] ) ) { |
| 971 |
$filter_title = __( 'Exclude Category', 'grand-media' ); |
| 972 |
$category_names = array(); |
| 973 |
if ( ! empty( $query_args['with_category__not_in'] ) ) { |
| 974 |
$category_names = $gmDB->get_terms( 'gmedia_category', array( 'fields' => 'names', 'include' => $query_args['with_category__not_in'] ) ); |
| 975 |
} |
| 976 |
$category_names[] = __( 'Hide items without categories', 'grand-media' ); |
| 977 |
} else { |
| 978 |
$filter_title = __( 'Filter Category', 'grand-media' ); |
| 979 |
$category_names = $gmDB->get_terms( 'gmedia_category', array( 'fields' => 'names', 'include' => $query_args['category__in'] ) ); |
| 980 |
} |
| 981 |
if ( ! empty( $category_names ) ) { |
| 982 |
$this->filters['filter_categories'] = array( |
| 983 |
'title' => $filter_title, |
| 984 |
'filter' => $category_names, |
| 985 |
); |
| 986 |
} |
| 987 |
} |
| 988 |
if ( ! empty( $query_args['category__not_in'] ) ) { |
| 989 |
if ( isset( $query_args['without_category'] ) ) { |
| 990 |
$filter_title = __( 'Filter Category', 'grand-media' ); |
| 991 |
$category_names = array(); |
| 992 |
if ( ! empty( $query_args['with_category__in'] ) ) { |
| 993 |
$category_names = $gmDB->get_terms( 'gmedia_category', array( 'fields' => 'names', 'include' => $query_args['with_category__in'] ) ); |
| 994 |
} |
| 995 |
$category_names[] = __( 'Show items without categories', 'grand-media' ); |
| 996 |
} else { |
| 997 |
$filter_title = __( 'Exclude Category', 'grand-media' ); |
| 998 |
$category_names = $gmDB->get_terms( 'gmedia_category', array( 'fields' => 'names', 'include' => $query_args['category__not_in'] ) ); |
| 999 |
} |
| 1000 |
if ( ! empty( $category_names ) ) { |
| 1001 |
$this->filters['exclude_categories'] = array( |
| 1002 |
'title' => $filter_title, |
| 1003 |
'filter' => $category_names, |
| 1004 |
); |
| 1005 |
} |
| 1006 |
} |
| 1007 |
|
| 1008 |
$gmDB->gmedias_tag_stuff( $query_args ); |
| 1009 |
if ( ! empty( $query_args['tag__in'] ) ) { |
| 1010 |
$tag_names = $gmDB->get_terms( 'gmedia_tag', array( 'fields' => 'names', 'include' => $query_args['tag__in'] ) ); |
| 1011 |
if ( ! empty( $tag_names ) ) { |
| 1012 |
$this->filters['filter_tags'] = array( |
| 1013 |
'title' => __( 'Filter Tag', 'grand-media' ), |
| 1014 |
'filter' => $tag_names, |
| 1015 |
); |
| 1016 |
} |
| 1017 |
} |
| 1018 |
if ( ! empty( $query_args['tag__not_in'] ) ) { |
| 1019 |
$tag_names = $gmDB->get_terms( 'gmedia_tag', array( 'fields' => 'names', 'include' => $query_args['tag__not_in'] ) ); |
| 1020 |
if ( ! empty( $tag_names ) ) { |
| 1021 |
$this->filters['exclude_tags'] = array( |
| 1022 |
'title' => __( 'Exclude Tag', 'grand-media' ), |
| 1023 |
'filter' => $tag_names, |
| 1024 |
); |
| 1025 |
} |
| 1026 |
} |
| 1027 |
|
| 1028 |
if ( ! empty( $args['terms_relation'] ) ) { |
| 1029 |
$this->filters['terms_relation'] = array( |
| 1030 |
'title' => __( 'Terms Relation', 'grand-media' ), |
| 1031 |
'filter' => array( wp_strip_all_tags( $args['terms_relation'] ) ), |
| 1032 |
); |
| 1033 |
} |
| 1034 |
|
| 1035 |
return $query_args; |
| 1036 |
} |
| 1037 |
} |
| 1038 |
|
| 1039 |
global $gmProcessorLibrary; |
| 1040 |
$gmProcessorLibrary = GmediaProcessor_Library::getMe(); |
| 1041 |
|