| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; |
| 4 |
} |
| 5 |
|
| 6 |
/** |
| 7 |
* Class GmediaProcessor |
| 8 |
*/ |
| 9 |
class GmediaProcessor { |
| 10 |
|
| 11 |
private static $me = null; |
| 12 |
|
| 13 |
public $user_options = array(); |
| 14 |
|
| 15 |
public $page; |
| 16 |
public $gmediablank; |
| 17 |
public $url; |
| 18 |
public $msg = array(); |
| 19 |
public $error = array(); |
| 20 |
|
| 21 |
public $display_mode; |
| 22 |
public $taxonomy; |
| 23 |
public $taxterm; |
| 24 |
public $edit_term; |
| 25 |
|
| 26 |
/** |
| 27 |
* initiate the manage page |
| 28 |
*/ |
| 29 |
public function __construct() { |
| 30 |
global $pagenow, $gmCore; |
| 31 |
// GET variables. |
| 32 |
$this->page = $gmCore->_get( 'page' ); |
| 33 |
$this->url = add_query_arg( array( 'page' => $this->page ), admin_url( 'admin.php' ) ); |
| 34 |
if ( 'media.php' === $pagenow ) { |
| 35 |
add_filter( 'wp_redirect', array( $this, 'redirect' ), 10, 2 ); |
| 36 |
} |
| 37 |
if ( 'edit-comments.php' === $pagenow ) { |
| 38 |
add_filter( 'get_comment_text', array( $this, 'gmedia_comment_text' ), 10, 3 ); |
| 39 |
} |
| 40 |
|
| 41 |
add_action( 'init', array( $this, 'controller' ) ); |
| 42 |
|
| 43 |
if ( ! $this->page || strpos( $this->page, 'GrandMedia' ) === false ) { |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
$this->gmediablank = $gmCore->_get( 'gmediablank' ); |
| 48 |
if ( $this->gmediablank ) { |
| 49 |
$this->url = add_query_arg( array( 'gmediablank' => $this->gmediablank ), $this->url ); |
| 50 |
} |
| 51 |
|
| 52 |
switch ( $this->page ) { |
| 53 |
case 'GrandMedia_Albums': |
| 54 |
$this->taxonomy = 'gmedia_album'; |
| 55 |
break; |
| 56 |
case 'GrandMedia_Categories': |
| 57 |
$this->taxonomy = 'gmedia_category'; |
| 58 |
break; |
| 59 |
case 'GrandMedia_Tags': |
| 60 |
$this->taxonomy = 'gmedia_tag'; |
| 61 |
break; |
| 62 |
case 'GrandMedia_Galleries': |
| 63 |
$this->taxonomy = 'gmedia_gallery'; |
| 64 |
break; |
| 65 |
} |
| 66 |
if ( $this->taxonomy ) { |
| 67 |
$this->taxterm = str_replace( 'gmedia_', '', $this->taxonomy ); |
| 68 |
$this->edit_term = $gmCore->_get( 'edit_term' ); |
| 69 |
} |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* @param string $key |
| 75 |
* @param string $post_key |
| 76 |
* |
| 77 |
* @return array |
| 78 |
*/ |
| 79 |
public static function selected_items( $key, $post_key = 'selected_items' ) { |
| 80 |
|
| 81 |
$selected_items = array(); |
| 82 |
if ( $key ) { |
| 83 |
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- Read-only selection parser; library/term/gallery mutation handlers verify their action nonce and permissions separately. |
| 84 |
if ( isset( $_POST[ $post_key ] ) ) { |
| 85 |
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- Also used to display selected items; this helper performs no state changes. |
| 86 |
$sel_string = sanitize_text_field( wp_unslash( $_POST[ $post_key ] ) ); |
| 87 |
$selected_items = array_filter( explode( ',', $sel_string ), 'is_numeric' ); |
| 88 |
} elseif ( isset( $_COOKIE[ $key ] ) ) { |
| 89 |
$sel_string = sanitize_text_field( wp_unslash( $_COOKIE[ $key ] ) ); |
| 90 |
$selected_items = array_filter( explode( '.', $sel_string ), 'is_numeric' ); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
return $selected_items; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* @param bool|string|array $author_id_list |
| 99 |
* |
| 100 |
* @return array |
| 101 |
*/ |
| 102 |
public static function filter_by_author( $author_id_list = false ) { |
| 103 |
global $user_ID, $gmCore; |
| 104 |
|
| 105 |
if ( false === $author_id_list ) { |
| 106 |
$author = false; |
| 107 |
if ( ! $gmCore->caps['gmedia_show_others_media'] ) { |
| 108 |
$author = array( $user_ID, 0 ); |
| 109 |
} |
| 110 |
} else { |
| 111 |
$author = wp_parse_id_list( $author_id_list ); |
| 112 |
if ( ! $gmCore->caps['gmedia_show_others_media'] ) { |
| 113 |
$author = array_intersect( array( $user_ID, 0 ), $author ); |
| 114 |
if ( empty( $author ) ) { |
| 115 |
// The requested authors are all off-limits for this user. Fall |
| 116 |
// back to their own scope: an empty list would be read by the |
| 117 |
// query layer as "no author restriction" and leak everyone. |
| 118 |
$author = array( $user_ID, 0 ); |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
return $author; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Autoloader |
| 128 |
*/ |
| 129 |
public static function autoload() { |
| 130 |
global $gmCore; |
| 131 |
$path_ = GMEDIA_ABSPATH . 'admin/processor/class.processor.'; |
| 132 |
$page = $gmCore->_get( 'page', '' ); |
| 133 |
switch ( $page ) { |
| 134 |
case 'GrandMedia': |
| 135 |
/** @var $gmProcessorLibrary */ |
| 136 |
include_once $path_ . 'library.php'; |
| 137 |
|
| 138 |
return $gmProcessorLibrary; |
| 139 |
case 'GrandMedia_AddMedia': |
| 140 |
/** @var $gmProcessorAddMedia */ |
| 141 |
include_once $path_ . 'addmedia.php'; |
| 142 |
|
| 143 |
return $gmProcessorAddMedia; |
| 144 |
case 'GrandMedia_Albums': |
| 145 |
case 'GrandMedia_Categories': |
| 146 |
/** @var $gmProcessorTerms */ |
| 147 |
include_once $path_ . 'terms.php'; |
| 148 |
/** @var $gmProcessorLibrary */ |
| 149 |
include_once $path_ . 'library.php'; |
| 150 |
|
| 151 |
return $gmProcessorTerms; |
| 152 |
case 'GrandMedia_Tags': |
| 153 |
/** @var $gmProcessorTerms */ |
| 154 |
include_once $path_ . 'terms.php'; |
| 155 |
|
| 156 |
return $gmProcessorTerms; |
| 157 |
case 'GrandMedia_Galleries': |
| 158 |
/** @var $gmProcessorGalleries */ |
| 159 |
include_once $path_ . 'galleries.php'; |
| 160 |
|
| 161 |
return $gmProcessorGalleries; |
| 162 |
case 'GrandMedia_Modules': |
| 163 |
/** @var $gmProcessorModules */ |
| 164 |
include_once $path_ . 'modules.php'; |
| 165 |
|
| 166 |
return $gmProcessorModules; |
| 167 |
case 'GrandMedia_Settings': |
| 168 |
/** @var $gmProcessorSettings */ |
| 169 |
include_once $path_ . 'settings.php'; |
| 170 |
|
| 171 |
return $gmProcessorSettings; |
| 172 |
case 'GrandMedia_WordpressLibrary': |
| 173 |
/** @var $gmProcessorWPMedia */ |
| 174 |
include_once $path_ . 'wpmedia.php'; |
| 175 |
|
| 176 |
return $gmProcessorWPMedia; |
| 177 |
default: |
| 178 |
if ( null === self::$me ) { |
| 179 |
self::$me = new GmediaProcessor(); |
| 180 |
} |
| 181 |
|
| 182 |
return self::$me; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Load only on Gmedia admin pages |
| 188 |
*/ |
| 189 |
public function controller() { |
| 190 |
|
| 191 |
$this->user_options = self::user_options(); |
| 192 |
$view = $this->gmediablank ? '_frame' : ''; |
| 193 |
$this->display_mode = $this->user_options["display_mode_gmedia{$view}"]; |
| 194 |
|
| 195 |
if ( ! $this->page || strpos( $this->page, 'GrandMedia' ) === false ) { |
| 196 |
return; |
| 197 |
} |
| 198 |
|
| 199 |
auth_redirect(); |
| 200 |
|
| 201 |
$this->processor(); |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* @return array|mixed |
| 206 |
*/ |
| 207 |
public static function user_options() { |
| 208 |
global $user_ID, $gmGallery; |
| 209 |
|
| 210 |
$screen_options = get_user_meta( $user_ID, 'gm_screen_options', true ); |
| 211 |
if ( ! is_array( $screen_options ) ) { |
| 212 |
$screen_options = array(); |
| 213 |
} |
| 214 |
|
| 215 |
return array_merge( $gmGallery->options['gm_screen_options'], $screen_options ); |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Do diff process before lib shell |
| 220 |
*/ |
| 221 |
protected function processor() {} |
| 222 |
|
| 223 |
/** |
| 224 |
* @param string $cookie_key |
| 225 |
* |
| 226 |
* @return array |
| 227 |
*/ |
| 228 |
public function clear_selected_items( $cookie_key ) { |
| 229 |
if ( $cookie_key ) { |
| 230 |
setcookie( $cookie_key, '', time() - 3600 ); |
| 231 |
unset( $_COOKIE[ $cookie_key ] ); |
| 232 |
} |
| 233 |
|
| 234 |
return array(); |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* redirect to original referer after update |
| 239 |
* |
| 240 |
* @param string $location |
| 241 |
* @param string $status |
| 242 |
* |
| 243 |
* @return mixed |
| 244 |
*/ |
| 245 |
public function redirect( $location, $status ) { |
| 246 |
global $pagenow, $gmCore; |
| 247 |
$ref = $gmCore->_post( '_wp_original_http_referer' ); |
| 248 |
if ( 'media.php' === $pagenow && $ref ) { |
| 249 |
if ( strpos( $ref, 'GrandMedia' ) !== false ) { |
| 250 |
return $ref; |
| 251 |
} else { |
| 252 |
return $location; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
return $location; |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Add thumb to gmedia comment text in admin |
| 261 |
* |
| 262 |
* @param string $comment_content |
| 263 |
* @param string $comment |
| 264 |
* @param array $args |
| 265 |
* |
| 266 |
* @return string $comment_content |
| 267 |
*/ |
| 268 |
public function gmedia_comment_text( $comment_content, $comment, $args ) { |
| 269 |
global $post; |
| 270 |
if ( ! $post ) { |
| 271 |
return $comment_content; |
| 272 |
} |
| 273 |
//if('gmedia' === substr($post->post_type, 0, 6)) { |
| 274 |
if ( 'gmedia' === $post->post_type ) { |
| 275 |
global $gmDB, $gmCore; |
| 276 |
$gmedia = $gmDB->get_post_gmedia( $post->ID ); |
| 277 |
$thumb = '<div class="alignright" style="clear:right;"><img class="gmedia-thumb" style="max-height:72px;" src="' . esc_url( $gmCore->gm_get_media_image( $gmedia, 'thumb', false ) ) . '" alt=""/></div>'; |
| 278 |
$comment_content = $thumb . $comment_content; |
| 279 |
} |
| 280 |
|
| 281 |
return $comment_content; |
| 282 |
} |
| 283 |
|
| 284 |
} |
| 285 |
|
| 286 |
global $gmProcessor; |
| 287 |
$gmProcessor = GmediaProcessor::autoload(); |
| 288 |
|