PageRenderTime 59ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/site/wp-content/plugins/jetpack/sync/class.jetpack-sync-module-posts.php

https://bitbucket.org/ericstrom/ballmerpeak
PHP | 410 lines | 247 code | 73 blank | 90 comment | 34 complexity | c093de41d16b42910d4a070801b0fcba MD5 | raw file
  1. <?php
  2. require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php';
  3. class Jetpack_Sync_Module_Posts extends Jetpack_Sync_Module {
  4. private $just_published = array();
  5. private $previous_status = array();
  6. private $action_handler;
  7. private $import_end = false;
  8. const DEFAULT_PREVIOUS_STATE = 'new';
  9. public function name() {
  10. return 'posts';
  11. }
  12. public function get_object_by_id( $object_type, $id ) {
  13. if ( $object_type === 'post' && $post = get_post( intval( $id ) ) ) {
  14. return $this->filter_post_content_and_add_links( $post );
  15. }
  16. return false;
  17. }
  18. public function init_listeners( $callable ) {
  19. $this->action_handler = $callable;
  20. // Core < 4.7 doesn't deal with nested wp_insert_post calls very well
  21. global $wp_version;
  22. $priority = version_compare( $wp_version, '4.7-alpha', '<' ) ? 0 : 11;
  23. add_action( 'wp_insert_post', array( $this, 'wp_insert_post' ), $priority, 3 );
  24. add_action( 'jetpack_sync_save_post', $callable, 10, 4 );
  25. add_action( 'deleted_post', $callable, 10 );
  26. add_action( 'jetpack_published_post', $callable, 10, 2 );
  27. add_action( 'transition_post_status', array( $this, 'save_published' ), 10, 3 );
  28. add_filter( 'jetpack_sync_before_enqueue_jetpack_sync_save_post', array( $this, 'filter_blacklisted_post_types' ) );
  29. // listen for meta changes
  30. $this->init_listeners_for_meta_type( 'post', $callable );
  31. $this->init_meta_whitelist_handler( 'post', array( $this, 'filter_meta' ) );
  32. add_action( 'jetpack_daily_akismet_meta_cleanup_before', array( $this, 'daily_akismet_meta_cleanup_before' ) );
  33. add_action( 'jetpack_daily_akismet_meta_cleanup_after', array( $this, 'daily_akismet_meta_cleanup_after' ) );
  34. add_action( 'jetpack_post_meta_batch_delete', $callable, 10, 2 );
  35. }
  36. public function daily_akismet_meta_cleanup_before( $feedback_ids ) {
  37. remove_action( 'deleted_post_meta', $this->action_handler );
  38. /**
  39. * Used for syncing deletion of batch post meta
  40. *
  41. * @since 6.1.0
  42. *
  43. * @module sync
  44. *
  45. * $param array $feedback_ids feedback post IDs
  46. * $param string $meta_key to be deleted
  47. */
  48. do_action( 'jetpack_post_meta_batch_delete', $feedback_ids, '_feedback_akismet_values');
  49. }
  50. public function daily_akismet_meta_cleanup_after( $feedback_ids ) {
  51. add_action( 'deleted_post_meta', $this->action_handler );
  52. }
  53. public function init_full_sync_listeners( $callable ) {
  54. add_action( 'jetpack_full_sync_posts', $callable ); // also sends post meta
  55. }
  56. public function init_before_send() {
  57. add_filter( 'jetpack_sync_before_send_jetpack_sync_save_post', array( $this, 'expand_jetpack_sync_save_post' ) );
  58. // full sync
  59. add_filter( 'jetpack_sync_before_send_jetpack_full_sync_posts', array( $this, 'expand_post_ids' ) );
  60. }
  61. public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
  62. global $wpdb;
  63. return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_posts', $wpdb->posts, 'ID', $this->get_where_sql( $config ), $max_items_to_enqueue, $state );
  64. }
  65. public function estimate_full_sync_actions( $config ) {
  66. global $wpdb;
  67. $query = "SELECT count(*) FROM $wpdb->posts WHERE " . $this->get_where_sql( $config );
  68. $count = $wpdb->get_var( $query );
  69. return (int) ceil( $count / self::ARRAY_CHUNK_SIZE );
  70. }
  71. private function get_where_sql( $config ) {
  72. $where_sql = Jetpack_Sync_Settings::get_blacklisted_post_types_sql();
  73. // config is a list of post IDs to sync
  74. if ( is_array( $config ) ) {
  75. $where_sql .= ' AND ID IN (' . implode( ',', array_map( 'intval', $config ) ) . ')';
  76. }
  77. return $where_sql;
  78. }
  79. function get_full_sync_actions() {
  80. return array( 'jetpack_full_sync_posts' );
  81. }
  82. /**
  83. * Process content before send
  84. *
  85. * @param array $args wp_insert_post arguments
  86. *
  87. * @return array
  88. */
  89. function expand_jetpack_sync_save_post( $args ) {
  90. list( $post_id, $post, $update, $previous_state ) = $args;
  91. return array( $post_id, $this->filter_post_content_and_add_links( $post ), $update, $previous_state );
  92. }
  93. function filter_blacklisted_post_types( $args ) {
  94. $post = $args[1];
  95. if ( in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) ) ) {
  96. return false;
  97. }
  98. return $args;
  99. }
  100. // Meta
  101. function filter_meta( $args ) {
  102. if ( $this->is_post_type_allowed( $args[1] ) && $this->is_whitelisted_post_meta( $args[2] ) ) {
  103. return $args;
  104. }
  105. return false;
  106. }
  107. function is_whitelisted_post_meta( $meta_key ) {
  108. // _wpas_skip_ is used by publicize
  109. return in_array( $meta_key, Jetpack_Sync_Settings::get_setting( 'post_meta_whitelist' ) ) || wp_startswith( $meta_key, '_wpas_skip_' );
  110. }
  111. function is_post_type_allowed( $post_id ) {
  112. $post = get_post( intval( $post_id ) );
  113. if( $post->post_type ) {
  114. return ! in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) );
  115. }
  116. return false;
  117. }
  118. function remove_embed() {
  119. global $wp_embed;
  120. remove_filter( 'the_content', array( $wp_embed, 'run_shortcode' ), 8 );
  121. // remove the embed shortcode since we would do the part later.
  122. remove_shortcode( 'embed' );
  123. // Attempts to embed all URLs in a post
  124. remove_filter( 'the_content', array( $wp_embed, 'autoembed' ), 8 );
  125. }
  126. function add_embed() {
  127. global $wp_embed;
  128. add_filter( 'the_content', array( $wp_embed, 'run_shortcode' ), 8 );
  129. // Shortcode placeholder for strip_shortcodes()
  130. add_shortcode( 'embed', '__return_false' );
  131. // Attempts to embed all URLs in a post
  132. add_filter( 'the_content', array( $wp_embed, 'autoembed' ), 8 );
  133. }
  134. // Expands wp_insert_post to include filtered content
  135. function filter_post_content_and_add_links( $post_object ) {
  136. global $post;
  137. $post = $post_object;
  138. // return non existant post
  139. $post_type = get_post_type_object( $post->post_type );
  140. if ( empty( $post_type ) || ! is_object( $post_type ) ) {
  141. $non_existant_post = new stdClass();
  142. $non_existant_post->ID = $post->ID;
  143. $non_existant_post->post_modified = $post->post_modified;
  144. $non_existant_post->post_modified_gmt = $post->post_modified_gmt;
  145. $non_existant_post->post_status = 'jetpack_sync_non_registered_post_type';
  146. return $non_existant_post;
  147. }
  148. /**
  149. * Filters whether to prevent sending post data to .com
  150. *
  151. * Passing true to the filter will prevent the post data from being sent
  152. * to the WordPress.com.
  153. * Instead we pass data that will still enable us to do a checksum against the
  154. * Jetpacks data but will prevent us from displaying the data on in the API as well as
  155. * other services.
  156. * @since 4.2.0
  157. *
  158. * @param boolean false prevent post data from being synced to WordPress.com
  159. * @param mixed $post WP_POST object
  160. */
  161. if ( apply_filters( 'jetpack_sync_prevent_sending_post_data', false, $post ) ) {
  162. // We only send the bare necessary object to be able to create a checksum.
  163. $blocked_post = new stdClass();
  164. $blocked_post->ID = $post->ID;
  165. $blocked_post->post_modified = $post->post_modified;
  166. $blocked_post->post_modified_gmt = $post->post_modified_gmt;
  167. $blocked_post->post_status = 'jetpack_sync_blocked';
  168. return $blocked_post;
  169. }
  170. // lets not do oembed just yet.
  171. $this->remove_embed();
  172. if ( 0 < strlen( $post->post_password ) ) {
  173. $post->post_password = 'auto-' . wp_generate_password( 10, false );
  174. }
  175. /** This filter is already documented in core. wp-includes/post-template.php */
  176. if ( Jetpack_Sync_Settings::get_setting( 'render_filtered_content' ) && $post_type->public ) {
  177. global $shortcode_tags;
  178. /**
  179. * Filter prevents some shortcodes from expanding.
  180. *
  181. * Since we can can expand some type of shortcode better on the .com side and make the
  182. * expansion more relevant to contexts. For example [galleries] and subscription emails
  183. *
  184. * @since 4.5.0
  185. *
  186. * @param array of shortcode tags to remove.
  187. */
  188. $shortcodes_to_remove = apply_filters( 'jetpack_sync_do_not_expand_shortcodes', array(
  189. 'gallery',
  190. 'slideshow'
  191. ) );
  192. $removed_shortcode_callbacks = array();
  193. foreach ( $shortcodes_to_remove as $shortcode ) {
  194. if ( isset ( $shortcode_tags[ $shortcode ] ) ) {
  195. $removed_shortcode_callbacks[ $shortcode ] = $shortcode_tags[ $shortcode ];
  196. }
  197. }
  198. array_map( 'remove_shortcode', array_keys( $removed_shortcode_callbacks ) );
  199. $post->post_content_filtered = apply_filters( 'the_content', $post->post_content );
  200. $post->post_excerpt_filtered = apply_filters( 'the_excerpt', $post->post_excerpt );
  201. foreach ( $removed_shortcode_callbacks as $shortcode => $callback ) {
  202. add_shortcode( $shortcode, $callback );
  203. }
  204. }
  205. $this->add_embed();
  206. if ( has_post_thumbnail( $post->ID ) ) {
  207. $image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
  208. if ( is_array( $image_attributes ) && isset( $image_attributes[0] ) ) {
  209. $post->featured_image = $image_attributes[0];
  210. }
  211. }
  212. $post->permalink = get_permalink( $post->ID );
  213. $post->shortlink = wp_get_shortlink( $post->ID );
  214. if ( function_exists( 'amp_get_permalink' ) ) {
  215. $post->amp_permalink = amp_get_permalink( $post->ID );
  216. }
  217. return $post;
  218. }
  219. public function save_published( $new_status, $old_status, $post ) {
  220. if ( 'publish' === $new_status && 'publish' !== $old_status ) {
  221. $this->just_published[ $post->ID ] = true;
  222. }
  223. $this->previous_status[ $post->ID ] = $old_status;
  224. }
  225. public function wp_insert_post( $post_ID, $post = null, $update = null ) {
  226. if ( ! is_numeric( $post_ID ) || is_null( $post ) ) {
  227. return;
  228. }
  229. // workaround for https://github.com/woocommerce/woocommerce/issues/18007
  230. if ( $post && 'shop_order' === $post->post_type ) {
  231. $post = get_post( $post_ID );
  232. }
  233. $previous_status = isset( $this->previous_status[ $post_ID ] ) ?
  234. $this->previous_status[ $post_ID ] :
  235. self::DEFAULT_PREVIOUS_STATE;
  236. $just_published = isset( $this->just_published[ $post_ID ] ) ?
  237. $this->just_published[ $post_ID ] :
  238. false;
  239. $state = array(
  240. 'is_auto_save' => (bool) Jetpack_Constants::get_constant( 'DOING_AUTOSAVE' ),
  241. 'previous_status' => $previous_status,
  242. 'just_published' => $just_published
  243. );
  244. /**
  245. * Filter that is used to add to the post flags ( meta data ) when a post gets published
  246. *
  247. * @since 5.8.0
  248. *
  249. * @param int $post_ID the post ID
  250. * @param mixed $post WP_POST object
  251. * @param bool $update Whether this is an existing post being updated or not.
  252. * @param mixed $state state
  253. *
  254. * @module sync
  255. */
  256. do_action( 'jetpack_sync_save_post', $post_ID, $post, $update, $state );
  257. unset( $this->previous_status[ $post_ID ] );
  258. $this->send_published( $post_ID, $post );
  259. }
  260. public function send_published( $post_ID, $post ) {
  261. if ( ! isset( $this->just_published[ $post_ID ] ) ) {
  262. return;
  263. }
  264. // Post revisions cause race conditions where this send_published add the action before the actual post gets synced
  265. if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) {
  266. return;
  267. }
  268. $post_flags = array(
  269. 'post_type' => $post->post_type
  270. );
  271. $author_user_object = get_user_by( 'id', $post->post_author );
  272. if ( $author_user_object ) {
  273. $post_flags['author'] = array(
  274. 'id' => $post->post_author,
  275. 'wpcom_user_id' => get_user_meta( $post->post_author, 'wpcom_user_id', true ),
  276. 'display_name' => $author_user_object->display_name,
  277. 'email' => $author_user_object->user_email,
  278. 'translated_role' => Jetpack::translate_user_to_role( $author_user_object ),
  279. );
  280. }
  281. /**
  282. * Filter that is used to add to the post flags ( meta data ) when a post gets published
  283. *
  284. * @since 4.4.0
  285. *
  286. * @param mixed array post flags that are added to the post
  287. * @param mixed $post WP_POST object
  288. */
  289. $flags = apply_filters( 'jetpack_published_post_flags', $post_flags, $post );
  290. /**
  291. * Action that gets synced when a post type gets published.
  292. *
  293. * @since 4.4.0
  294. *
  295. * @param int $post_ID
  296. * @param mixed array $flags post flags that are added to the post
  297. */
  298. do_action( 'jetpack_published_post', $post_ID, $flags );
  299. unset( $this->just_published[ $post_ID ] );
  300. /**
  301. * Send additional sync action for Activity Log when post is a Customizer publish
  302. */
  303. if ( 'customize_changeset' == $post->post_type ) {
  304. $post_content = json_decode( $post->post_content, true );
  305. foreach ( $post_content as $key => $value ) {
  306. //Skip if it isn't a widget
  307. if ( 'widget_' != substr( $key, 0, strlen( 'widget_' ) ) ) {
  308. continue;
  309. }
  310. // Change key from "widget_archives[2]" to "archives-2"
  311. $key = str_replace( 'widget_', '', $key );
  312. $key = str_replace( '[', '-', $key );
  313. $key = str_replace( ']', '', $key );
  314. global $wp_registered_widgets;
  315. if ( isset( $wp_registered_widgets[ $key ] ) ) {
  316. $widget_data = array(
  317. 'name' => $wp_registered_widgets[ $key ]['name'],
  318. 'id' => $key,
  319. 'title' => $value['value']['title'],
  320. );
  321. do_action( 'jetpack_widget_edited', $widget_data );
  322. }
  323. }
  324. }
  325. }
  326. public function expand_post_ids( $args ) {
  327. $post_ids = $args[0];
  328. $posts = array_filter( array_map( array( 'WP_Post', 'get_instance' ), $post_ids ) );
  329. $posts = array_map( array( $this, 'filter_post_content_and_add_links' ), $posts );
  330. $posts = array_values( $posts ); // reindex in case posts were deleted
  331. return array(
  332. $posts,
  333. $this->get_metadata( $post_ids, 'post', Jetpack_Sync_Settings::get_setting( 'post_meta_whitelist' ) ),
  334. $this->get_term_relationships( $post_ids ),
  335. );
  336. }
  337. }