Completed
Push — develop ( 44b932...9593c1 )
by David
05:41 queued 10s
created
src/modules/analyzer/wordlift_analyzer.php 2 patches
Indentation   +52 added lines, -52 removed lines patch added patch discarded remove patch
@@ -10,23 +10,23 @@  discard block
 block discarded – undo
10 10
  */
11 11
 function wl_ajax_analyze_action() {
12 12
 
13
-	try {
14
-		if ( $analysis = wl_analyze_content( file_get_contents( "php://input" ) ) ) {
15
-			header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) );
16
-			echo( $analysis );
17
-			wp_die();
18
-		}
19
-
20
-		status_header( 500 );
21
-		wp_send_json( __( 'An error occurred while request an analysis to the remote service. Please try again later.', 'wordlift' ) );
22
-
23
-	} catch ( Exception $e ) {
24
-		wp_send_json_error( array(
25
-			'code'    => $e->getCode(),
26
-			'message' => $e->getMessage(),
27
-			'trace'   => $e->getTraceAsString()
28
-		) );
29
-	}
13
+    try {
14
+        if ( $analysis = wl_analyze_content( file_get_contents( "php://input" ) ) ) {
15
+            header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) );
16
+            echo( $analysis );
17
+            wp_die();
18
+        }
19
+
20
+        status_header( 500 );
21
+        wp_send_json( __( 'An error occurred while request an analysis to the remote service. Please try again later.', 'wordlift' ) );
22
+
23
+    } catch ( Exception $e ) {
24
+        wp_send_json_error( array(
25
+            'code'    => $e->getCode(),
26
+            'message' => $e->getMessage(),
27
+            'trace'   => $e->getTraceAsString()
28
+        ) );
29
+    }
30 30
 
31 31
 }
32 32
 
@@ -48,51 +48,51 @@  discard block
 block discarded – undo
48 48
  */
49 49
 function wl_analyze_content( $content ) {
50 50
 
51
-	// Get the analyzer URL.
52
-	$url = wl_configuration_get_analyzer_url();
51
+    // Get the analyzer URL.
52
+    $url = wl_configuration_get_analyzer_url();
53 53
 
54
-	// Set the content type to the request content type or to text/plain by default.
55
-	$content_type = isset( $_SERVER['CONTENT_TYPE'] ) ? $_SERVER['CONTENT_TYPE'] : 'text/plain';
54
+    // Set the content type to the request content type or to text/plain by default.
55
+    $content_type = isset( $_SERVER['CONTENT_TYPE'] ) ? $_SERVER['CONTENT_TYPE'] : 'text/plain';
56 56
 
57
-	// Prepare the request.
58
-	$args = array_merge_recursive( unserialize( WL_REDLINK_API_HTTP_OPTIONS ), array(
59
-		'method'      => 'POST',
60
-		'headers'     => array(
61
-			'Accept'       => 'application/json',
62
-			'Content-type' => $content_type,
63
-		),
64
-		// we need to downgrade the HTTP version in this case since chunked encoding is dumping numbers in the response.
65
-		'httpversion' => '1.0',
66
-		'body'        => $content,
67
-	) );
57
+    // Prepare the request.
58
+    $args = array_merge_recursive( unserialize( WL_REDLINK_API_HTTP_OPTIONS ), array(
59
+        'method'      => 'POST',
60
+        'headers'     => array(
61
+            'Accept'       => 'application/json',
62
+            'Content-type' => $content_type,
63
+        ),
64
+        // we need to downgrade the HTTP version in this case since chunked encoding is dumping numbers in the response.
65
+        'httpversion' => '1.0',
66
+        'body'        => $content,
67
+    ) );
68 68
 
69
-	$response = wp_remote_post( $url, $args );
69
+    $response = wp_remote_post( $url, $args );
70 70
 
71
-	// If it's an error log it.
72
-	if ( is_wp_error( $response ) ) {
71
+    // If it's an error log it.
72
+    if ( is_wp_error( $response ) ) {
73 73
 
74
-		$message = "An error occurred while requesting an analysis to $url: {$response->get_error_message()}";
74
+        $message = "An error occurred while requesting an analysis to $url: {$response->get_error_message()}";
75 75
 
76
-		Wordlift_Log_Service::get_logger( 'wl_analyze_content' )->error( $message );
76
+        Wordlift_Log_Service::get_logger( 'wl_analyze_content' )->error( $message );
77 77
 
78
-		throw new Exception( $response->get_error_message(), is_numeric( $response->get_error_code() ) ? $response->get_error_code() : - 1 );
79
-	}
78
+        throw new Exception( $response->get_error_message(), is_numeric( $response->get_error_code() ) ? $response->get_error_code() : - 1 );
79
+    }
80 80
 
81
-	// Get the status code.
82
-	$status_code = (int) $response['response']['code'];
81
+    // Get the status code.
82
+    $status_code = (int) $response['response']['code'];
83 83
 
84
-	// If status code is OK, return the body.
85
-	if ( 200 === $status_code ) {
86
-		return $response['body'];
87
-	}
84
+    // If status code is OK, return the body.
85
+    if ( 200 === $status_code ) {
86
+        return $response['body'];
87
+    }
88 88
 
89
-	// Invalid request, e.g. invalid key.
90
-	if ( 400 === $status_code ) {
91
-		$error = json_decode( $response['body'] );
89
+    // Invalid request, e.g. invalid key.
90
+    if ( 400 === $status_code ) {
91
+        $error = json_decode( $response['body'] );
92 92
 
93
-		throw new Exception( $error->message, $error->code );
94
-	}
93
+        throw new Exception( $error->message, $error->code );
94
+    }
95 95
 
96
-	// Another generic error.
97
-	throw new Exception( "An error occurred.", $status_code );;
96
+    // Another generic error.
97
+    throw new Exception( "An error occurred.", $status_code );;
98 98
 }
Please login to merge, or discard this patch.
Spacing   +22 added lines, -22 removed lines patch added patch discarded remove patch
@@ -11,26 +11,26 @@  discard block
 block discarded – undo
11 11
 function wl_ajax_analyze_action() {
12 12
 
13 13
 	try {
14
-		if ( $analysis = wl_analyze_content( file_get_contents( "php://input" ) ) ) {
15
-			header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) );
16
-			echo( $analysis );
14
+		if ($analysis = wl_analyze_content(file_get_contents("php://input"))) {
15
+			header('Content-Type: application/json; charset='.get_option('blog_charset'));
16
+			echo($analysis);
17 17
 			wp_die();
18 18
 		}
19 19
 
20
-		status_header( 500 );
21
-		wp_send_json( __( 'An error occurred while request an analysis to the remote service. Please try again later.', 'wordlift' ) );
20
+		status_header(500);
21
+		wp_send_json(__('An error occurred while request an analysis to the remote service. Please try again later.', 'wordlift'));
22 22
 
23
-	} catch ( Exception $e ) {
24
-		wp_send_json_error( array(
23
+	} catch (Exception $e) {
24
+		wp_send_json_error(array(
25 25
 			'code'    => $e->getCode(),
26 26
 			'message' => $e->getMessage(),
27 27
 			'trace'   => $e->getTraceAsString()
28
-		) );
28
+		));
29 29
 	}
30 30
 
31 31
 }
32 32
 
33
-add_action( 'wp_ajax_wordlift_analyze', 'wl_ajax_analyze_action' );
33
+add_action('wp_ajax_wordlift_analyze', 'wl_ajax_analyze_action');
34 34
 
35 35
 /**
36 36
  * Analyze the provided content. The analysis will make use of the method *wl_ajax_analyze_action*
@@ -46,16 +46,16 @@  discard block
 block discarded – undo
46 46
  *
47 47
  * @throws Exception
48 48
  */
