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