PageRenderTime 24ms CodeModel.GetById 15ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/wp-content/plugins/wordpress-seo/admin/links/class-link-reindex-post-service.php

https://bitbucket.org/carloskikea/helpet
PHP | 95 lines | 39 code | 14 blank | 42 comment | 4 complexity | 640cd2fadf8de0642a6254d1d30ff652 MD5 | raw file
 1<?php
 2/**
 3 * WPSEO plugin file.
 4 *
 5 * @package WPSEO\Admin\Links\Reindex
 6 */
 7
 8/**
 9 * Class WPSEO_Link_Reindex_Post_Service
10 */
11class WPSEO_Link_Reindex_Post_Service {
12
13	/**
14	 * Reindexes the unprocessed posts by REST request.
15	 *
16	 * @return WP_REST_Response The response object.
17	 */
18	public function reindex() {
19		return new WP_REST_Response( $this->process_posts() );
20	}
21
22	/**
23	 * Returns the posts.
24	 *
25	 * @return int The total amount of unprocessed posts.
26	 */
27	protected function process_posts() {
28		if ( ! $this->is_processable() ) {
29			return 0;
30		}
31
32		$posts = $this->get_unprocessed_posts();
33		array_walk( $posts, array( $this, 'process_post' ) );
34
35		return count( $posts );
36	}
37
38	/**
39	 * Returns all unprocessed posts.
40	 *
41	 * @return array The unprocessed posts.
42	 */
43	protected function get_unprocessed_posts() {
44		$post_types = apply_filters( 'wpseo_link_count_post_types', WPSEO_Post_Type::get_accessible_post_types() );
45		if ( ! is_array( $post_types ) ) {
46			return array();
47		}
48		return WPSEO_Link_Query::get_unprocessed_posts( $post_types );
49	}
50
51	/**
52	 * Checks if the required tables are accessible.
53	 *
54	 * @return bool True when the tables are accessible.
55	 */
56	protected function is_processable() {
57		return WPSEO_Link_Table_Accessible::is_accessible() && WPSEO_Meta_Table_Accessible::is_accessible();
58	}
59
60	/**
61	 * Processes the post.
62	 *
63	 * @param stdObject $post The post to process.
64	 *
65	 * @return void
66	 */
67	protected function process_post( $post ) {
68		// Some plugins might output data, so let's buffer this to prevent wrong responses.
69		ob_start();
70
71		// Apply the filters to have the same content as shown on the frontend.
72		$content = apply_filters( 'the_content', $post->post_content );
73		$content = str_replace( ']]>', ']]&gt;', $content );
74
75		ob_end_clean();
76
77		$content_processor = $this->get_content_processor();
78		$content_processor->process( $post->ID, $content );
79	}
80
81	/**
82	 * Returns an instance of the content processor.
83	 *
84	 * @return WPSEO_Link_Content_Processor The instance of the link content processor.
85	 */
86	protected function get_content_processor() {
87		static $content_processor;
88
89		if ( $content_processor === null ) {
90			$content_processor = new WPSEO_Link_Content_Processor( new WPSEO_Link_Storage(), new WPSEO_Meta_Storage() );
91		}
92
93		return $content_processor;
94	}
95}