PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 3.2.5
Jetpack – WP Security, Backup, Speed, & Growth v3.2.5
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / custom-post-types / comics.php

comics.php in Jetpack – WP Security, Backup, Speed, & Growth 3.2.5, at modules/custom-post-types/comics.php

505 lines 17.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class Jetpack_Comic {
4 const POST_TYPE = 'jetpack-comic';
5
6 static function init() {
7 static $instance = false;
8
9 if ( ! $instance )
10 $instance = new Jetpack_Comic;
11
12 return $instance;
13 }
14
15 /**
16 * Conditionally hook into WordPress.
17 *
18 * Themes must declare that they support this module by adding
19 * add_theme_support( 'jetpack-comic' ); during after_setup_theme.
20 *
21 * If no theme support is found there is no need to hook into
22 * WordPress. We'll just return early instead.
23 */
24 function __construct() {
25 // Make sure the post types are loaded for imports
26 add_action( 'import_start', array( $this, 'register_post_types' ) );
27
28 // Return early if theme does not support Jetpack Comic.
29 if ( ! ( $this->site_supports_comics() ) )
30 return;
31
32 $this->register_post_types();
33
34 add_action( 'pre_get_posts', array( $this, 'add_posts_to_loop' ) );
35
36 // In order for the Feedbag job to find Comic posts, we need to circumvent any pretty
37 // URLs in the RSS feed given to Feedbag in favor of /?p=123&post_type=jetpack-comic
38 add_filter( 'the_permalink_rss', array( $this, 'custom_permalink_for_feedbag' ) );
39
40 // There are some cases (like when Feedbag is fetching posts) that the comics
41 // post type needs to be registered no matter what, but none of the UI needs to be
42 // available.
43
44 // Enable Omnisearch for Comic posts.
45 if ( class_exists( 'Jetpack_Omnisearch_Posts' ) )
46 new Jetpack_Omnisearch_Posts( self::POST_TYPE );
47
48 add_filter( 'post_updated_messages', array( $this, 'updated_messages' ) );
49
50 if ( function_exists( 'queue_publish_post' ) ) {
51 add_action( 'publish_jetpack-comic', 'queue_publish_post', 10, 2 );
52 }
53
54 add_action( 'pre_get_posts', array( $this, 'include_in_feeds' ) );
55
56 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
57
58 add_filter( 'manage_' . self::POST_TYPE . '_posts_columns', array( $this, 'manage_posts_columns' ) );
59 add_action( 'manage_' . self::POST_TYPE . '_posts_custom_column', array( $this, 'manage_posts_custom_column' ), 10, 2 );
60 add_image_size( 'jetpack-comic-thumb', 150, 0, false );
61
62 // Enable front-end uploading for users special enough.
63 if ( current_user_can( 'upload_files' ) && current_user_can( 'edit_posts' ) ) {
64 add_action( 'wp_ajax_jetpack_comic_upload', array( $this, 'upload' ) );
65 add_action( 'wp_enqueue_scripts', array( $this, 'register_scripts' ) );
66 }
67
68 /**
69 * Add a "Convert to Comic" and "Convert to Post" option to the bulk
70 * edit dropdowns.
71 */
72 add_action( 'admin_footer-edit.php', array( $this, 'admin_footer' ) );
73 add_action( 'load-edit.php', array( $this, 'bulk_edit' ) );
74 add_action( 'admin_notices', array( $this, 'bulk_edit_notices' ) );
75 }
76
77 public function admin_footer() {
78 $post_type = get_post_type();
79
80 ?>
81 <script type="text/javascript">
82 jQuery( document ).ready( function( $ ) {
83 <?php if ( ! $post_type || 'post' == $post_type ) { ?>
84 $( '<option>' )
85 .val( 'post2comic' )
86 .text( <?php echo json_encode( __( 'Convert to Comic', 'jetpack' ) ); ?> )
87 .appendTo( "select[name='action'], select[name='action2']" );
88 <?php } ?>
89 <?php if ( ! $post_type || self::POST_TYPE == $post_type ) { ?>
90 $( '<option>' )
91 .val( 'comic2post' )
92 .text( <?php echo json_encode( __( 'Convert to Post', 'jetpack' ) ); ?> )
93 .appendTo( "select[name='action'], select[name='action2']" );
94 <?php } ?>
95
96 $( '#message.jetpack-comic-post-type-conversion' ).remove().insertAfter( $( '.wrap h2:first' ) ).show();
97 });
98 </script>
99 <?php
100 }
101
102 /**
103 * Handle the "Convert to [Post|Comic]" bulk action.
104 */
105 public function bulk_edit() {
106 if ( empty( $_REQUEST['post'] ) )
107 return;
108
109 $wp_list_table = _get_list_table( 'WP_Posts_List_Table' );
110 $action = $wp_list_table->current_action();
111
112 check_admin_referer( 'bulk-posts' );
113
114 if ( 'post2comic' == $action || 'comic2post' == $action ) {
115 if ( ! current_user_can( 'publish_posts' ) )
116 wp_die( __( 'You are not allowed to make this change.', 'jetpack' ) );
117
118 $post_ids = array_map( 'intval', $_REQUEST['post'] );
119
120 $modified_count = 0;
121
122 foreach ( $post_ids as $post_id ) {
123 $destination_post_type = ( $action == 'post2comic' ) ? self::POST_TYPE : 'post';
124 $origin_post_type = ( $destination_post_type == 'post' ) ? self::POST_TYPE : 'post';
125
126 if ( current_user_can( 'edit_post', $post_id ) ) {
127 $post = get_post( $post_id );
128
129 // Only convert posts that are post => comic or comic => post.
130 // (e.g., Ignore comic => comic, page => post, etc. )
131 if ( $post->post_type != $destination_post_type && $post->post_type == $origin_post_type ) {
132 $post_type_object = get_post_type_object( $destination_post_type );
133
134 if ( current_user_can( $post_type_object->cap->publish_posts ) ) {
135 set_post_type( $post_id, $destination_post_type );
136 $modified_count++;
137 }
138 }
139 }
140 }
141
142 $sendback = remove_query_arg( array( 'exported', 'untrashed', 'deleted', 'ids' ), wp_get_referer() );
143
144 if ( ! $sendback )
145 $sendback = add_query_arg( array( 'post_type', get_post_type() ), admin_url( 'edit.php' ) );
146
147 $pagenum = $wp_list_table->get_pagenum();
148 $sendback = add_query_arg( array( 'paged' => $pagenum, 'post_type_changed' => $modified_count ), $sendback );
149
150 wp_safe_redirect( $sendback );
151 exit();
152 }
153 }
154
155 /**
156 * Show the post conversion success notice.
157 */
158 public function bulk_edit_notices() {
159 global $pagenow;
160
161 if ( 'edit.php' == $pagenow && ! empty( $_GET['post_type_changed'] ) ) {
162 ?><div id="message" class="updated below-h2 jetpack-comic-post-type-conversion" style="display: none;"><p><?php
163 printf( _n( 'Post converted.', '%s posts converted', $_GET['post_type_changed'], 'jetpack' ), number_format_i18n( $_GET['post_type_changed'] ) );
164 ?></p></div><?php
165 }
166 }
167
168 public function register_scripts() {
169 if( is_rtl() ) {
170 wp_enqueue_style( 'jetpack-comics-style', plugins_url( 'comics/rtl/comics-rtl.css', __FILE__ ) );
171 } else {
172 wp_enqueue_style( 'jetpack-comics-style', plugins_url( 'comics/comics.css', __FILE__ ) );
173 }
174
175 wp_enqueue_script( 'jetpack-comics', plugins_url( 'comics/comics.js', __FILE__ ), array( 'jquery', 'jquery.spin' ) );
176
177 $options = array(
178 'nonce' => wp_create_nonce( 'jetpack_comic_upload_nonce' ),
179 'writeURL' => admin_url( 'admin-ajax.php?action=jetpack_comic_upload' ),
180 'labels' => array(
181 'dragging' => __( 'Drop images to upload', 'jetpack' ),
182 'uploading' => __( 'Uploading...', 'jetpack' ),
183 'processing' => __( 'Processing...', 'jetpack' ),
184 'unsupported' => __( "Sorry, your browser isn't supported. Upgrade at browsehappy.com.", 'jetpack' ),
185 'invalidUpload' => __( 'Only images can be uploaded here.', 'jetpack' ),
186 'error' => __( "Your upload didn't complete; try again later or cross your fingers and try again right now.", 'jetpack' ),
187 )
188 );
189
190 wp_localize_script( 'jetpack-comics', 'Jetpack_Comics_Options', $options );
191 }
192
193 public function admin_enqueue_scripts() {
194 wp_enqueue_style( 'jetpack-comics-admin', plugins_url( 'comics/admin.css', __FILE__ ) );
195 }
196
197 function register_post_types() {
198 if ( post_type_exists( self::POST_TYPE ) ) {
199 return;
200 }
201
202 register_post_type( self::POST_TYPE, array(
203 'description' => __( 'Comics', 'jetpack' ),
204 'labels' => array(
205 'name' => esc_html__( 'Comics', 'jetpack' ),
206 'singular_name' => esc_html__( 'Comic', 'jetpack' ),
207 'menu_name' => esc_html__( 'Comics', 'jetpack' ),
208 'all_items' => esc_html__( 'All Comics', 'jetpack' ),
209 'add_new' => esc_html__( 'Add New', 'jetpack' ),
210 'add_new_item' => esc_html__( 'Add New Comic', 'jetpack' ),
211 'edit_item' => esc_html__( 'Edit Comic', 'jetpack' ),
212 'new_item' => esc_html__( 'New Comic', 'jetpack' ),
213 'view_item' => esc_html__( 'View Comic', 'jetpack' ),
214 'search_items' => esc_html__( 'Search Comics', 'jetpack' ),
215 'not_found' => esc_html__( 'No Comics found', 'jetpack' ),
216 'not_found_in_trash' => esc_html__( 'No Comics found in Trash', 'jetpack' ),
217 ),
218 'supports' => array(
219 'title',
220 'editor',
221 'thumbnail',
222 'comments',
223 'revisions',
224 'publicize', // Jetpack
225 'subscriptions', // wpcom
226 'shortlinks', // Jetpack
227 ),
228 'rewrite' => array(
229 'slug' => 'comic',
230 'with_front' => false,
231 ),
232 'taxonomies' => array(
233 'category',
234 'post_tag',
235 ),
236 // Only make the type public for sites that support Comics.
237 'public' => true,
238 'menu_position' => 5, // below Posts
239 'map_meta_cap' => true,
240 'has_archive' => true,
241 'query_var' => 'comic',
242 ) );
243 }
244
245 public function manage_posts_columns( $columns ) {
246 $new_columns = array(
247 'preview-jetpack-comic' => __( 'Preview', 'jetpack' ),
248 );
249 return array_merge( array_slice( $columns, 0, 2 ), $new_columns, array_slice( $columns, 2 ) );
250 }
251
252 public function manage_posts_custom_column( $column_name, $post_ID ) {
253 if ( 'preview-jetpack-comic' == $column_name && has_post_thumbnail( $post_ID ) ) {
254 echo get_the_post_thumbnail( $post_ID, 'jetpack-comic-thumb' );
255 }
256 }
257
258 /**
259 * The function url_to_postid() doesn't handle pretty permalinks
260 * for CPTs very well. When we're generating an RSS feed to be consumed
261 * for Feedbag (the Reader's feed storage mechanism), eschew
262 * a pretty URL for one that will get the post into the Reader.
263 *
264 * @see http://core.trac.wordpress.org/ticket/19744
265 * @param string $permalink The existing (possibly pretty) permalink.
266 */
267 public function custom_permalink_for_feedbag( $permalink ) {
268 global $post;
269
270 if ( ! empty( $GLOBALS['is_feedbag_rss_script'] ) && self::POST_TYPE == $post->post_type ) {
271 $permalink = home_url( add_query_arg( array( 'p' => $post->ID, 'post_type' => self::POST_TYPE ), '?' ) );
272 }
273
274 return $permalink;
275 }
276
277 /*
278 * Update messages for the Comic admin.
279 */
280 function updated_messages( $messages ) {
281 global $post;
282
283 $messages['jetpack-comic'] = array(
284 0 => '', // Unused. Messages start at index 1.
285 1 => sprintf( __( 'Comic updated. <a href="%s">View comic</a>', 'jetpack'), esc_url( get_permalink( $post->ID ) ) ),
286 2 => esc_html__( 'Custom field updated.', 'jetpack' ),
287 3 => esc_html__( 'Custom field deleted.', 'jetpack' ),
288 4 => esc_html__( 'Comic updated.', 'jetpack' ),
289 /* translators: %s: date and time of the revision */
290 5 => isset( $_GET['revision'] ) ? sprintf( esc_html__( 'Comic restored to revision from %s', 'jetpack'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
291 6 => sprintf( __( 'Comic published. <a href="%s">View comic</a>', 'jetpack' ), esc_url( get_permalink( $post->ID ) ) ),
292 7 => esc_html__( 'Comic saved.', 'jetpack' ),
293 8 => sprintf( __( 'Comic submitted. <a target="_blank" href="%s">Preview comic</a>', 'jetpack'), esc_url( add_query_arg( 'preview', 'true', get_permalink( $post->ID ) ) ) ),
294 9 => sprintf( __( 'Comic scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview comic</a>', 'jetpack' ),
295 // translators: Publish box date format, see http://php.net/date
296 date_i18n( __( 'M j, Y @ G:i', 'jetpack' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post->ID) ) ),
297 10 => sprintf( __( 'Comic draft updated. <a target="_blank" href="%s">Preview comic</a>', 'jetpack' ), esc_url( add_query_arg( 'preview', 'true', get_permalink( $post->ID ) ) ) ),
298 );
299
300 return $messages;
301 }
302
303 /**
304 * Should this Custom Post Type be made available?
305 */
306 public function site_supports_comics() {
307 /**
308 * @todo: Extract this out into a wpcom only file.
309 */
310 if ( 'blog-rss.php' == substr( $_SERVER['PHP_SELF'], -12 ) && count( $_SERVER['argv'] ) > 1 ) {
311 // blog-rss.php isn't run in the context of the target blog when the init action fires,
312 // so check manually whether the target blog supports comics.
313 switch_to_blog( $_SERVER['argv'][1] );
314 // The add_theme_support( 'jetpack-comic' ) won't fire on switch_to_blog, so check for Panel manually.
315 $supports_comics = ( ( function_exists( 'site_vertical' ) && 'comics' == site_vertical() )
316 || current_theme_supports( self::POST_TYPE )
317 || get_stylesheet() == 'pub/panel' );
318 restore_current_blog();
319 return (bool) apply_filters( 'jetpack_enable_cpt', $supports_comics, self::POST_TYPE );
320 }
321
322 $supports_comics = false;
323
324 /**
325 * If we're on WordPress.com, and it has the menu site vertical.
326 * @todo: Extract this out into a wpcom only file.
327 */
328 if ( function_exists( 'site_vertical' ) && 'comics' == site_vertical() ) {
329 $supports_comics = true;
330 }
331
332 /**
333 * Else, if the current theme requests it.
334 */
335 if ( current_theme_supports( self::POST_TYPE ) ) {
336 $supports_comics = true;
337 }
338
339 /**
340 * Filter it in case something else knows better.
341 */
342 return (bool) apply_filters( 'jetpack_enable_cpt', $supports_comics, self::POST_TYPE );
343 }
344
345 /**
346 * Anywhere that a feed is displaying posts, show comics too.
347 *
348 * @param WP_Query $query
349 */
350 public function include_in_feeds( $query ) {
351 if ( ! $query->is_feed() )
352 return;
353
354 // Don't modify the query if the post type isn't public.
355 if ( ! get_post_type_object( 'jetpack-comic' )->public )
356 return;
357
358 $query_post_types = $query->get( 'post_type' );
359
360 if ( empty( $query_post_types ) )
361 $query_post_types = 'post';
362
363 if ( ! is_array( $query_post_types ) )
364 $query_post_types = array( $query_post_types );
365
366 if ( in_array( 'post', $query_post_types ) ) {
367 $query_post_types[] = self::POST_TYPE;
368 $query->set( 'post_type', $query_post_types );
369 }
370 }
371
372 /**
373 * API endpoint for front-end image uploading.
374 */
375 public function upload() {
376 global $content_width;
377
378 header( 'Content-Type: application/json' );
379
380 if ( ! wp_verify_nonce( $_REQUEST['nonce'], 'jetpack_comic_upload_nonce' ) )
381 die( json_encode( array( 'error' => __( 'Invalid or expired nonce.', 'jetpack' ) ) ) );
382
383 $_POST['action'] = 'wp_handle_upload';
384
385 $image_id_arr = array();
386 $image_error_arr = array();
387
388 $i = 0;
389
390 while ( isset( $_FILES['image_' . $i ] ) ) {
391 // Create attachment for the image.
392 $image_id = media_handle_upload( "image_$i", 0 );
393
394 if ( is_wp_error( $image_id ) ) {
395 $error = array( $image_id, $image_id->get_error_message() );
396 array_push( $image_error_arr, $error );
397 } else {
398 array_push( $image_id_arr, $image_id );
399 }
400
401 $i++;
402 }
403
404 if ( count( $image_id_arr ) == 0 ) {
405 // All image uploads failed.
406 $rv = array( 'error' => '' );
407
408 foreach ( $image_error_arr as $error )
409 $rv['error'] .= $error[1] . "\n";
410 }
411 else {
412 if ( count( $image_id_arr ) == 1 ) {
413 $image_id = $image_id_arr[0];
414
415 // Get the image
416 $image_src = get_the_guid( $image_id );
417 $image_dims = wp_get_attachment_image_src( $image_id, 'full' );
418
419 // Take off 10px of width to account for padding and border. @todo make this smarter.
420 if ( $content_width )
421 $image_width = $content_width - 10;
422 else
423 $image_width = $image_dims[1] - 10;
424
425 $post_content = '<a href="' . esc_attr( $image_src ) .'"><img src="' . esc_attr( $image_src ) . '?w=' . esc_attr( $image_width ) . '" alt="' . esc_attr( $_FILES['image_0']['name'] ) . '" class="size-full wp-image alignnone" id="i-' . esc_attr( $image_id ) . '" data-filename="' . esc_attr( $_FILES['image_0']['name'] ) . '" /></a>';
426 }
427 else {
428 $post_content = '[gallery ids="' . esc_attr( implode( ',', $image_id_arr ) ) . '"]';
429 }
430
431 // Create a new post with the image(s)
432 $post_id = wp_insert_post( array(
433 'post_content' => $post_content,
434 'post_type' => 'jetpack-comic',
435 'post_status' => 'draft',
436 ),
437 true
438 );
439
440 if ( is_wp_error( $post_id, 'WP_Error' ) ) {
441 // Failed to create the post.
442 $rv = array( 'error' => $post_id->get_error_message() );
443
444 // Delete the uploaded images.
445 foreach ( $image_id_arr as $image_id ) {
446 wp_delete_post( $image_id, true );
447 }
448 }
449 else {
450 foreach ( $image_id_arr as $image_id ) {
451 wp_update_post( array(
452 'ID' => $image_id,
453 'post_parent' => $post_id
454 ) );
455 }
456
457 if ( current_theme_supports( 'post-thumbnails' ) && count( $image_id_arr ) == 1 )
458 set_post_thumbnail( $post_id, $image_id_arr[0] );
459
460 $rv = array( 'url' => add_query_arg( array( 'post' => $post_id, 'action' => 'edit' ), admin_url( 'post.php' ) ) );
461 }
462 }
463
464 die( json_encode( $rv ) );
465 }
466
467 public function add_posts_to_loop( $query ) {
468 // Add comic posts to the tag and category pages.
469 if ( ! is_admin() && $query->is_main_query() && ( $query->is_category() || $query->is_tag() ) ) {
470 $post_types = $query->get( 'post_type' );
471
472 if ( ! $post_types || 'post' == $post_types )
473 $post_types = array( 'post', self::POST_TYPE );
474 else if ( is_array( $post_types ) )
475 $post_types[] = self::POST_TYPE;
476
477 $query->set( 'post_type', $post_types );
478 }
479
480 return $query;
481 }
482
483 }
484
485 add_action( 'init', array( 'Jetpack_Comic', 'init' ) );
486
487
488 function comics_welcome_email( $welcome_email, $blog_id, $user_id, $password, $title, $meta ) {
489 if ( ( isset( $meta['vertical'] ) && 'comics' == $meta['vertical'] ) || has_blog_sticker( 'vertical-comics', $blog_id ) ) {
490 return __( "Welcome! Ready to publish your first strip?
491
492 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.
493
494 Looking for more help with setting up your site? Check out the WordPress.com <a href=\"http://learn.wordpress.com/\">beginner's tutorial</a> and the <a href=\"http://en.support.wordpress.com/comics/\">guide to comics on WordPress.com</a>. Dive right in by <a href=\"BLOG_URLwp-admin/customize.php#title\">publishing your first strip!</a>
495
496 Lots of laughs,
497 The WordPress.com Team", 'jetpack' );
498 }
499
500 return $welcome_email;
501 }
502
503 add_filter( 'update_welcome_email_pre_replacement', 'comics_welcome_email', 10, 6 );
504
505