| 1 |
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 |
/** |
| 3 |
* Create and manage comics with this Custom Post Type. |
| 4 |
* |
| 5 |
* @package automattic/jetpack |
| 6 |
*/ |
| 7 |
|
| 8 |
use Automattic\Jetpack\Assets; |
| 9 |
|
| 10 |
/** |
| 11 |
* Create a jetpack-comic CPT. |
| 12 |
*/ |
| 13 |
class Jetpack_Comic { |
| 14 |
const POST_TYPE = 'jetpack-comic'; |
| 15 |
|
| 16 |
/** |
| 17 |
* Initialize the class. |
| 18 |
* |
| 19 |
* @return self |
| 20 |
*/ |
| 21 |
public static function init() { |
| 22 |
static $instance = false; |
| 23 |
|
| 24 |
if ( ! $instance ) { |
| 25 |
$instance = new Jetpack_Comic(); |
| 26 |
} |
| 27 |
|
| 28 |
return $instance; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Conditionally hook into WordPress. |
| 33 |
* |
| 34 |
* Themes must declare that they support this module by adding |
| 35 |
* add_theme_support( 'jetpack-comic' ); during after_setup_theme. |
| 36 |
* |
| 37 |
* If no theme support is found there is no need to hook into |
| 38 |
* WordPress. We'll just return early instead. |
| 39 |
*/ |
| 40 |
public function __construct() { |
| 41 |
// Make sure the post types are loaded for imports |
| 42 |
add_action( 'import_start', array( $this, 'register_post_types' ) ); |
| 43 |
|
| 44 |
// Add to REST API post type allowed list. |
| 45 |
add_filter( 'rest_api_allowed_post_types', array( $this, 'allow_rest_api_type' ) ); |
| 46 |
|
| 47 |
// If called via REST API, we need to register later in lifecycle |
| 48 |
add_action( 'restapi_theme_init', array( $this, 'maybe_register_post_types' ) ); |
| 49 |
|
| 50 |
// Return early if theme does not support Jetpack Comic. |
| 51 |
if ( ! ( $this->site_supports_comics() ) ) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
$this->register_post_types(); |
| 56 |
|
| 57 |
add_action( 'pre_get_posts', array( $this, 'add_posts_to_loop' ) ); |
| 58 |
|
| 59 |
// In order for the Feedbag job to find Comic posts, we need to circumvent any pretty |
| 60 |
// URLs in the RSS feed given to Feedbag in favor of /?p=123&post_type=jetpack-comic |
| 61 |
add_filter( 'the_permalink_rss', array( $this, 'custom_permalink_for_feedbag' ) ); |
| 62 |
|
| 63 |
// There are some cases (like when Feedbag is fetching posts) that the comics |
| 64 |
// post type needs to be registered no matter what, but none of the UI needs to be |
| 65 |
// available. |
| 66 |
|
| 67 |
add_filter( 'post_updated_messages', array( $this, 'updated_messages' ) ); |
| 68 |
|
| 69 |
if ( function_exists( 'queue_publish_post' ) ) { |
| 70 |
add_action( 'publish_jetpack-comic', 'queue_publish_post', 10, 2 ); |
| 71 |
} |
| 72 |
|
| 73 |
add_action( 'pre_get_posts', array( $this, 'include_in_feeds' ) ); |
| 74 |
|
| 75 |
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); |
| 76 |
|
| 77 |
add_filter( 'manage_' . self::POST_TYPE . '_posts_columns', array( $this, 'manage_posts_columns' ) ); |
| 78 |
add_action( 'manage_' . self::POST_TYPE . '_posts_custom_column', array( $this, 'manage_posts_custom_column' ), 10, 2 ); |
| 79 |
add_image_size( 'jetpack-comic-thumb', 150, 0, false ); |
| 80 |
|
| 81 |
// Enable front-end uploading for users special enough. |
| 82 |
if ( current_user_can( 'upload_files' ) && current_user_can( 'edit_posts' ) ) { |
| 83 |
add_action( 'wp_ajax_jetpack_comic_upload', array( $this, 'upload' ) ); |
| 84 |
add_action( 'wp_enqueue_scripts', array( $this, 'register_scripts' ) ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Add a "Convert to Comic" and "Convert to Post" option to the bulk |
| 89 |
* edit dropdowns. |
| 90 |
*/ |
| 91 |
add_action( 'admin_footer-edit.php', array( $this, 'admin_footer' ) ); |
| 92 |
add_action( 'load-edit.php', array( $this, 'bulk_edit' ) ); |
| 93 |
add_action( 'admin_notices', array( $this, 'bulk_edit_notices' ) ); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Enqueue JavaScript in the footer. |
| 98 |
* |
| 99 |
* @return void |
| 100 |
*/ |
| 101 |
public function admin_footer() { |
| 102 |
$post_type = get_post_type(); |
| 103 |
|
| 104 |
?> |
| 105 |
<script type="text/javascript"> |
| 106 |
jQuery( document ).ready( function( $ ) { |
| 107 |
<?php if ( ! $post_type || 'post' === $post_type ) { ?> |
| 108 |
$( '<option>' ) |
| 109 |
.val( 'post2comic' ) |
| 110 |
.text( <?php echo wp_json_encode( __( 'Convert to Comic', 'jetpack' ) ); ?> ) |
| 111 |
.appendTo( "select[name='action'], select[name='action2']" ); |
| 112 |
<?php } ?> |
| 113 |
<?php if ( ! $post_type || self::POST_TYPE === $post_type ) { ?> |
| 114 |
$( '<option>' ) |
| 115 |
.val( 'comic2post' ) |
| 116 |
.text( <?php echo wp_json_encode( __( 'Convert to Post', 'jetpack' ) ); ?> ) |
| 117 |
.appendTo( "select[name='action'], select[name='action2']" ); |
| 118 |
<?php } ?> |
| 119 |
|
| 120 |
$( '#message.jetpack-comic-post-type-conversion' ).remove().insertAfter( $( '.wrap h2:first' ) ).show(); |
| 121 |
}); |
| 122 |
</script> |
| 123 |
<?php |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Handle the "Convert to [Post|Comic]" bulk action. |
| 128 |
* |
| 129 |
* @return void |
| 130 |
*/ |
| 131 |
public function bulk_edit() { |
| 132 |
if ( empty( $_REQUEST['post'] ) ) { |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
$wp_list_table = _get_list_table( 'WP_Posts_List_Table' ); |
| 137 |
$action = $wp_list_table->current_action(); |
| 138 |
|
| 139 |
check_admin_referer( 'bulk-posts' ); |
| 140 |
|
| 141 |
if ( 'post2comic' === $action || 'comic2post' === $action ) { |
| 142 |
if ( ! current_user_can( 'publish_posts' ) ) { |
| 143 |
wp_die( esc_html__( 'You are not allowed to make this change.', 'jetpack' ) ); |
| 144 |
} |
| 145 |
|
| 146 |
$post_ids = array_map( 'intval', $_REQUEST['post'] ); |
| 147 |
|
| 148 |
$modified_count = 0; |
| 149 |
|
| 150 |
foreach ( $post_ids as $post_id ) { |
| 151 |
$destination_post_type = ( $action === 'post2comic' ) ? self::POST_TYPE : 'post'; |
| 152 |
$origin_post_type = ( $destination_post_type === 'post' ) ? self::POST_TYPE : 'post'; |
| 153 |
|
| 154 |
if ( current_user_can( 'edit_post', $post_id ) ) { |
| 155 |
$post = get_post( $post_id ); |
| 156 |
|
| 157 |
// Only convert posts that are post => comic or comic => post. |
| 158 |
// (e.g., Ignore comic => comic, page => post, etc. ) |
| 159 |
if ( $post->post_type !== $destination_post_type && $post->post_type === $origin_post_type ) { |
| 160 |
$post_type_object = get_post_type_object( $destination_post_type ); |
| 161 |
|
| 162 |
if ( current_user_can( $post_type_object->cap->publish_posts ) ) { |
| 163 |
set_post_type( $post_id, $destination_post_type ); |
| 164 |
$modified_count++; |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
$sendback = remove_query_arg( array( 'exported', 'untrashed', 'deleted', 'ids' ), wp_get_referer() ); |
| 171 |
|
| 172 |
if ( ! $sendback ) { |
| 173 |
$sendback = add_query_arg( array( 'post_type', get_post_type() ), admin_url( 'edit.php' ) ); |
| 174 |
} |
| 175 |
|
| 176 |
$pagenum = $wp_list_table->get_pagenum(); |
| 177 |
$bulk_edit_comics_nonce = wp_create_nonce( 'bulk-edit-comics-nonce' ); |
| 178 |
$sendback = add_query_arg( |
| 179 |
array( |
| 180 |
'paged' => $pagenum, |
| 181 |
'post_type_changed' => $modified_count, |
| 182 |
'bulk_edit_comics_nonce' => $bulk_edit_comics_nonce, |
| 183 |
), |
| 184 |
$sendback |
| 185 |
); |
| 186 |
|
| 187 |
wp_safe_redirect( $sendback ); |
| 188 |
exit(); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Show the post conversion success notice. |
| 194 |
* |
| 195 |
* @return void |
| 196 |
*/ |
| 197 |
public function bulk_edit_notices() { |
| 198 |
global $pagenow; |
| 199 |
|
| 200 |
if ( |
| 201 |
empty( $_GET['bulk_edit_comics_nonce'] ) |
| 202 |
|| ! wp_verify_nonce( sanitize_key( $_GET['bulk_edit_comics_nonce'] ), 'bulk-edit-comics-nonce' ) |
| 203 |
) { |
| 204 |
return; |
| 205 |
} |
| 206 |
|
| 207 |
$number_posts_changed = isset( $_GET['post_type_changed'] ) |
| 208 |
? (int) $_GET['post_type_changed'] |
| 209 |
: 0; |
| 210 |
|
| 211 |
if ( 'edit.php' === $pagenow && $number_posts_changed ) { |
| 212 |
?> |
| 213 |
<div id="message" class="updated below-h2 jetpack-comic-post-type-conversion" style="display: none;"><p> |
| 214 |
<?php |
| 215 |
echo esc_html( |
| 216 |
sprintf( |
| 217 |
/* Translators: placeholder is a number. */ |
| 218 |
_n( '%s post converted.', '%s posts converted', (int) $number_posts_changed, 'jetpack' ), |
| 219 |
number_format_i18n( $number_posts_changed ) |
| 220 |
) |
| 221 |
); |
| 222 |
?> |
| 223 |
</p></div> |
| 224 |
<?php |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Enqueue scripts and styles. |
| 230 |
* |
| 231 |
* @return void |
| 232 |
*/ |
| 233 |
public function register_scripts() { |
| 234 |
wp_enqueue_style( 'jetpack-comics-style', plugins_url( 'comics/comics.css', __FILE__ ), array(), JETPACK__VERSION ); |
| 235 |
wp_style_add_data( 'jetpack-comics-style', 'rtl', 'replace' ); |
| 236 |
|
| 237 |
$is_amp = class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request(); |
| 238 |
if ( ! $is_amp ) { |
| 239 |
wp_enqueue_script( |
| 240 |
'jetpack-comics', |
| 241 |
Assets::get_file_url_for_environment( |
| 242 |
'_inc/build/custom-post-types/comics/comics.min.js', |
| 243 |
'modules/custom-post-types/comics/comics.js' |
| 244 |
), |
| 245 |
array( 'jquery' ), |
| 246 |
JETPACK__VERSION, |
| 247 |
false |
| 248 |
); |
| 249 |
|
| 250 |
$options = array( |
| 251 |
'nonce' => wp_create_nonce( 'jetpack_comic_upload_nonce' ), |
| 252 |
'writeURL' => admin_url( 'admin-ajax.php?action=jetpack_comic_upload' ), |
| 253 |
'labels' => array( |
| 254 |
'dragging' => __( 'Drop images to upload', 'jetpack' ), |
| 255 |
'uploading' => __( 'Uploading...', 'jetpack' ), |
| 256 |
'processing' => __( 'Processing...', 'jetpack' ), |
| 257 |
'unsupported' => __( "Sorry, your browser isn't supported. Upgrade at browsehappy.com.", 'jetpack' ), |
| 258 |
'invalidUpload' => __( 'Only images can be uploaded here.', 'jetpack' ), |
| 259 |
'error' => __( "Your upload didn't complete; try again later or cross your fingers and try again right now.", 'jetpack' ), |
| 260 |
), |
| 261 |
); |
| 262 |
|
| 263 |
wp_localize_script( 'jetpack-comics', 'Jetpack_Comics_Options', $options ); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Enqueue stylesheet in the admin. |
| 269 |
*/ |
| 270 |
public function admin_enqueue_scripts() { |
| 271 |
wp_enqueue_style( 'jetpack-comics-admin', plugins_url( 'comics/admin.css', __FILE__ ), array(), JETPACK__VERSION ); |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Register the post types if the theme supports them. |
| 276 |
*/ |
| 277 |
public function maybe_register_post_types() { |
| 278 |
// Return early if theme does not support Jetpack Comic. |
| 279 |
if ( ! ( $this->site_supports_comics() ) ) { |
| 280 |
return; |
| 281 |
} |
| 282 |
|
| 283 |
$this->register_post_types(); |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* Register our CPT. |
| 288 |
*/ |
| 289 |
public function register_post_types() { |
| 290 |
if ( post_type_exists( self::POST_TYPE ) ) { |
| 291 |
return; |
| 292 |
} |
| 293 |
|
| 294 |
register_post_type( |
| 295 |
self::POST_TYPE, |
| 296 |
array( |
| 297 |
'description' => __( 'Comics', 'jetpack' ), |
| 298 |
'labels' => array( |
| 299 |
'name' => esc_html__( 'Comics', 'jetpack' ), |
| 300 |
'singular_name' => esc_html__( 'Comic', 'jetpack' ), |
| 301 |
'menu_name' => esc_html__( 'Comics', 'jetpack' ), |
| 302 |
'all_items' => esc_html__( 'All Comics', 'jetpack' ), |
| 303 |
'add_new' => esc_html__( 'Add New', 'jetpack' ), |
| 304 |
'add_new_item' => esc_html__( 'Add New Comic', 'jetpack' ), |
| 305 |
'edit_item' => esc_html__( 'Edit Comic', 'jetpack' ), |
| 306 |
'new_item' => esc_html__( 'New Comic', 'jetpack' ), |
| 307 |
'view_item' => esc_html__( 'View Comic', 'jetpack' ), |
| 308 |
'search_items' => esc_html__( 'Search Comics', 'jetpack' ), |
| 309 |
'not_found' => esc_html__( 'No Comics found', 'jetpack' ), |
| 310 |
'not_found_in_trash' => esc_html__( 'No Comics found in Trash', 'jetpack' ), |
| 311 |
'filter_items_list' => esc_html__( 'Filter comics list', 'jetpack' ), |
| 312 |
'items_list_navigation' => esc_html__( 'Comics list navigation', 'jetpack' ), |
| 313 |
'items_list' => esc_html__( 'Comics list', 'jetpack' ), |
| 314 |
), |
| 315 |
'supports' => array( |
| 316 |
'title', |
| 317 |
'editor', |
| 318 |
'thumbnail', |
| 319 |
'comments', |
| 320 |
'revisions', |
| 321 |
'publicize', // Jetpack |
| 322 |
'subscriptions', // wpcom |
| 323 |
'shortlinks', // Jetpack |
| 324 |
), |
| 325 |
'rewrite' => array( |
| 326 |
'slug' => 'comic', |
| 327 |
'with_front' => false, |
| 328 |
), |
| 329 |
'taxonomies' => array( |
| 330 |
'category', |
| 331 |
'post_tag', |
| 332 |
), |
| 333 |
// Only make the type public for sites that support Comics. |
| 334 |
'public' => true, |
| 335 |
'menu_position' => 5, // below Posts |
| 336 |
'map_meta_cap' => true, |
| 337 |
'has_archive' => true, |
| 338 |
'query_var' => 'comic', |
| 339 |
'show_in_rest' => true, |
| 340 |
) |
| 341 |
); |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* Add a Preview colunm to the Comic CPT admin view. |
| 346 |
* |
| 347 |
* @param array $columns An array of column names. |
| 348 |
* @return array Updated `$columns`. |
| 349 |
*/ |
| 350 |
public function manage_posts_columns( $columns ) { |
| 351 |
$new_columns = array( |
| 352 |
'preview-jetpack-comic' => __( 'Preview', 'jetpack' ), |
| 353 |
); |
| 354 |
return array_merge( array_slice( $columns, 0, 2 ), $new_columns, array_slice( $columns, 2 ) ); |
| 355 |
} |
| 356 |
|
| 357 |
/** |
| 358 |
* Display the post's featured image in column. |
| 359 |
* |
| 360 |
* @param string $column_name The name of the column to display. |
| 361 |
* @param int $post_ID The current post ID. |
| 362 |
*/ |
| 363 |
public function manage_posts_custom_column( $column_name, $post_ID ) { |
| 364 |
if ( 'preview-jetpack-comic' === $column_name && has_post_thumbnail( $post_ID ) ) { |
| 365 |
echo get_the_post_thumbnail( $post_ID, 'jetpack-comic-thumb' ); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
/** |
| 370 |
* The function url_to_postid() doesn't handle pretty permalinks |
| 371 |
* for CPTs very well. When we're generating an RSS feed to be consumed |
| 372 |
* for Feedbag (the Reader's feed storage mechanism), eschew |
| 373 |
* a pretty URL for one that will get the post into the Reader. |
| 374 |
* |
| 375 |
* @see https://core.trac.wordpress.org/ticket/19744 |
| 376 |
* @param string $permalink The existing (possibly pretty) permalink. |
| 377 |
* |
| 378 |
* @return string The permalink to use. |
| 379 |
*/ |
| 380 |
public function custom_permalink_for_feedbag( $permalink ) { |
| 381 |
global $post; |
| 382 |
|
| 383 |
if ( ! empty( $GLOBALS['is_feedbag_rss_script'] ) && self::POST_TYPE === $post->post_type ) { |
| 384 |
$permalink = home_url( |
| 385 |
add_query_arg( |
| 386 |
array( |
| 387 |
'p' => $post->ID, |
| 388 |
'post_type' => self::POST_TYPE, |
| 389 |
), |
| 390 |
'?' |
| 391 |
) |
| 392 |
); |
| 393 |
} |
| 394 |
|
| 395 |
return $permalink; |
| 396 |
} |
| 397 |
|
| 398 |
/** |
| 399 |
* Update messages for the Comic admin. |
| 400 |
* |
| 401 |
* @param array $messages Existing post update messages. |
| 402 |
* |
| 403 |
* @return array $messages Amended post update messages. |
| 404 |
*/ |
| 405 |
public function updated_messages( $messages ) { |
| 406 |
global $post; |
| 407 |
|
| 408 |
$messages['jetpack-comic'] = array( |
| 409 |
0 => '', // Unused. Messages start at index 1. |
| 410 |
1 => sprintf( |
| 411 |
/* Translators: link to comic item's page. */ |
| 412 |
__( 'Comic updated. <a href="%s">View comic</a>', 'jetpack' ), |
| 413 |
esc_url( get_permalink( $post->ID ) ) |
| 414 |
), |
| 415 |
2 => esc_html__( 'Custom field updated.', 'jetpack' ), |
| 416 |
3 => esc_html__( 'Custom field deleted.', 'jetpack' ), |
| 417 |
4 => esc_html__( 'Comic updated.', 'jetpack' ), |
| 418 |
/* translators: %s: date and time of the revision */ |
| 419 |
5 => isset( $_GET['revision'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Copying core message handling. |
| 420 |
? sprintf( |
| 421 |
/* Translators: link to comic item's page. */ |
| 422 |
esc_html__( 'Comic restored to revision from %s', 'jetpack' ), |
| 423 |
wp_post_revision_title( (int) $_GET['revision'], false ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Copying core message handling. |
| 424 |
) |
| 425 |
: false, |
| 426 |
6 => sprintf( |
| 427 |
/* Translators: link to comic item's page. */ |
| 428 |
__( 'Comic published. <a href="%s">View comic</a>', 'jetpack' ), |
| 429 |
esc_url( get_permalink( $post->ID ) ) |
| 430 |
), |
| 431 |
7 => esc_html__( 'Comic saved.', 'jetpack' ), |
| 432 |
8 => sprintf( |
| 433 |
/* Translators: link to portfolio item's page. */ |
| 434 |
__( 'Comic submitted. <a target="_blank" href="%s">Preview comic</a>', 'jetpack' ), |
| 435 |
esc_url( add_query_arg( 'preview', 'true', get_permalink( $post->ID ) ) ) |
| 436 |
), |
| 437 |
9 => sprintf( |
| 438 |
/* Translators: link to comic item's page. */ |
| 439 |
__( 'Comic scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview comic</a>', 'jetpack' ), |
| 440 |
// translators: Publish box date format, see https://php.net/date |
| 441 |
date_i18n( __( 'M j, Y @ G:i', 'jetpack' ), strtotime( $post->post_date ) ), |
| 442 |
esc_url( get_permalink( $post->ID ) ) |
| 443 |
), |
| 444 |
10 => sprintf( |
| 445 |
/* Translators: link to comic item's page. */ |
| 446 |
__( 'Comic draft updated. <a target="_blank" href="%s">Preview comic</a>', 'jetpack' ), |
| 447 |
esc_url( add_query_arg( 'preview', 'true', get_permalink( $post->ID ) ) ) |
| 448 |
), |
| 449 |
); |
| 450 |
|
| 451 |
return $messages; |
| 452 |
} |
| 453 |
|
| 454 |
/** |
| 455 |
* Should this Custom Post Type be made available? |
| 456 |
* |
| 457 |
* @return bool |
| 458 |
*/ |
| 459 |
public function site_supports_comics() { |
| 460 |
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 461 |
$php_self = isset( $_SERVER['PHP_SELF'] ) |
| 462 |
? sanitize_text_field( wp_unslash( $_SERVER['PHP_SELF'] ) ) |
| 463 |
: ''; |
| 464 |
$blog_ids = isset( $_SERVER['argv'] ) |
| 465 |
? array_map( 'intval', (array) wp_unslash( $_SERVER['argv'] ) ) |
| 466 |
: array(); |
| 467 |
|
| 468 |
if ( |
| 469 |
! empty( $php_self ) |
| 470 |
&& 'blog-rss.php' === substr( $php_self, -12 ) |
| 471 |
&& count( $blog_ids ) > 1 |
| 472 |
) { |
| 473 |
// blog-rss.php isn't run in the context of the target blog when the init action fires, |
| 474 |
// so check manually whether the target blog supports comics. |
| 475 |
switch_to_blog( $blog_ids[1] ); |
| 476 |
// The add_theme_support( 'jetpack-comic' ) won't fire on switch_to_blog, so check for Panel manually. |
| 477 |
$supports_comics = ( ( function_exists( 'site_vertical' ) && 'comics' === site_vertical() ) |
| 478 |
|| current_theme_supports( self::POST_TYPE ) |
| 479 |
|| get_stylesheet() === 'pub/panel' ); |
| 480 |
restore_current_blog(); |
| 481 |
|
| 482 |
/** This action is documented in modules/custom-post-types/nova.php */ |
| 483 |
return (bool) apply_filters( 'jetpack_enable_cpt', $supports_comics, self::POST_TYPE ); |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
$supports_comics = false; |
| 488 |
|
| 489 |
/** |
| 490 |
* If we're on WordPress.com, and it has the menu site vertical. |
| 491 |
* |
| 492 |
* @todo: Extract this out into a wpcom only file. |
| 493 |
*/ |
| 494 |
if ( function_exists( 'site_vertical' ) && 'comics' === site_vertical() ) { |
| 495 |
$supports_comics = true; |
| 496 |
} |
| 497 |
|
| 498 |
/** |
| 499 |
* Else, if the current theme requests it. |
| 500 |
*/ |
| 501 |
if ( current_theme_supports( self::POST_TYPE ) ) { |
| 502 |
$supports_comics = true; |
| 503 |
} |
| 504 |
|
| 505 |
/** |
| 506 |
* Filter it in case something else knows better. |
| 507 |
*/ |
| 508 |
/** This action is documented in modules/custom-post-types/nova.php */ |
| 509 |
return (bool) apply_filters( 'jetpack_enable_cpt', $supports_comics, self::POST_TYPE ); |
| 510 |
} |
| 511 |
|
| 512 |
/** |
| 513 |
* Anywhere that a feed is displaying posts, show comics too. |
| 514 |
* |
| 515 |
* @param WP_Query $query The current query. |
| 516 |
* |
| 517 |
* @return void |
| 518 |
*/ |
| 519 |
public function include_in_feeds( $query ) { |
| 520 |
if ( ! $query->is_feed() ) { |
| 521 |
return; |
| 522 |
} |
| 523 |
|
| 524 |
// Don't modify the query if the post type isn't public. |
| 525 |
if ( ! get_post_type_object( 'jetpack-comic' )->public ) { |
| 526 |
return; |
| 527 |
} |
| 528 |
|
| 529 |
$query_post_types = $query->get( 'post_type' ); |
| 530 |
|
| 531 |
if ( empty( $query_post_types ) ) { |
| 532 |
$query_post_types = 'post'; |
| 533 |
} |
| 534 |
|
| 535 |
if ( ! is_array( $query_post_types ) ) { |
| 536 |
$query_post_types = array( $query_post_types ); |
| 537 |
} |
| 538 |
|
| 539 |
if ( in_array( 'post', $query_post_types, true ) ) { |
| 540 |
$query_post_types[] = self::POST_TYPE; |
| 541 |
$query->set( 'post_type', $query_post_types ); |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
/** |
| 546 |
* API endpoint for front-end image uploading. |
| 547 |
*/ |
| 548 |
public function upload() { |
| 549 |
global $content_width; |
| 550 |
|
| 551 |
header( 'Content-Type: application/json' ); |
| 552 |
|
| 553 |
if ( |
| 554 |
empty( $_REQUEST['nonce'] ) |
| 555 |
|| ! wp_verify_nonce( sanitize_key( $_REQUEST['nonce'] ), 'jetpack_comic_upload_nonce' ) |
| 556 |
) { |
| 557 |
die( wp_json_encode( array( 'error' => __( 'Invalid or expired nonce.', 'jetpack' ) ) ) ); |
| 558 |
} |
| 559 |
|
| 560 |
$_POST['action'] = 'wp_handle_upload'; |
| 561 |
|
| 562 |
$image_id_arr = array(); |
| 563 |
$image_error_arr = array(); |
| 564 |
|
| 565 |
$i = 0; |
| 566 |
|
| 567 |
while ( isset( $_FILES[ 'image_' . $i ] ) ) { |
| 568 |
// Create attachment for the image. |
| 569 |
$image_id = media_handle_upload( "image_$i", 0 ); |
| 570 |
|
| 571 |
if ( is_wp_error( $image_id ) ) { |
| 572 |
$error = array( $image_id, $image_id->get_error_message() ); |
| 573 |
array_push( $image_error_arr, $error ); |
| 574 |
} else { |
| 575 |
array_push( $image_id_arr, $image_id ); |
| 576 |
} |
| 577 |
|
| 578 |
$i++; |
| 579 |
} |
| 580 |
|
| 581 |
if ( count( $image_id_arr ) === 0 ) { |
| 582 |
// All image uploads failed. |
| 583 |
$rv = array( 'error' => '' ); |
| 584 |
|
| 585 |
foreach ( $image_error_arr as $error ) { |
| 586 |
$rv['error'] .= $error[1] . "\n"; |
| 587 |
} |
| 588 |
} else { |
| 589 |
if ( count( $image_id_arr ) === 1 ) { |
| 590 |
$image_id = $image_id_arr[0]; |
| 591 |
|
| 592 |
// Get the image |
| 593 |
$image_src = get_the_guid( $image_id ); |
| 594 |
$image_dims = wp_get_attachment_image_src( $image_id, 'full' ); |
| 595 |
|
| 596 |
// Take off 10px of width to account for padding and border. @todo make this smarter. |
| 597 |
if ( $content_width ) { |
| 598 |
$image_width = $content_width - 10; |
| 599 |
} else { |
| 600 |
$image_width = $image_dims[1] - 10; |
| 601 |
} |
| 602 |
|
| 603 |
$image_name = isset( $_FILES['image_0']['name'] ) |
| 604 |
? sanitize_file_name( wp_unslash( $_FILES['image_0']['name'] ) ) |
| 605 |
: ''; |
| 606 |
$post_content = sprintf( |
| 607 |
'<a href="%1$s"><img src="%1$s?w=%2$d" alt="%3$s" class="size-full wp-image alignnone" id="%4$s" data-filename="%3$s"/></a>', |
| 608 |
esc_url( $image_src ), |
| 609 |
esc_attr( $image_width ), |
| 610 |
esc_attr( $image_name ), |
| 611 |
esc_attr( $image_id ) |
| 612 |
); |
| 613 |
} else { |
| 614 |
$post_content = '[gallery ids="' . esc_attr( implode( ',', $image_id_arr ) ) . '"]'; |
| 615 |
} |
| 616 |
|
| 617 |
// Create a new post with the image(s) |
| 618 |
$post_id = wp_insert_post( |
| 619 |
array( |
| 620 |
'post_content' => $post_content, |
| 621 |
'post_type' => 'jetpack-comic', |
| 622 |
'post_status' => 'draft', |
| 623 |
), |
| 624 |
true |
| 625 |
); |
| 626 |
|
| 627 |
if ( is_wp_error( $post_id, 'WP_Error' ) ) { |
| 628 |
// Failed to create the post. |
| 629 |
$rv = array( 'error' => $post_id->get_error_message() ); |
| 630 |
|
| 631 |
// Delete the uploaded images. |
| 632 |
foreach ( $image_id_arr as $image_id ) { |
| 633 |
wp_delete_post( $image_id, true ); |
| 634 |
} |
| 635 |
} else { |
| 636 |
foreach ( $image_id_arr as $image_id ) { |
| 637 |
wp_update_post( |
| 638 |
array( |
| 639 |
'ID' => $image_id, |
| 640 |
'post_parent' => $post_id, |
| 641 |
) |
| 642 |
); |
| 643 |
} |
| 644 |
|
| 645 |
if ( current_theme_supports( 'post-thumbnails' ) && count( $image_id_arr ) === 1 ) { |
| 646 |
set_post_thumbnail( $post_id, $image_id_arr[0] ); |
| 647 |
} |
| 648 |
|
| 649 |
$rv = array( |
| 650 |
'url' => add_query_arg( |
| 651 |
array( |
| 652 |
'post' => $post_id, |
| 653 |
'action' => 'edit', |
| 654 |
), |
| 655 |
admin_url( 'post.php' ) |
| 656 |
), |
| 657 |
); |
| 658 |
} |
| 659 |
} |
| 660 |
|
| 661 |
die( wp_json_encode( $rv ) ); |
| 662 |
} |
| 663 |
|
| 664 |
/** |
| 665 |
* Add comic posts to the tag and category pages. |
| 666 |
* |
| 667 |
* @param WP_Query $query Post query. |
| 668 |
* |
| 669 |
* @return WP_Query |
| 670 |
*/ |
| 671 |
public function add_posts_to_loop( $query ) { |
| 672 |
if ( ! is_admin() && $query->is_main_query() && ( $query->is_category() || $query->is_tag() ) ) { |
| 673 |
$post_types = $query->get( 'post_type' ); |
| 674 |
|
| 675 |
if ( ! $post_types || 'post' === $post_types ) { |
| 676 |
$post_types = array( 'post', self::POST_TYPE ); |
| 677 |
} elseif ( is_array( $post_types ) ) { |
| 678 |
$post_types[] = self::POST_TYPE; |
| 679 |
} |
| 680 |
|
| 681 |
$query->set( 'post_type', $post_types ); |
| 682 |
} |
| 683 |
|
| 684 |
return $query; |
| 685 |
} |
| 686 |
|
| 687 |
/** |
| 688 |
* Add to REST API post type allowed list. |
| 689 |
* |
| 690 |
* @param array $post_types Array of post types to add to the allowed list. |
| 691 |
* |
| 692 |
* @return array |
| 693 |
*/ |
| 694 |
public function allow_rest_api_type( $post_types ) { |
| 695 |
$post_types[] = self::POST_TYPE; |
| 696 |
return $post_types; |
| 697 |
} |
| 698 |
|
| 699 |
} |
| 700 |
|
| 701 |
add_action( 'init', array( 'Jetpack_Comic', 'init' ) ); |
| 702 |
|
| 703 |
/** |
| 704 |
* Custom welcome email for WordPress.com sites in the Comic vertical. |
| 705 |
* |
| 706 |
* @param string $welcome_email Body of the email. |
| 707 |
* @param int $blog_id Site ID. |
| 708 |
* @param int $user_id User ID. |
| 709 |
* @param string $password User password, or "N/A" if the user account is not new. |
| 710 |
* @param string $title Site title. |
| 711 |
* @param array $meta Signup meta data. By default, contains the requested privacy setting and lang id. |
| 712 |
* |
| 713 |
* @return string |
| 714 |
*/ |
| 715 |
function comics_welcome_email( $welcome_email, $blog_id, $user_id, $password, $title, $meta ) { |
| 716 |
if ( ( isset( $meta['vertical'] ) && 'comics' === $meta['vertical'] ) || has_blog_sticker( 'vertical-comics', $blog_id ) ) { |
| 717 |
return __( |
| 718 |
"Welcome! Ready to publish your first strip? |
| 719 |
|
| 720 |
Your webcomic's new site is ready to go. Get started by <a href=\"BLOG_URLwp-admin/customize.php#title\">setting your comic's title and tagline</a> so your readers know what it's all about. |
| 721 |
|
| 722 |
Looking for more help with setting up your site? Check out the WordPress.com <a href=\"https://learn.wordpress.com/\" target=\"_blank\">beginner's tutorial</a> and the <a href=\"https://en.support.wordpress.com/comics/\" target=\"_blank\">guide to comics on WordPress.com</a>. Dive right in by <a href=\"BLOG_URLwp-admin/customize.php#title\">publishing your first strip!</a> |
| 723 |
|
| 724 |
Lots of laughs, |
| 725 |
The WordPress.com Team", |
| 726 |
'jetpack' |
| 727 |
); |
| 728 |
} |
| 729 |
|
| 730 |
return $welcome_email; |
| 731 |
} |
| 732 |
add_filter( 'update_welcome_email_pre_replacement', 'comics_welcome_email', 10, 6 ); |
| 733 |
|