49
-function wl_analyze_content( $content ) {
49
+function wl_analyze_content($content) {
50 50
 
51 51
 	// Get the analyzer URL.
52 52
 	$url = wl_configuration_get_analyzer_url();
53 53
 
54 54
 	// Set the content type to the request content type or to text/plain by default.
55
-	$content_type = isset( $_SERVER['CONTENT_TYPE'] ) ? $_SERVER['CONTENT_TYPE'] : 'text/plain';
55
+	$content_type = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : 'text/plain';
56 56
 
57 57
 	// Prepare the request.
58
-	$args = array_merge_recursive( unserialize( WL_REDLINK_API_HTTP_OPTIONS ), array(
58
+	$args = array_merge_recursive(unserialize(WL_REDLINK_API_HTTP_OPTIONS), array(
59 59
 		'method'      => 'POST',
60 60
 		'headers'     => array(
61 61
 			'Accept'       => 'application/json',
@@ -64,35 +64,35 @@  discard block
 block discarded – undo
64 64
 		// we need to downgrade the HTTP version in this case since chunked encoding is dumping numbers in the response.
65 65
 		'httpversion' => '1.0',
66 66
 		'body'        => $content,
67
-	) );
67
+	));
68 68
 
69
-	$response = wp_remote_post( $url, $args );
69
+	$response = wp_remote_post($url, $args);
70 70
 
71 71
 	// If it's an error log it.
72
-	if ( is_wp_error( $response ) ) {
72
+	if (is_wp_error($response)) {
73 73
 
74 74
 		$message = "An error occurred while requesting an analysis to $url: {$response->get_error_message()}";
75 75
 
76
-		Wordlift_Log_Service::get_logger( 'wl_analyze_content' )->error( $message );
76
+		Wordlift_Log_Service::get_logger('wl_analyze_content')->error($message);
77 77
 
78
-		throw new Exception( $response->get_error_message(), is_numeric( $response->get_error_code() ) ? $response->get_error_code() : - 1 );
78
+		throw new Exception($response->get_error_message(), is_numeric($response->get_error_code()) ? $response->get_error_code() : - 1);
79 79
 	}
80 80
 
81 81
 	// Get the status code.
82 82
 	$status_code = (int) $response['response']['code'];
83 83
 
84 84
 	// If status code is OK, return the body.
85
-	if ( 200 === $status_code ) {
85
+	if (200 === $status_code) {
86 86
 		return $response['body'];
87 87
 	}
88 88
 
89 89
 	// Invalid request, e.g. invalid key.
90
-	if ( 400 === $status_code ) {
91
-		$error = json_decode( $response['body'] );
90
+	if (400 === $status_code) {
91
+		$error = json_decode($response['body']);
92 92
 
93
-		throw new Exception( $error->message, $error->code );
93
+		throw new Exception($error->message, $error->code);
94 94
 	}
95 95
 
96 96
 	// Another generic error.
97
-	throw new Exception( "An error occurred.", $status_code );;
97
+	throw new Exception("An error occurred.", $status_code); ;
98 98
 }
Please login to merge, or discard this patch.
src/wordlift_entity_type.php 2 patches
Indentation   +29 added lines, -29 removed lines patch added patch discarded remove patch
@@ -10,8 +10,8 @@  discard block
 block discarded – undo
10 10
  */
11 11
 function wl_set_entity_main_type( $post_id, $type_uri ) {
12 12
 
13
-	Wordlift_Entity_Type_Service::get_instance()
14
-	                            ->set( $post_id, $type_uri );
13
+    Wordlift_Entity_Type_Service::get_instance()
14
+                                ->set( $post_id, $type_uri );
15 15
 
16 16
 }
17 17
 
@@ -20,48 +20,48 @@  discard block
 block discarded – undo
20 20
  */
21 21
 function wl_print_entity_type_inline_js() {
22 22
 
23
-	$terms = get_terms( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array(
24
-		'hide_empty' => false,
25
-	) );
23
+    $terms = get_terms( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array(
24
+        'hide_empty' => false,
25
+    ) );
26 26
 
27
-	echo <<<EOF
27
+    echo <<<EOF
28 28
     <script type="text/javascript">
29 29
         (function() {
30 30
         var t = [];
31 31
 
32 32
 EOF;
33 33
 
34
-	// Cycle in each WordLift term and get its metadata. The metadata will be printed as a global object in JavaScript
35
-	// to be used by the JavaScript client library.
36
-	foreach ( $terms as $term ) {
34
+    // Cycle in each WordLift term and get its metadata. The metadata will be printed as a global object in JavaScript
35
+    // to be used by the JavaScript client library.
36
+    foreach ( $terms as $term ) {
37 37
 
38
-		$term_name = $term->name;
38
+        $term_name = $term->name;
39 39
 
40
-		// Load the type data.
41
-		$type = Wordlift_Schema_Service::get_instance()
42
-		                               ->get_schema( $term->slug );
40
+        // Load the type data.
41
+        $type = Wordlift_Schema_Service::get_instance()
42
+                                        ->get_schema( $term->slug );
43 43
 
44
-		// Skip types that are not defined.
45
-		if ( ! empty( $type['uri'] ) ) {
44
+        // Skip types that are not defined.
45
+        if ( ! empty( $type['uri'] ) ) {
46 46
 
47
-			// Prepare the JSON output then print it to the browser.
48
-			$json = json_encode( array(
49
-				'label'     => $term_name,
50
-				'uri'       => $type['uri'],
51
-				'css'       => $type['css_class'],
52
-				'sameAs'    => $type['same_as'],
53
-				'slug'      => $term->slug,
54
-				'templates' => ( isset( $type['templates'] ) ? $type['templates'] : array() ),
55
-			) );
47
+            // Prepare the JSON output then print it to the browser.
48
+            $json = json_encode( array(
49
+                'label'     => $term_name,
50
+                'uri'       => $type['uri'],
51
+                'css'       => $type['css_class'],
52
+                'sameAs'    => $type['same_as'],
53
+                'slug'      => $term->slug,
54
+                'templates' => ( isset( $type['templates'] ) ? $type['templates'] : array() ),
55
+            ) );
56 56
 
57
-			// Output the type data.
58
-			echo "t.push($json);\n";
57
+            // Output the type data.
58
+            echo "t.push($json);\n";
59 59
 
60
-		}
60
+        }
61 61
 
62
-	}
62
+    }
63 63
 
64
-	echo <<<EOF
64
+    echo <<<EOF
65 65
             if ('undefined' == typeof window.wordlift) {
66 66
                 window.wordlift = {}
67 67
             }
Please login to merge, or discard this patch.
Spacing   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -8,10 +8,10 @@  discard block
 block discarded – undo
8 8
  * @param int $post_id The numeric post ID.
9 9
  * @param string $type_uri A type URI.
10 10
  */
11
-function wl_set_entity_main_type( $post_id, $type_uri ) {
11
+function wl_set_entity_main_type($post_id, $type_uri) {
12 12
 
13 13
 	Wordlift_Entity_Type_Service::get_instance()
14
-	                            ->set( $post_id, $type_uri );
14
+	                            ->set($post_id, $type_uri);
15 15
 
16 16
 }
17 17
 
@@ -20,9 +20,9 @@  discard block
 block discarded – undo
20 20
  */
21 21
 function wl_print_entity_type_inline_js() {
22 22
 
23
-	$terms = get_terms( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array(
23
+	$terms = get_terms(Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array(
24 24
 		'hide_empty' => false,
25
-	) );
25
+	));
26 26
 
27 27
 	echo <<<EOF
28 28
     <script type="text/javascript">
@@ -33,26 +33,26 @@  discard block
 block discarded – undo
33 33
 
34 34
 	// Cycle in each WordLift term and get its metadata. The metadata will be printed as a global object in JavaScript
35 35
 	// to be used by the JavaScript client library.
36
-	foreach ( $terms as $term ) {
36
+	foreach ($terms as $term) {
37 37
 
38 38
 		$term_name = $term->name;
39 39
 
40 40
 		// Load the type data.
41 41
 		$type = Wordlift_Schema_Service::get_instance()
42
-		                               ->get_schema( $term->slug );
42
+		                               ->get_schema($term->slug);
43 43
 
44 44
 		// Skip types that are not defined.
45
-		if ( ! empty( $type['uri'] ) ) {
45
+		if ( ! empty($type['uri'])) {
46 46
 
47 47
 			// Prepare the JSON output then print it to the browser.
48
-			$json = json_encode( array(
48
+			$json = json_encode(array(
49 49
 				'label'     => $term_name,
50 50
 				'uri'       => $type['uri'],
51 51
 				'css'       => $type['css_class'],
52 52
 				'sameAs'    => $type['same_as'],
53 53
 				'slug'      => $term->slug,
54
-				'templates' => ( isset( $type['templates'] ) ? $type['templates'] : array() ),
55
-			) );
54
+				'templates' => (isset($type['templates']) ? $type['templates'] : array()),
55
+			));
56 56
 
57 57
 			// Output the type data.
58 58
 			echo "t.push($json);\n";
@@ -73,4 +73,4 @@  discard block
 block discarded – undo
73 73
 
74 74
 }
75 75
 
76
-add_action( 'admin_print_scripts', 'wl_print_entity_type_inline_js' );
76
+add_action('admin_print_scripts', 'wl_print_entity_type_inline_js');
Please login to merge, or discard this patch.
src/includes/class-wordlift-autocomplete-adapter.php 2 patches
Indentation   +68 added lines, -68 removed lines patch added patch discarded remove patch
@@ -11,7 +11,7 @@  discard block
 block discarded – undo
11 11
  */
12 12
 
13 13
 if ( ! defined( 'ABSPATH' ) ) {
14
-	exit;
14
+    exit;
15 15
 }
16 16
 
17 17
 /**
@@ -21,82 +21,82 @@  discard block
 block discarded – undo
21 21
  */
22 22
 class Wordlift_Autocomplete_Adapter {
23 23
 
24
-	/**
25
-	 * The {@link Wordlift_Autocomplete_Service} instance.
26
-	 *
27
-	 * @since  3.15.0
28
-	 * @access private
29
-	 * @var \Wordlift_Autocomplete_Service $configuration_service The {@link Wordlift_Autocomplete_Service} instance.
30
-	 */
31
-	private $autocomplete_service;
24
+    /**
25
+     * The {@link Wordlift_Autocomplete_Service} instance.
26
+     *
27
+     * @since  3.15.0
28
+     * @access private
29
+     * @var \Wordlift_Autocomplete_Service $configuration_service The {@link Wordlift_Autocomplete_Service} instance.
30
+     */
31
+    private $autocomplete_service;
32 32
 
33 33
 
34
-	/**
35
-	 * Wordlift_Autocomplete_Adapter constructor.
36
-	 *
37
-	 * @since 3.14.2
38
-	 *
39
-	 * @param \Wordlift_Autocomplete_Service $autocomplete_service The {@link Wordlift_Autocomplete_Service} instance.
40
-	 */
41
-	public function __construct( $autocomplete_service ) {
42
-		$this->autocomplete_service = $autocomplete_service;
43
-	}
34
+    /**
35
+     * Wordlift_Autocomplete_Adapter constructor.
36
+     *
37
+     * @since 3.14.2
38
+     *
39
+     * @param \Wordlift_Autocomplete_Service $autocomplete_service The {@link Wordlift_Autocomplete_Service} instance.
40
+     */
41
+    public function __construct( $autocomplete_service ) {
42
+        $this->autocomplete_service = $autocomplete_service;
43
+    }
44 44
 
45
-	/**
46
-	 * Handle the autocomplete ajax request.
47
-	 *
48
-	 * @since 3.15.0
49
-	 */
50
-	public function wl_autocomplete() {
51
-		if (
52
-			! isset( $_REQUEST['_wpnonce'] ) || // Input var okay.
53
-			! wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'wordlift_autocomplete' ) // Input var okay.
54
-		) {
55
-			wp_send_json_error( array(
56
-				'message' => __( 'Nonce field doesn\'t match.', 'wordlift' ),
57
-			) );
58
-		}
45
+    /**
46
+     * Handle the autocomplete ajax request.
47
+     *
48
+     * @since 3.15.0
49
+     */
50
+    public function wl_autocomplete() {
51
+        if (
52
+            ! isset( $_REQUEST['_wpnonce'] ) || // Input var okay.
53
+            ! wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'wordlift_autocomplete' ) // Input var okay.
54
+        ) {
55
+            wp_send_json_error( array(
56
+                'message' => __( 'Nonce field doesn\'t match.', 'wordlift' ),
57
+            ) );
58
+        }
59 59
 
60
-		// Return error if the query param is empty.
61
-		if ( ! empty( $_REQUEST['query'] ) ) { // Input var okay.
62
-			$query = sanitize_text_field( wp_unslash( $_REQUEST['query'] ) ); // Input var okay.
63
-		} else {
64
-			wp_send_json_error( array(
65
-				'message' => __( 'The query param is empty.', 'wordlift' ),
66
-			) );
67
-		}
60
+        // Return error if the query param is empty.
61
+        if ( ! empty( $_REQUEST['query'] ) ) { // Input var okay.
62
+            $query = sanitize_text_field( wp_unslash( $_REQUEST['query'] ) ); // Input var okay.
63
+        } else {
64
+            wp_send_json_error( array(
65
+                'message' => __( 'The query param is empty.', 'wordlift' ),
66
+            ) );
67
+        }
68 68
 
69
-		// Get the exclude parameter.
70
-		$exclude = ! empty( $_REQUEST['exclude'] )
71
-			? sanitize_text_field( wp_unslash( $_REQUEST['exclude'] ) ) : '';
69
+        // Get the exclude parameter.
70
+        $exclude = ! empty( $_REQUEST['exclude'] )
71
+            ? sanitize_text_field( wp_unslash( $_REQUEST['exclude'] ) ) : '';
72 72
 
73
-		$scope = ! empty( $_REQUEST['scope'] )
74
-			? sanitize_text_field( wp_unslash( $_REQUEST['scope'] ) ) : '';
73
+        $scope = ! empty( $_REQUEST['scope'] )
74
+            ? sanitize_text_field( wp_unslash( $_REQUEST['scope'] ) ) : '';
75 75
 
76
-		// Make request.
77
-		$response = $this->autocomplete_service->make_request( $query, $exclude, $scope );
76
+        // Make request.
77
+        $response = $this->autocomplete_service->make_request( $query, $exclude, $scope );
78 78
 
79
-		// Clear any buffer.
80
-		ob_clean();
79
+        // Clear any buffer.
80
+        ob_clean();
81 81
 
82
-		// If the response is valid, then send the suggestions.
83
-		if ( ! is_wp_error( $response ) && 200 === (int) $response['response']['code'] ) {
84
-			// Echo the response.
85
-			wp_send_json_success( json_decode( wp_remote_retrieve_body( $response ), true ) );
86
-		} else {
87
-			// Default error message.
88
-			$error_message = 'Something went wrong.';
82
+        // If the response is valid, then send the suggestions.
83
+        if ( ! is_wp_error( $response ) && 200 === (int) $response['response']['code'] ) {
84
+            // Echo the response.
85
+            wp_send_json_success( json_decode( wp_remote_retrieve_body( $response ), true ) );
86
+        } else {
87
+            // Default error message.
88
+            $error_message = 'Something went wrong.';
89 89
 
90
-			// Get the real error message if there is WP_Error.
91
-			if ( is_wp_error( $response ) ) {
92
-				$error_message = $response->get_error_message();
93
-			}
90
+            // Get the real error message if there is WP_Error.
91
+            if ( is_wp_error( $response ) ) {
92
+                $error_message = $response->get_error_message();
93
+            }
94 94
 
95
-			// There is an error, so send error message.
96
-			wp_send_json_error( array(
97
-				/* translators: Placeholders: %s - the error message that will be returned. */
98
-				'message' => sprintf( esc_html__( 'Error: %s', 'wordlift' ), $error_message ),
99
-			) );
100
-		}
101
-	}
95
+            // There is an error, so send error message.
96
+            wp_send_json_error( array(
97
+                /* translators: Placeholders: %s - the error message that will be returned. */
98
+                'message' => sprintf( esc_html__( 'Error: %s', 'wordlift' ), $error_message ),
99
+            ) );
100
+        }
101
+    }
102 102
 }
Please login to merge, or discard this patch.
Spacing   +23 added lines, -23 removed lines patch added patch discarded remove patch
@@ -10,7 +10,7 @@  discard block
 block discarded – undo
10 10
  * @since      3.15.0
11 11
  */
12 12
 
13
-if ( ! defined( 'ABSPATH' ) ) {
13
+if ( ! defined('ABSPATH')) {
14 14
 	exit;
15 15
 }
16 16
 
@@ -38,7 +38,7 @@  discard block
 block discarded – undo
38 38
 	 *
39 39
 	 * @param \Wordlift_Autocomplete_Service $autocomplete_service The {@link Wordlift_Autocomplete_Service} instance.
40 40
 	 */
41
-	public function __construct( $autocomplete_service ) {
41
+	public function __construct($autocomplete_service) {
42 42
 		$this->autocomplete_service = $autocomplete_service;
43 43
 	}
44 44
 
@@ -49,54 +49,54 @@  discard block
 block discarded – undo
49 49
 	 */
50 50
 	public function wl_autocomplete() {
51 51
 		if (
52
-			! isset( $_REQUEST['_wpnonce'] ) || // Input var okay.
53
-			! wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'wordlift_autocomplete' ) // Input var okay.
52
+			! isset($_REQUEST['_wpnonce']) || // Input var okay.
53
+			! wp_verify_nonce(sanitize_key($_REQUEST['_wpnonce']), 'wordlift_autocomplete') // Input var okay.
54 54
 		) {
55
-			wp_send_json_error( array(
56
-				'message' => __( 'Nonce field doesn\'t match.', 'wordlift' ),
57
-			) );
55
+			wp_send_json_error(array(
56
+				'message' => __('Nonce field doesn\'t match.', 'wordlift'),
57
+			));
58 58
 		}
59 59
 
60 60
 		// Return error if the query param is empty.
61
-		if ( ! empty( $_REQUEST['query'] ) ) { // Input var okay.
62
-			$query = sanitize_text_field( wp_unslash( $_REQUEST['query'] ) ); // Input var okay.
61
+		if ( ! empty($_REQUEST['query'])) { // Input var okay.
62
+			$query = sanitize_text_field(wp_unslash($_REQUEST['query'])); // Input var okay.
63 63
 		} else {
64
-			wp_send_json_error( array(
65
-				'message' => __( 'The query param is empty.', 'wordlift' ),
66
-			) );
64
+			wp_send_json_error(array(
65
+				'message' => __('The query param is empty.', 'wordlift'),
66
+			));
67 67
 		}
68 68
 
69 69
 		// Get the exclude parameter.
70
-		$exclude = ! empty( $_REQUEST['exclude'] )
71
-			? sanitize_text_field( wp_unslash( $_REQUEST['exclude'] ) ) : '';
70
+		$exclude = ! empty($_REQUEST['exclude'])
71
+			? sanitize_text_field(wp_unslash($_REQUEST['exclude'])) : '';
72 72
 
73
-		$scope = ! empty( $_REQUEST['scope'] )
74
-			? sanitize_text_field( wp_unslash( $_REQUEST['scope'] ) ) : '';
73
+		$scope = ! empty($_REQUEST['scope'])
74
+			? sanitize_text_field(wp_unslash($_REQUEST['scope'])) : '';
75 75
 
76 76
 		// Make request.
77
-		$response = $this->autocomplete_service->make_request( $query, $exclude, $scope );
77
+		$response = $this->autocomplete_service->make_request($query, $exclude, $scope);
78 78
 
79 79
 		// Clear any buffer.
80 80
 		ob_clean();
81 81
 
82 82
 		// If the response is valid, then send the suggestions.
83
-		if ( ! is_wp_error( $response ) && 200 === (int) $response['response']['code'] ) {
83
+		if ( ! is_wp_error($response) && 200 === (int) $response['response']['code']) {
84 84
 			// Echo the response.
85
-			wp_send_json_success( json_decode( wp_remote_retrieve_body( $response ), true ) );
85
+			wp_send_json_success(json_decode(wp_remote_retrieve_body($response), true));
86 86
 		} else {
87 87
 			// Default error message.
88 88
 			$error_message = 'Something went wrong.';
89 89
 
90 90
 			// Get the real error message if there is WP_Error.
91
-			if ( is_wp_error( $response ) ) {
91
+			if (is_wp_error($response)) {
92 92
 				$error_message = $response->get_error_message();
93 93
 			}
94 94
 
95 95
 			// There is an error, so send error message.
96
-			wp_send_json_error( array(
96
+			wp_send_json_error(array(
97 97
 				/* translators: Placeholders: %s - the error message that will be returned. */
98
-				'message' => sprintf( esc_html__( 'Error: %s', 'wordlift' ), $error_message ),
99
-			) );
98
+				'message' => sprintf(esc_html__('Error: %s', 'wordlift'), $error_message),
99
+			));
100 100
 		}
101 101
 	}
102 102
 }
Please login to merge, or discard this patch.
src/includes/class-wordlift-autocomplete-service.php 2 patches
Indentation   +80 added lines, -81 removed lines patch added patch discarded remove patch
@@ -11,7 +11,7 @@  discard block
 block discarded – undo
11 11
  */
12 12
 
13 13
 if ( ! defined( 'ABSPATH' ) ) {
14
-	exit;
14
+    exit;
15 15
 }
16 16
 
17 17
 /**
@@ -20,93 +20,92 @@  discard block
 block discarded – undo
20 20
  * @since 3.15.0
21 21
  */
22 22
 class Wordlift_Autocomplete_Service {
23
-	/**
24
-	 * The {@link Wordlift_Configuration_Service} instance.
25
-	 *
26
-	 * @since  3.15.0
27
-	 * @access private
28
-	 * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
29
-	 */
30
-	private $configuration_service;
23
+    /**
24
+     * The {@link Wordlift_Configuration_Service} instance.
25
+     *
26
+     * @since  3.15.0
27
+     * @access private
28
+     * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
29
+     */
30
+    private $configuration_service;
31 31
 
32
-	/**
33
-	 * A {@link Wordlift_Log_Service} instance.
34
-	 *
35
-	 * @since  3.15.0
36
-	 * @access private
37
-	 * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
38
-	 */
39
-	private $log;
32
+    /**
33
+     * A {@link Wordlift_Log_Service} instance.
34
+     *
35
+     * @since  3.15.0
36
+     * @access private
37
+     * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
38
+     */
39
+    private $log;
40 40
 
41
-	/**
42
-	 * The {@link Class_Wordlift_Autocomplete_Service} instance.
43
-	 *
44
-	 * @since 3.15.0
45
-	 *
46
-	 * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
47
-	 */
48
-	public function __construct( $configuration_service ) {
49
-		$this->configuration_service = $configuration_service;
50
-		$this->log                   = Wordlift_Log_Service::get_logger( 'Wordlift_Autocomplete_Service' );
51
-	}
41
+    /**
42
+     * The {@link Class_Wordlift_Autocomplete_Service} instance.
43
+     *
44
+     * @since 3.15.0
45
+     *
46
+     * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
47
+     */
48
+    public function __construct( $configuration_service ) {
49
+        $this->configuration_service = $configuration_service;
50
+        $this->log                   = Wordlift_Log_Service::get_logger( 'Wordlift_Autocomplete_Service' );
51
+    }
52 52
 
53
-	/**
54
-	 * Make request to external API and return the response.
55
-	 *
56
-	 * @since 3.15.0
57
-	 *
58
-	 * @param string $query The search string.
59
-	 * @param array|string $exclude The exclude parameter string.
60
-	 * @param string $scope The search scope: "local" will search only in the local dataset; "cloud" will search also
61
-	 *                      in Wikipedia. By default is "cloud".
62
-	 *
63
-	 * @return array $response The API response.
64
-	 */
65
-	public function make_request( $query, $exclude = '', $scope = 'cloud' ) {
66
-		$url = $this->build_request_url( $query, $exclude, $scope );
53
+    /**
54
+     * Make request to external API and return the response.
55
+     *
56
+     * @since 3.15.0
57
+     *
58
+     * @param string $query The search string.
59
+     * @param array|string $exclude The exclude parameter string.
60
+     * @param string $scope The search scope: "local" will search only in the local dataset; "cloud" will search also
61
+     *                      in Wikipedia. By default is "cloud".
62
+     *
63
+     * @return array $response The API response.
64
+     */
65
+    public function make_request( $query, $exclude = '', $scope = 'cloud' ) {
66
+        $url = $this->build_request_url( $query, $exclude, $scope );
67 67
 
68
-		// Make request.
69
-		$response = wp_remote_get( $url );
68
+        // Make request.
69
+        $response = wp_remote_get( $url );
70 70
 
71
-		// Return the response.
72
-		return $response;
73
-	}
71
+        // Return the response.
72
+        return $response;
73
+    }
74 74
 
75
-	/**
76
-	 * Build the autocomplete url.
77
-	 *
78
-	 * @since 3.15.0
79
-	 *
80
-	 * @param string $query The search string.
81
-	 * @param array|string $exclude The exclude parameter.
82
-	 * @param string $scope The search scope: "local" will search only in the local dataset; "cloud" will search also
83
-	 *                      in Wikipedia. By default is "cloud".
75
+    /**
76
+     * Build the autocomplete url.
77
+     *
78
+     * @since 3.15.0
79
+     *
80
+     * @param string $query The search string.
81
+     * @param array|string $exclude The exclude parameter.
82
+     * @param string $scope The search scope: "local" will search only in the local dataset; "cloud" will search also
83
+     *                      in Wikipedia. By default is "cloud".
84
+     * @return string Built url.
85
+     */
86
+    private function build_request_url( $query, $exclude, $scope ) {
87
+        $args = array(
88
+            'key'      => $this->configuration_service->get_key(),
89
+            'language' => $this->configuration_service->get_language_code(),
90
+            'query'    => $query,
91
+            'scope'    => $scope,
92
+            'limit'    => 100,
93
+        );
84 94
 
85
-	 * @return string Built url.
86
-	 */
87
-	private function build_request_url( $query, $exclude, $scope ) {
88
-		$args = array(
89
-			'key'      => $this->configuration_service->get_key(),
90
-			'language' => $this->configuration_service->get_language_code(),
91
-			'query'    => $query,
92
-			'scope'    => $scope,
93
-			'limit'    => 100,
94
-		);
95
+        // Add args to URL.
96
+        $request_url = add_query_arg(
97
+            urlencode_deep( $args ),
98
+            $this->configuration_service->get_autocomplete_url()
99
+        );
95 100
 
96
-		// Add args to URL.
97
-		$request_url = add_query_arg(
98
-			urlencode_deep( $args ),
99
-			$this->configuration_service->get_autocomplete_url()
100
-		);
101
+        // Add the exclude parameter.
102
+        if ( ! empty( $exclude ) ) {
103
+            foreach ( (array) $exclude as $item ) {
104
+                $request_url .= "&exclude=" . urlencode( $item );
105
+            }
106
+        }
101 107
 
102
-		// Add the exclude parameter.
103
-		if ( ! empty( $exclude ) ) {
104
-			foreach ( (array) $exclude as $item ) {
105
-				$request_url .= "&exclude=" . urlencode( $item );
106
-			}
107
-		}
108
-
109
-		// return the built url.
110
-		return $request_url;
111
-	}
108
+        // return the built url.
109
+        return $request_url;
110
+    }
112 111
 }
Please login to merge, or discard this patch.
Spacing   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -10,7 +10,7 @@  discard block
 block discarded – undo
10 10
  * @since      3.15.0
11 11
  */
12 12
 
13
-if ( ! defined( 'ABSPATH' ) ) {
13
+if ( ! defined('ABSPATH')) {
14 14
 	exit;
15 15
 }
16 16
 
@@ -45,9 +45,9 @@  discard block
 block discarded – undo
45 45
 	 *
46 46
 	 * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
47 47
 	 */
48
-	public function __construct( $configuration_service ) {
48
+	public function __construct($configuration_service) {
49 49
 		$this->configuration_service = $configuration_service;
50
-		$this->log                   = Wordlift_Log_Service::get_logger( 'Wordlift_Autocomplete_Service' );
50
+		$this->log                   = Wordlift_Log_Service::get_logger('Wordlift_Autocomplete_Service');
51 51
 	}
52 52
 
53 53
 	/**
@@ -62,11 +62,11 @@  discard block
 block discarded – undo
62 62
 	 *
63 63
 	 * @return array $response The API response.
64 64
 	 */
65
-	public function make_request( $query, $exclude = '', $scope = 'cloud' ) {
66
-		$url = $this->build_request_url( $query, $exclude, $scope );
65
+	public function make_request($query, $exclude = '', $scope = 'cloud') {
66
+		$url = $this->build_request_url($query, $exclude, $scope);
67 67
 
68 68
 		// Make request.
69
-		$response = wp_remote_get( $url );
69
+		$response = wp_remote_get($url);
70 70
 
71 71
 		// Return the response.
72 72
 		return $response;
@@ -84,7 +84,7 @@  discard block
 block discarded – undo
84 84
 
85 85
 	 * @return string Built url.
86 86
 	 */
87
-	private function build_request_url( $query, $exclude, $scope ) {
87
+	private function build_request_url($query, $exclude, $scope) {
88 88
 		$args = array(
89 89
 			'key'      => $this->configuration_service->get_key(),
90 90
 			'language' => $this->configuration_service->get_language_code(),
@@ -95,14 +95,14 @@  discard block
 block discarded – undo
95 95
 
96 96
 		// Add args to URL.
97 97
 		$request_url = add_query_arg(
98
-			urlencode_deep( $args ),
98
+			urlencode_deep($args),
99 99
 			$this->configuration_service->get_autocomplete_url()
100 100
 		);
101 101
 
102 102
 		// Add the exclude parameter.
103
-		if ( ! empty( $exclude ) ) {
104
-			foreach ( (array) $exclude as $item ) {
105
-				$request_url .= "&exclude=" . urlencode( $item );
103
+		if ( ! empty($exclude)) {
104
+			foreach ((array) $exclude as $item) {
105
+				$request_url .= "&exclude=".urlencode($item);
106 106
 			}
107 107
 		}
108 108
 
Please login to merge, or discard this patch.
src/includes/class-wordlift-abstract-post-to-jsonld-converter.php 2 patches
Indentation   +229 added lines, -229 removed lines patch added patch discarded remove patch
@@ -18,237 +18,237 @@
 block discarded – undo
18 18
  */
19 19
 abstract class Wordlift_Abstract_Post_To_Jsonld_Converter implements Wordlift_Post_Converter {
20 20
 
21
-	/**
22
-	 * The JSON-LD context.
23
-	 *
24
-	 * @since 3.10.0
25
-	 */
26
-	const CONTEXT = 'http://schema.org';
27
-
28
-	/**
29
-	 * A {@link Wordlift_Entity_Type_Service} instance.
30
-	 *
31
-	 * @since  3.10.0
32
-	 * @access protected
33
-	 * @var \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance.
34
-	 */
35
-	protected $entity_type_service;
36
-
37
-	/**
38
-	 * A {@link Wordlift_Entity_Service} instance.
39
-	 *
40
-	 * @since  3.10.0
41
-	 * @access protected
42
-	 * @var \Wordlift_Entity_Service $entity_type_service A {@link Wordlift_Entity_Service} instance.
43
-	 */
44
-	protected $entity_service;
45
-
46
-	/**
47
-	 * A {@link Wordlift_User_Service} instance.
48
-	 *
49
-	 * @since  3.10.0
50
-	 * @access private
51
-	 * @var \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance.
52
-	 */
53
-	protected $user_service;
54
-
55
-	/**
56
-	 * A {@link Wordlift_Attachment_Service} instance.
57
-	 *
58
-	 * @since  3.10.0
59
-	 * @access private
60
-	 * @var \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance.
61
-	 */
62
-	protected $attachment_service;
63
-
64
-	/**
65
-	 * Wordlift_Post_To_Jsonld_Converter constructor.
66
-	 *
67
-	 * @since 3.10.0
68
-	 *
69
-	 * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance.
70
-	 * @param \Wordlift_Entity_Service      $entity_service      A {@link Wordlift_Entity_Service} instance.
71
-	 * @param \Wordlift_User_Service        $user_service        A {@link Wordlift_User_Service} instance.
72
-	 * @param \Wordlift_Attachment_Service  $attachment_service  A {@link Wordlift_Attachment_Service} instance.
73
-	 */
74
-	public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service ) {
75
-
76
-		$this->entity_type_service = $entity_type_service;
77
-		$this->entity_service      = $entity_service;
78
-		$this->user_service        = $user_service;
79
-		$this->attachment_service  = $attachment_service;
80
-	}
81
-
82
-	/**
83
-	 * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference
84
-	 * found while processing the post is set in the $references array.
85
-	 *
86
-	 * @since 3.10.0
87
-	 *
88
-	 * @param int   $post_id    The post id.
89
-	 * @param array $references An array of entity references.
90
-	 *
91
-	 * @return array A JSON-LD array.
92
-	 */
93
-	public function convert( $post_id, &$references = array() ) {
94
-
95
-		// Get the post instance.
96
-		$post = get_post( $post_id );
97
-		if ( null === $post ) {
98
-			// Post not found.
99
-			return null;
100
-		}
101
-
102
-		// Get the post URI @id.
103
-		$id = $this->entity_service->get_uri( $post->ID );
104
-
105
-		// Get the entity @type. We consider `post` BlogPostings.
106
-		$type = $this->entity_type_service->get( $post_id );
107
-
108
-		// Prepare the response.
109
-		$jsonld = array(
110
-			'@context'    => self::CONTEXT,
111
-			'@id'         => $id,
112
-			'@type'       => $this->relative_to_context( $type['uri'] ),
113
-			'description' => Wordlift_Post_Excerpt_Helper::get_text_excerpt( $post ),
114
-		);
115
-
116
-		// Set the `mainEntityOfPage` property if the post has some contents.
117
-		if ( ! empty( $post->post_content ) ) {
118
-			// We're setting the `mainEntityOfPage` to signal which one is the
119
-			// main entity for the specified URL. It might be however that the
120
-			// post/page is actually about another specific entity. How WL deals
121
-			// with that hasn't been defined yet (see https://github.com/insideout10/wordlift-plugin/issues/451).
122
-			//
123
-			// See http://schema.org/mainEntityOfPage
124
-			//
125
-			// No need to specify `'@type' => 'WebPage'.
126
-			//
127
-			// See https://github.com/insideout10/wordlift-plugin/issues/451.
128
-			$jsonld['mainEntityOfPage'] = get_the_permalink( $post->ID );
129
-		};
130
-
131
-		$this->set_images( $post, $jsonld );
132
-
133
-		// Get the entities referenced by this post and set it to the `references`
134
-		// array so that the caller can do further processing, such as printing out
135
-		// more of those references.
136
-		$references = $this->entity_service->get_related_entities( $post->ID );
137
-
138
-		return $jsonld;
139
-	}
140
-
141
-	/**
142
-	 * If the provided value starts with the schema.org context, we remove the schema.org
143
-	 * part since it is set with the '@context'.
144
-	 *
145
-	 * @since 3.10.0
146
-	 *
147
-	 * @param string $value The property value.
148
-	 *
149
-	 * @return string The property value without the context.
150
-	 */
151
-	public function relative_to_context( $value ) {
152
-
153
-		return 0 === strpos( $value, self::CONTEXT . '/' ) ? substr( $value, strlen( self::CONTEXT ) + 1 ) : $value;
154
-	}
155
-
156
-	/**
157
-	 * Set the images, by looking for embedded images, for images loaded via the
158
-	 * gallery and for the featured image.
159
-	 *
160
-	 * Uses the cache service to store the results of this function for a day.
161
-	 *
162
-	 * @since 3.10.0
163
-	 *
164
-	 * @param WP_Post $post   The target {@link WP_Post}.
165
-	 * @param array   $jsonld The JSON-LD array.
166
-	 */
167
-	protected function set_images( $post, &$jsonld ) {
168
-
169
-		// Prepare the attachment ids array.
170
-		$ids = array();
171
-
172
-		// Set the thumbnail id as first attachment id, if any.
173
-		$thumbnail_id = get_post_thumbnail_id( $post->ID );
174
-		if ( '' !== $thumbnail_id ) {
175
-			$ids[] = $thumbnail_id;
176
-		}
177
-
178
-		// For the time being the following is being removed since the query
179
-		// initiated by `get_image_embeds` is consuming lots of CPU.
180
-		//
181
-		// See https://github.com/insideout10/wordlift-plugin/issues/689.
182
-		//
183
-		// Get the embeds, removing existing ids.
184
-		// $embeds = array_diff( $this->attachment_service->get_image_embeds( $post->post_content ), $ids );
185
-		$embeds = array();
186
-
187
-		// Get the gallery, removing existing ids.
188
-		$gallery = array_diff( $this->attachment_service->get_gallery( $post ), $ids, $embeds );
189
-
190
-		// Map the attachment ids to images' data structured for schema.org use.
191
-		$images = array_filter(
192
-			array_map(
193
-				function ( $item ) {
194
-					/*
21
+    /**
22
+     * The JSON-LD context.
23
+     *
24
+     * @since 3.10.0
25
+     */
26
+    const CONTEXT = 'http://schema.org';
27
+
28
+    /**
29
+     * A {@link Wordlift_Entity_Type_Service} instance.
30
+     *
31
+     * @since  3.10.0
32
+     * @access protected
33
+     * @var \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance.
34
+     */
35
+    protected $entity_type_service;
36
+
37
+    /**
38
+     * A {@link Wordlift_Entity_Service} instance.
39
+     *
40
+     * @since  3.10.0
41
+     * @access protected
42
+     * @var \Wordlift_Entity_Service $entity_type_service A {@link Wordlift_Entity_Service} instance.
43
+     */
44
+    protected $entity_service;
45
+
46
+    /**
47
+     * A {@link Wordlift_User_Service} instance.
48
+     *
49
+     * @since  3.10.0
50
+     * @access private
51
+     * @var \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance.
52
+     */
53
+    protected $user_service;
54
+
55
+    /**
56
+     * A {@link Wordlift_Attachment_Service} instance.
57
+     *
58
+     * @since  3.10.0
59
+     * @access private
60
+     * @var \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance.
61
+     */
62
+    protected $attachment_service;
63
+
64
+    /**
65
+     * Wordlift_Post_To_Jsonld_Converter constructor.
66
+     *
67
+     * @since 3.10.0
68
+     *
69
+     * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance.
70
+     * @param \Wordlift_Entity_Service      $entity_service      A {@link Wordlift_Entity_Service} instance.
71
+     * @param \Wordlift_User_Service        $user_service        A {@link Wordlift_User_Service} instance.
72
+     * @param \Wordlift_Attachment_Service  $attachment_service  A {@link Wordlift_Attachment_Service} instance.
73
+     */
74
+    public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service ) {
75
+
76
+        $this->entity_type_service = $entity_type_service;
77
+        $this->entity_service      = $entity_service;
78
+        $this->user_service        = $user_service;
79
+        $this->attachment_service  = $attachment_service;
80
+    }
81
+
82
+    /**
83
+     * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference
84
+     * found while processing the post is set in the $references array.
85
+     *
86
+     * @since 3.10.0
87
+     *
88
+     * @param int   $post_id    The post id.
89
+     * @param array $references An array of entity references.
90
+     *
91
+     * @return array A JSON-LD array.
92
+     */
93
+    public function convert( $post_id, &$references = array() ) {
94
+
95
+        // Get the post instance.
96
+        $post = get_post( $post_id );
97
+        if ( null === $post ) {
98
+            // Post not found.
99
+            return null;
100
+        }
101
+
102
+        // Get the post URI @id.
103
+        $id = $this->entity_service->get_uri( $post->ID );
104
+
105
+        // Get the entity @type. We consider `post` BlogPostings.
106
+        $type = $this->entity_type_service->get( $post_id );
107
+
108
+        // Prepare the response.
109
+        $jsonld = array(
110
+            '@context'    => self::CONTEXT,
111
+            '@id'         => $id,
112
+            '@type'       => $this->relative_to_context( $type['uri'] ),
113
+            'description' => Wordlift_Post_Excerpt_Helper::get_text_excerpt( $post ),
114
+        );
115
+
116
+        // Set the `mainEntityOfPage` property if the post has some contents.
117
+        if ( ! empty( $post->post_content ) ) {
118
+            // We're setting the `mainEntityOfPage` to signal which one is the
119
+            // main entity for the specified URL. It might be however that the
120
+            // post/page is actually about another specific entity. How WL deals
121
+            // with that hasn't been defined yet (see https://github.com/insideout10/wordlift-plugin/issues/451).
122
+            //
123
+            // See http://schema.org/mainEntityOfPage
124
+            //
125
+            // No need to specify `'@type' => 'WebPage'.
126
+            //
127
+            // See https://github.com/insideout10/wordlift-plugin/issues/451.
128
+            $jsonld['mainEntityOfPage'] = get_the_permalink( $post->ID );
129
+        };
130
+
131
+        $this->set_images( $post, $jsonld );
132
+
133
+        // Get the entities referenced by this post and set it to the `references`
134
+        // array so that the caller can do further processing, such as printing out
135
+        // more of those references.
136
+        $references = $this->entity_service->get_related_entities( $post->ID );
137
+
138
+        return $jsonld;
139
+    }
140
+
141
+    /**
142
+     * If the provided value starts with the schema.org context, we remove the schema.org
143
+     * part since it is set with the '@context'.
144
+     *
145
+     * @since 3.10.0
146
+     *
147
+     * @param string $value The property value.
148
+     *
149
+     * @return string The property value without the context.
150
+     */
151
+    public function relative_to_context( $value ) {
152
+
153
+        return 0 === strpos( $value, self::CONTEXT . '/' ) ? substr( $value, strlen( self::CONTEXT ) + 1 ) : $value;
154
+    }
155
+
156
+    /**
157
+     * Set the images, by looking for embedded images, for images loaded via the
158
+     * gallery and for the featured image.
159
+     *
160
+     * Uses the cache service to store the results of this function for a day.
161
+     *
162
+     * @since 3.10.0
163
+     *
164
+     * @param WP_Post $post   The target {@link WP_Post}.
165
+     * @param array   $jsonld The JSON-LD array.
166
+     */
167
+    protected function set_images( $post, &$jsonld ) {
168
+
169
+        // Prepare the attachment ids array.
170
+        $ids = array();
171
+
172
+        // Set the thumbnail id as first attachment id, if any.
173
+        $thumbnail_id = get_post_thumbnail_id( $post->ID );
174
+        if ( '' !== $thumbnail_id ) {
175
+            $ids[] = $thumbnail_id;
176
+        }
177
+
178
+        // For the time being the following is being removed since the query
179
+        // initiated by `get_image_embeds` is consuming lots of CPU.
180
+        //
181
+        // See https://github.com/insideout10/wordlift-plugin/issues/689.
182
+        //
183
+        // Get the embeds, removing existing ids.
184
+        // $embeds = array_diff( $this->attachment_service->get_image_embeds( $post->post_content ), $ids );
185
+        $embeds = array();
186
+
187
+        // Get the gallery, removing existing ids.
188
+        $gallery = array_diff( $this->attachment_service->get_gallery( $post ), $ids, $embeds );
189
+
190
+        // Map the attachment ids to images' data structured for schema.org use.
191
+        $images = array_filter(
192
+            array_map(
193
+                function ( $item ) {
194
+                    /*
195 195
 					* @todo: we're not sure that we're getting attachment data here, we
196 196
 					* should filter `false`s.
197 197
 					*/
198 198
 
199
-					// Get the attachment data.
200
-					$attachment = wp_get_attachment_image_src( $item, 'full' );
201
-
202
-					// Bail if image is not found.
203
-					// In some cases, you can delete the image from the database
204
-					// or from uploads dir, but the image id still exists as featured image
205
-					// or in [gallery] shortcode.
206
-					if ( empty( $attachment[0] ) ) {
207
-						return null;
208
-					}
209
-
210
-					// Refactor data as per schema.org specifications.
211
-					return Wordlift_Abstract_Post_To_Jsonld_Converter::set_image_size(
212
-						array(
213
-							'@type' => 'ImageObject',
214
-							'url'   => $attachment[0],
215
-						), $attachment
216
-					);
217
-				}, array_merge( $ids, $embeds, $gallery )
218
-			)
219
-		);
220
-
221
-		if ( 0 < count( $images ) ) {
222
-			$jsonld['image'] = $images;
223
-		}
224
-
225
-	}
226
-
227
-	/**
228
-	 * Process the provided array by adding the width / height if the values
229
-	 * are available and are greater than 0.
230
-	 *
231
-	 * @since 3.14.0
232
-	 *
233
-	 * @param array $image      The `ImageObject` array.
234
-	 * @param array $attachment The attachment array.
235
-	 *
236
-	 * @return array The enriched `ImageObject` array.
237
-	 */
238
-	public static function set_image_size( $image, $attachment ) {
239
-
240
-		// If you specify a "width" or "height" value you should leave out
241
-		// 'px'. For example: "width":"4608px" should be "width":"4608".
242
-		//
243
-		// See https://github.com/insideout10/wordlift-plugin/issues/451.
244
-		if ( isset( $attachment[1] ) && is_numeric( $attachment[1] ) && 0 < $attachment[1] ) {
245
-			$image['width'] = $attachment[1];
246
-		}
247
-
248
-		if ( isset( $attachment[2] ) && is_numeric( $attachment[2] ) && 0 < $attachment[2] ) {
249
-			$image['height'] = $attachment[2];
250
-		}
251
-
252
-		return $image;
253
-	}
199
+                    // Get the attachment data.
200
+                    $attachment = wp_get_attachment_image_src( $item, 'full' );
201
+
202
+                    // Bail if image is not found.
203
+                    // In some cases, you can delete the image from the database
204
+                    // or from uploads dir, but the image id still exists as featured image
205
+                    // or in [gallery] shortcode.
206
+                    if ( empty( $attachment[0] ) ) {
207
+                        return null;
208
+                    }
209
+
210
+                    // Refactor data as per schema.org specifications.
211
+                    return Wordlift_Abstract_Post_To_Jsonld_Converter::set_image_size(
212
+                        array(
213
+                            '@type' => 'ImageObject',
214
+                            'url'   => $attachment[0],
215
+                        ), $attachment
216
+                    );
217
+                }, array_merge( $ids, $embeds, $gallery )
218
+            )
219
+        );
220
+
221
+        if ( 0 < count( $images ) ) {
222
+            $jsonld['image'] = $images;
223
+        }
224
+
225
+    }
226
+
227
+    /**
228
+     * Process the provided array by adding the width / height if the values
229
+     * are available and are greater than 0.
230
+     *
231
+     * @since 3.14.0
232
+     *
233
+     * @param array $image      The `ImageObject` array.
234
+     * @param array $attachment The attachment array.
235
+     *
236
+     * @return array The enriched `ImageObject` array.
237
+     */
238
+    public static function set_image_size( $image, $attachment ) {
239
+
240
+        // If you specify a "width" or "height" value you should leave out
241
+        // 'px'. For example: "width":"4608px" should be "width":"4608".
242
+        //
243
+        // See https://github.com/insideout10/wordlift-plugin/issues/451.
244
+        if ( isset( $attachment[1] ) && is_numeric( $attachment[1] ) && 0 < $attachment[1] ) {
245
+            $image['width'] = $attachment[1];
246
+        }
247
+
248
+        if ( isset( $attachment[2] ) && is_numeric( $attachment[2] ) && 0 < $attachment[2] ) {
249
+            $image['height'] = $attachment[2];
250
+        }
251
+
252
+        return $image;
253
+    }
254 254
 }
Please login to merge, or discard this patch.
Spacing   +26 added lines, -26 removed lines patch added patch discarded remove patch
@@ -71,7 +71,7 @@  discard block
 block discarded – undo
71 71
 	 * @param \Wordlift_User_Service        $user_service        A {@link Wordlift_User_Service} instance.
72 72
 	 * @param \Wordlift_Attachment_Service  $attachment_service  A {@link Wordlift_Attachment_Service} instance.
73 73
 	 */
74
-	public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service ) {
74
+	public function __construct($entity_type_service, $entity_service, $user_service, $attachment_service) {
75 75
 
76 76
 		$this->entity_type_service = $entity_type_service;
77 77
 		$this->entity_service      = $entity_service;
@@ -90,31 +90,31 @@  discard block
 block discarded – undo
90 90
 	 *
91 91
 	 * @return array A JSON-LD array.
92 92
 	 */
93
-	public function convert( $post_id, &$references = array() ) {
93
+	public function convert($post_id, &$references = array()) {
94 94
 
95 95
 		// Get the post instance.
96
-		$post = get_post( $post_id );
97
-		if ( null === $post ) {
96
+		$post = get_post($post_id);
97
+		if (null === $post) {
98 98
 			// Post not found.
99 99
 			return null;
100 100
 		}
101 101
 
102 102
 		// Get the post URI @id.
103
-		$id = $this->entity_service->get_uri( $post->ID );
103
+		$id = $this->entity_service->get_uri($post->ID);
104 104
 
105 105
 		// Get the entity @type. We consider `post` BlogPostings.
106
-		$type = $this->entity_type_service->get( $post_id );
106
+		$type = $this->entity_type_service->get($post_id);
107 107
 
108 108
 		// Prepare the response.
109 109
 		$jsonld = array(
110 110
 			'@context'    => self::CONTEXT,
111 111
 			'@id'         => $id,
112
-			'@type'       => $this->relative_to_context( $type['uri'] ),
113
-			'description' => Wordlift_Post_Excerpt_Helper::get_text_excerpt( $post ),
112
+			'@type'       => $this->relative_to_context($type['uri']),
113
+			'description' => Wordlift_Post_Excerpt_Helper::get_text_excerpt($post),
114 114
 		);
115 115
 
116 116
 		// Set the `mainEntityOfPage` property if the post has some contents.
117
-		if ( ! empty( $post->post_content ) ) {
117
+		if ( ! empty($post->post_content)) {
118 118
 			// We're setting the `mainEntityOfPage` to signal which one is the
119 119
 			// main entity for the specified URL. It might be however that the
120 120
 			// post/page is actually about another specific entity. How WL deals
@@ -125,15 +125,15 @@  discard block
 block discarded – undo
125 125
 			// No need to specify `'@type' => 'WebPage'.
126 126
 			//
127 127
 			// See https://github.com/insideout10/wordlift-plugin/issues/451.
128
-			$jsonld['mainEntityOfPage'] = get_the_permalink( $post->ID );
128
+			$jsonld['mainEntityOfPage'] = get_the_permalink($post->ID);
129 129
 		};
130 130
 
131
-		$this->set_images( $post, $jsonld );
131
+		$this->set_images($post, $jsonld);
132 132
 
133 133
 		// Get the entities referenced by this post and set it to the `references`
134 134
 		// array so that the caller can do further processing, such as printing out
135 135
 		// more of those references.
136
-		$references = $this->entity_service->get_related_entities( $post->ID );
136
+		$references = $this->entity_service->get_related_entities($post->ID);
137 137
 
138 138
 		return $jsonld;
139 139
 	}
@@ -148,9 +148,9 @@  discard block
 block discarded – undo
148 148
 	 *
149 149
 	 * @return string The property value without the context.
150 150
 	 */
151
-	public function relative_to_context( $value ) {
151
+	public function relative_to_context($value) {
152 152
 
153
-		return 0 === strpos( $value, self::CONTEXT . '/' ) ? substr( $value, strlen( self::CONTEXT ) + 1 ) : $value;
153
+		return 0 === strpos($value, self::CONTEXT.'/') ? substr($value, strlen(self::CONTEXT) + 1) : $value;
154 154
 	}
155 155
 
156 156
 	/**
@@ -164,14 +164,14 @@  discard block
 block discarded – undo
164 164
 	 * @param WP_Post $post   The target {@link WP_Post}.
165 165
 	 * @param array   $jsonld The JSON-LD array.
166 166
 	 */
167
-	protected function set_images( $post, &$jsonld ) {
167
+	protected function set_images($post, &$jsonld) {
168 168
 
169 169
 		// Prepare the attachment ids array.
170 170
 		$ids = array();
171 171
 
172 172
 		// Set the thumbnail id as first attachment id, if any.
173
-		$thumbnail_id = get_post_thumbnail_id( $post->ID );
174
-		if ( '' !== $thumbnail_id ) {
173
+		$thumbnail_id = get_post_thumbnail_id($post->ID);
174
+		if ('' !== $thumbnail_id) {
175 175
 			$ids[] = $thumbnail_id;
176 176
 		}
177 177
 
@@ -185,25 +185,25 @@  discard block
 block discarded – undo
185 185
 		$embeds = array();
186 186
 
187 187
 		// Get the gallery, removing existing ids.
188
-		$gallery = array_diff( $this->attachment_service->get_gallery( $post ), $ids, $embeds );
188
+		$gallery = array_diff($this->attachment_service->get_gallery($post), $ids, $embeds);
189 189
 
190 190
 		// Map the attachment ids to images' data structured for schema.org use.
191 191
 		$images = array_filter(
192 192
 			array_map(
193
-				function ( $item ) {
193
+				function($item) {
194 194
 					/*
195 195
 					* @todo: we're not sure that we're getting attachment data here, we
196 196
 					* should filter `false`s.
197 197
 					*/
198 198
 
199 199
 					// Get the attachment data.
200
-					$attachment = wp_get_attachment_image_src( $item, 'full' );
200
+					$attachment = wp_get_attachment_image_src($item, 'full');
201 201
 
202 202
 					// Bail if image is not found.
203 203
 					// In some cases, you can delete the image from the database
204 204
 					// or from uploads dir, but the image id still exists as featured image
205 205
 					// or in [gallery] shortcode.
206
-					if ( empty( $attachment[0] ) ) {
206
+					if (empty($attachment[0])) {
207 207
 						return null;
208 208
 					}
209 209
 
@@ -214,11 +214,11 @@  discard block
 block discarded – undo
214 214
 							'url'   => $attachment[0],
215 215
 						), $attachment
216 216
 					);
217
-				}, array_merge( $ids, $embeds, $gallery )
217
+				}, array_merge($ids, $embeds, $gallery)
218 218
 			)
219 219
 		);
220 220
 
221
-		if ( 0 < count( $images ) ) {
221
+		if (0 < count($images)) {
222 222
 			$jsonld['image'] = $images;
223 223
 		}
224 224
 
@@ -235,17 +235,17 @@  discard block
 block discarded – undo
235 235
 	 *
236 236
 	 * @return array The enriched `ImageObject` array.
237 237
 	 */
238
-	public static function set_image_size( $image, $attachment ) {
238
+	public static function set_image_size($image, $attachment) {
239 239
 
240 240
 		// If you specify a "width" or "height" value you should leave out
241 241
 		// 'px'. For example: "width":"4608px" should be "width":"4608".
242 242
 		//
243 243
 		// See https://github.com/insideout10/wordlift-plugin/issues/451.
244
-		if ( isset( $attachment[1] ) && is_numeric( $attachment[1] ) && 0 < $attachment[1] ) {
244
+		if (isset($attachment[1]) && is_numeric($attachment[1]) && 0 < $attachment[1]) {
245 245
 			$image['width'] = $attachment[1];
246 246
 		}
247 247
 
248
-		if ( isset( $attachment[2] ) && is_numeric( $attachment[2] ) && 0 < $attachment[2] ) {
248
+		if (isset($attachment[2]) && is_numeric($attachment[2]) && 0 < $attachment[2]) {
249 249
 			$image['height'] = $attachment[2];
250 250
 		}
251 251
 
Please login to merge, or discard this patch.