| 1 |
<?php |
| 2 |
/** |
| 3 |
* The admin-specific functionality of the module. |
| 4 |
* |
| 5 |
* @link https://codesupply.co |
| 6 |
* @since 1.0.0 |
| 7 |
* |
| 8 |
* @package Powerkit |
| 9 |
* @subpackage Modules/Admin |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* The admin-specific functionality of the module. |
| 14 |
*/ |
| 15 |
class Powerkit_Post_Format_UI_Admin extends Powerkit_Module_Admin { |
| 16 |
|
| 17 |
/** |
| 18 |
* Initialize |
| 19 |
*/ |
| 20 |
public function initialize() { |
| 21 |
add_action( 'add_meta_boxes', array( $this, 'add_custom_meta_boxes' ) ); |
| 22 |
add_action( 'save_post', array( $this, 'save_post' ) ); |
| 23 |
add_action( 'wp_ajax_pk_media_oembed', array( $this, 'media_oembed' ) ); |
| 24 |
add_action( 'wp_ajax_pk_gallery_attachment', array( $this, 'gallery_attachment' ) ); |
| 25 |
add_action( 'wp_ajax_pk_gallery_update', array( $this, 'gallery_update' ) ); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Add custom meta boxes. |
| 30 |
*/ |
| 31 |
public function add_custom_meta_boxes() { |
| 32 |
add_meta_box( 'powerkit-post-format-link', esc_html__( 'Link', 'powerkit' ), array( $this, 'link_markup' ), 'post', 'normal', 'high', null ); |
| 33 |
add_meta_box( 'powerkit-post-format-audio', esc_html__( 'Audio', 'powerkit' ), array( $this, 'audio_markup' ), 'post', 'normal', 'high', null ); |
| 34 |
add_meta_box( 'powerkit-post-format-video', esc_html__( 'Video', 'powerkit' ), array( $this, 'video_markup' ), 'post', 'normal', 'high', null ); |
| 35 |
add_meta_box( 'powerkit-post-format-gallery', esc_html__( 'Gallery', 'powerkit' ), array( $this, 'gallery_markup' ), 'post', 'normal', 'high', null ); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Link markup. |
| 40 |
*/ |
| 41 |
public function link_markup() { |
| 42 |
$link = get_post_meta( get_the_ID(), 'powerkit_post_format_link', true ); |
| 43 |
?> |
| 44 |
<div class="pk-post-format-media pk-post-format-link"> |
| 45 |
<div class="title-search"> |
| 46 |
<input class="search-input" name="pk-post-format-link" type="text" placeholder="<?php esc_html_e( 'Enter URL', 'powerkit' ); ?>" value="<?php echo esc_attr( $link ); ?>"> |
| 47 |
</div> |
| 48 |
</div> |
| 49 |
<?php wp_nonce_field( 'pk-post-format', 'pk-post-format' ); ?> |
| 50 |
<?php |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Audio markup. |
| 55 |
*/ |
| 56 |
public function audio_markup() { |
| 57 |
$this->media_markup( 'audio' ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Video markup. |
| 62 |
*/ |
| 63 |
public function video_markup() { |
| 64 |
$this->media_markup( 'video' ); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Media markup. |
| 69 |
* |
| 70 |
* @param string $markup The type markup. |
| 71 |
*/ |
| 72 |
public function media_markup( $markup ) { |
| 73 |
$url = get_post_meta( get_the_ID(), 'powerkit_post_format_' . $markup, true ); |
| 74 |
?> |
| 75 |
<div class="pk-post-format-media pk-post-format-<?php echo esc_attr( $markup ); ?>"> |
| 76 |
<div class="title-search"> |
| 77 |
<input class="search-input" name="pk-post-format-<?php echo esc_attr( $markup ); ?>" type="text" placeholder="<?php esc_html_e( 'Enter URL', 'powerkit' ); ?>" value="<?php echo esc_attr( $url ); ?>"> |
| 78 |
</div> |
| 79 |
<div class="canvas"><?php echo $this->wp_oembed_get( $url ); // XSS. ?></div> |
| 80 |
</div> |
| 81 |
<?php wp_nonce_field( 'pk-post-format', 'pk-post-format' ); ?> |
| 82 |
<?php |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Ajax get oembed. |
| 87 |
*/ |
| 88 |
public function media_oembed() { |
| 89 |
check_ajax_referer( 'nonce', 'nonce' ); |
| 90 |
|
| 91 |
if ( isset( $_POST['url'] ) ) { // Input var ok; sanitization ok. |
| 92 |
$url = $_POST['url']; // Input var ok; sanitization ok. |
| 93 |
} |
| 94 |
|
| 95 |
if ( ! isset( $url ) ) { |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
echo $this->wp_oembed_get( $url ); // XSS. |
| 100 |
|
| 101 |
die(); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Get oembed. |
| 106 |
* |
| 107 |
* @param string $url The url media. |
| 108 |
* @param int $width The width media. |
| 109 |
* @param int $height The height media. |
| 110 |
*/ |
| 111 |
public function wp_oembed_get( $url = '', $width = 640, $height = 390 ) { |
| 112 |
|
| 113 |
$embed = @wp_oembed_get( $url, array( |
| 114 |
'width' => $width, |
| 115 |
'height' => $height, |
| 116 |
) ); |
| 117 |
|
| 118 |
return $embed; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Gallery markup. |
| 123 |
*/ |
| 124 |
public function gallery_markup() { |
| 125 |
$attachments = get_post_meta( get_the_ID(), 'powerkit_post_format_gallery', true ); |
| 126 |
?> |
| 127 |
<div class="pk-post-format-gallery"> |
| 128 |
<div class="pk-post-format-gallery-main"> |
| 129 |
<div class="pk-post-format-gallery-attachments"> |
| 130 |
<?php |
| 131 |
if ( $attachments ) { |
| 132 |
foreach ( $attachments as $attachment ) { |
| 133 |
$attachment_url = wp_get_attachment_image_url( $attachment, 'thumbnail' ); |
| 134 |
?> |
| 135 |
<div class="pk-post-format-gallery-attachment" data-id="<?php echo esc_attr( $attachment ); ?>"> |
| 136 |
<input name="pk-post-format-gallery[]" type="hidden" value="<?php echo esc_attr( $attachment ); ?>"> |
| 137 |
<div class="thumbnail"><img src="<?php echo esc_attr( $attachment_url ); ?>" alt="thumbnail"></div> |
| 138 |
<div class="actions"> |
| 139 |
<a href="#" class="pk-post-format-gallery-remove" data-id="<?php echo esc_attr( $attachment ); ?>"></a> |
| 140 |
</div> |
| 141 |
</div> |
| 142 |
<?php |
| 143 |
} |
| 144 |
} |
| 145 |
?> |
| 146 |
</div> |
| 147 |
<div class="pk-post-format-gallery-toolbar"> |
| 148 |
<a href="#" class="button button-primary pk-post-format-gallery-add"><?php esc_html_e( 'Add to gallery', 'powerkit' ); ?></a> |
| 149 |
</div> |
| 150 |
</div> |
| 151 |
<div class="pk-post-format-gallery-side"> |
| 152 |
<div class="pk-post-format-gallery-side-wrap"> |
| 153 |
<div class="pk-post-format-gallery-side-inner"> |
| 154 |
<div class="pk-post-format-gallery-side-data"> |
| 155 |
<span class="spinner"></span> |
| 156 |
</div> |
| 157 |
<div class="pk-post-format-gallery-side-toolbar"> |
| 158 |
<a href="#" class="button pk-post-format-gallery-close"><?php esc_html_e( 'Close', 'powerkit' ); ?></a> |
| 159 |
|
| 160 |
<span class="spinner"></span> |
| 161 |
<a href="#" class="button button-primary pk-post-format-gallery-update"><?php esc_html_e( 'Update', 'powerkit' ); ?></a> |
| 162 |
</div> |
| 163 |
</div> |
| 164 |
</div> |
| 165 |
</div> |
| 166 |
<?php $ids = $attachments ? implode( ',', $attachments ) : null; ?> |
| 167 |
<input class="pk-post-format-gallery-settings" type="hidden" value="<?php echo esc_attr( $ids ); ?>" data-title="<?php esc_html_e( 'Add Image', 'powerkit' ); ?>" data-button="<?php esc_html_e( 'Select', 'powerkit' ); ?>" disabled> |
| 168 |
</div> |
| 169 |
<?php wp_nonce_field( 'pk-post-format', 'pk-post-format' ); ?> |
| 170 |
<?php |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Gallery attachment info. |
| 175 |
*/ |
| 176 |
public function gallery_attachment() { |
| 177 |
check_ajax_referer( 'nonce', 'nonce' ); |
| 178 |
|
| 179 |
if ( isset( $_POST['id'] ) ) { // Input var ok; sanitization ok. |
| 180 |
$attachment_id = (int) $_POST['id']; // Input var ok; sanitization ok. |
| 181 |
} |
| 182 |
|
| 183 |
if ( ! isset( $attachment_id ) ) { |
| 184 |
return; |
| 185 |
} |
| 186 |
|
| 187 |
// Vars. |
| 188 |
$attachment = wp_prepare_attachment_for_js( $attachment_id ); |
| 189 |
$compat = get_compat_media_markup( $attachment_id ); |
| 190 |
$thumb = null; |
| 191 |
$dimentions = null; |
| 192 |
|
| 193 |
// Thumb. |
| 194 |
if ( 'image' === $attachment['type'] ) { |
| 195 |
$thumb = $attachment['url']; |
| 196 |
} else { |
| 197 |
$thumb = wp_mime_type_icon(); |
| 198 |
} |
| 199 |
|
| 200 |
// Dimentions. |
| 201 |
if ( ! empty( $attachment['width'] ) ) { |
| 202 |
$dimentions = $attachment['width'] . ' x ' . $attachment['height']; |
| 203 |
} |
| 204 |
if ( ! empty( $attachment['filesizeHumanReadable'] ) ) { |
| 205 |
$dimentions .= sprintf( ' (%s)', $attachment['filesizeHumanReadable'] ); |
| 206 |
} |
| 207 |
?> |
| 208 |
|
| 209 |
<div class="pk-post-format-gallery-side-info"> |
| 210 |
<img src="<?php echo esc_attr( $thumb ); ?>" alt="<?php echo esc_attr( $attachment['alt'] ); ?>" /> |
| 211 |
<p class="filename"><strong><?php echo esc_html( $attachment['filename'] ); ?></strong></p> |
| 212 |
<p class="uploaded"><?php echo esc_html( $attachment['dateFormatted'] ); ?></p> |
| 213 |
<p class="dimensions"><?php echo esc_html( $dimentions ); ?></p> |
| 214 |
</div> |
| 215 |
<div class="clear"></div> |
| 216 |
<table class="form-table"> |
| 217 |
<tbody> |
| 218 |
<tr data-name="title" data-type="text"> |
| 219 |
<td class="label"><label for="pk-attachments-title"><?php esc_html_e( 'Title', 'powerkit' ); ?></label></td> |
| 220 |
<td class="input"><input type="text" id="pk-attachments-title" name="title" value="<?php echo esc_html( $attachment['title'] ); ?>"></td> |
| 221 |
</tr> |
| 222 |
<tr data-name="caption" data-type="textarea"> |
| 223 |
<td class="label"><label for="pk-attachments-caption"><?php esc_html_e( 'Caption', 'powerkit' ); ?></label></td> |
| 224 |
<td class="input"><textarea id="pk-attachments-caption" name="caption"><?php echo wp_kses( $attachment['caption'], 'post' ); ?></textarea></td> |
| 225 |
</tr> |
| 226 |
<tr data-name="alt" data-type="text"> |
| 227 |
<td class="label"><label for="pk-attachments-alt"><?php esc_html_e( 'Alt Text', 'powerkit' ); ?></label></td> |
| 228 |
<td class="input"><input type="text" id="pk-attachments-alt" name="alt" value="<?php echo esc_html( $attachment['alt'] ); ?>"></td> |
| 229 |
</tr> |
| 230 |
<tr data-name="description" data-type="textarea"> |
| 231 |
<td class="label"><label for="pk-attachments-description"><?php esc_html_e( 'Description', 'powerkit' ); ?></label></td> |
| 232 |
<td class="input"><textarea id="pk-attachments-description" name="description"><?php echo wp_kses( $attachment['description'], 'post' ); ?></textarea></td> |
| 233 |
</tr> |
| 234 |
</tbody> |
| 235 |
</table> |
| 236 |
<input type="hidden" name="id" value="<?php echo esc_html( $attachment_id ); ?>"> |
| 237 |
<input type="hidden" name="action" value="pk_gallery_update"> |
| 238 |
<?php wp_nonce_field( 'nonce', 'nonce' ); ?> |
| 239 |
<?php |
| 240 |
|
| 241 |
die(); |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Gallery attachment update. |
| 246 |
*/ |
| 247 |
public function gallery_update() { |
| 248 |
check_ajax_referer( 'nonce', 'nonce' ); |
| 249 |
|
| 250 |
$changes = null; |
| 251 |
|
| 252 |
if ( isset( $_POST['id'] ) ) { // Input var ok; sanitization ok. |
| 253 |
$attachment_id = (int) $_POST['id']; // Input var ok; sanitization ok. |
| 254 |
} |
| 255 |
|
| 256 |
if ( isset( $_POST['title'] ) ) { |
| 257 |
$changes['title'] = $_POST['title']; // Input var ok; sanitization ok. |
| 258 |
} |
| 259 |
if ( isset( $_POST['caption'] ) ) { |
| 260 |
$changes['caption'] = $_POST['caption']; // Input var ok; sanitization ok. |
| 261 |
} |
| 262 |
if ( isset( $_POST['description'] ) ) { |
| 263 |
$changes['description'] = $_POST['description']; // Input var ok; sanitization ok. |
| 264 |
} |
| 265 |
if ( isset( $_POST['alt'] ) ) { |
| 266 |
$changes['alt'] = $_POST['alt']; // Input var ok; sanitization ok. |
| 267 |
} |
| 268 |
|
| 269 |
if ( ! isset( $attachment_id ) ) { |
| 270 |
return; |
| 271 |
} |
| 272 |
|
| 273 |
if ( ! current_user_can( 'edit_post', $attachment_id ) ) { |
| 274 |
wp_send_json_error(); |
| 275 |
} |
| 276 |
|
| 277 |
$post = get_post( $attachment_id, ARRAY_A ); |
| 278 |
|
| 279 |
if ( 'attachment' !== $post['post_type'] ) { |
| 280 |
wp_send_json_error(); |
| 281 |
} |
| 282 |
|
| 283 |
if ( isset( $changes['title'] ) ) { |
| 284 |
$post['post_title'] = $changes['title']; |
| 285 |
} |
| 286 |
|
| 287 |
if ( isset( $changes['caption'] ) ) { |
| 288 |
$post['post_excerpt'] = $changes['caption']; |
| 289 |
} |
| 290 |
|
| 291 |
if ( isset( $changes['description'] ) ) { |
| 292 |
$post['post_content'] = $changes['description']; |
| 293 |
} |
| 294 |
|
| 295 |
if ( isset( $changes['alt'] ) ) { |
| 296 |
$alt = wp_unslash( $changes['alt'] ); |
| 297 |
|
| 298 |
$post_meta = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ); |
| 299 |
|
| 300 |
if ( $alt !== $post_meta ) { |
| 301 |
$alt = wp_strip_all_tags( $alt, true ); |
| 302 |
update_post_meta( $attachment_id, '_wp_attachment_image_alt', wp_slash( $alt ) ); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
// Save post. |
| 307 |
wp_update_post( $post ); |
| 308 |
|
| 309 |
/** This filter is documented in wp-admin/includes/media.php */ |
| 310 |
// - seems off to run this filter AFTER the update_post function, but there is a reason |
| 311 |
// - when placed BEFORE, an empty post_title will be populated by WP |
| 312 |
// - this filter will still allow 3rd party to save extra image data! |
| 313 |
$post = apply_filters( 'attachment_fields_to_save', $post, $changes ); |
| 314 |
|
| 315 |
wp_send_json_success(); |
| 316 |
} |
| 317 |
|
| 318 |
/** |
| 319 |
* Save meta box |
| 320 |
* |
| 321 |
* @param int $post_id The post id. |
| 322 |
*/ |
| 323 |
public function save_post( $post_id ) { |
| 324 |
|
| 325 |
// Bail if we're doing an auto save. |
| 326 |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 327 |
return; |
| 328 |
} |
| 329 |
|
| 330 |
// if our nonce isn't there, or we can't verify it, bail. |
| 331 |
if ( ! isset( $_POST['pk-post-format'] ) || ! wp_verify_nonce( $_POST['pk-post-format'], 'pk-post-format' ) ) { // Input var ok; sanitization ok. |
| 332 |
return; |
| 333 |
} |
| 334 |
|
| 335 |
if ( isset( $_POST['pk-post-format-link'] ) ) { // Input var ok; sanitization ok. |
| 336 |
update_post_meta( $post_id, 'powerkit_post_format_link', $_POST['pk-post-format-link'] ); // Input var ok; sanitization ok. |
| 337 |
} else { |
| 338 |
delete_post_meta( $post_id, 'powerkit_post_format_link' ); |
| 339 |
} |
| 340 |
if ( isset( $_POST['pk-post-format-audio'] ) ) { // Input var ok; sanitization ok. |
| 341 |
update_post_meta( $post_id, 'powerkit_post_format_audio', $_POST['pk-post-format-audio'] ); // Input var ok; sanitization ok. |
| 342 |
} else { |
| 343 |
delete_post_meta( $post_id, 'powerkit_post_format_audio' ); |
| 344 |
} |
| 345 |
if ( isset( $_POST['pk-post-format-video'] ) ) { // Input var ok; sanitization ok. |
| 346 |
update_post_meta( $post_id, 'powerkit_post_format_video', $_POST['pk-post-format-video'] ); // Input var ok; sanitization ok. |
| 347 |
} else { |
| 348 |
delete_post_meta( $post_id, 'powerkit_post_format_video' ); |
| 349 |
} |
| 350 |
if ( isset( $_POST['pk-post-format-gallery'] ) ) { // Input var ok; sanitization ok. |
| 351 |
update_post_meta( $post_id, 'powerkit_post_format_gallery', $_POST['pk-post-format-gallery'] ); // Input var ok; sanitization ok. |
| 352 |
} else { |
| 353 |
delete_post_meta( $post_id, 'powerkit_post_format_gallery' ); |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
/** |
| 358 |
* Register the stylesheets and JavaScript for the admin area. |
| 359 |
* |
| 360 |
* @param string $page Current page. |
| 361 |
*/ |
| 362 |
public function admin_enqueue_scripts( $page ) { |
| 363 |
if ( 'post.php' === $page || 'post-new.php' === $page ) { |
| 364 |
|
| 365 |
wp_enqueue_media(); |
| 366 |
wp_enqueue_script( 'jquery-ui-sortable' ); |
| 367 |
|
| 368 |
// Styles. |
| 369 |
wp_enqueue_style( 'powerkit-post-format-ui', powerkit_style( plugin_dir_url( __FILE__ ) . 'css/admin-powerkit-post-format-ui.css' ), array(), powerkit_get_setting( 'version' ), 'all' ); |
| 370 |
|
| 371 |
wp_add_inline_style( 'powerkit-post-format-ui', sprintf( '#powerkit-post-format-%s { display: block; }', get_post_format() ) ); |
| 372 |
|
| 373 |
// Scripts. |
| 374 |
wp_enqueue_script( 'powerkit-post-format-ui', plugin_dir_url( __FILE__ ) . 'js/admin-powerkit-post-format-ui.js', array( 'jquery' ), powerkit_get_setting( 'version' ), false ); |
| 375 |
|
| 376 |
wp_localize_script( 'powerkit-post-format-ui', 'powerkit_post_format_ui', |
| 377 |
array( |
| 378 |
'url' => admin_url( 'admin-ajax.php' ), |
| 379 |
'nonce' => wp_create_nonce( 'nonce' ), |
| 380 |
) |
| 381 |
); |
| 382 |
} |
| 383 |
} |
| 384 |
} |
| 385 |
|