Completed
Pull Request — master (#1455)
by Naveen
01:04
created
src/admin/partials/wordlift-admin-settings-videoobject-settings-page.php 1 patch
Spacing   +29 added lines, -29 removed lines patch added patch discarded remove patch
@@ -4,8 +4,8 @@  discard block
 block discarded – undo
4 4
 use Wordlift\Videoobject\Provider\Client\Youtube_Client;
5 5
 
6 6
 
7
-if ( isset( $_POST['wordlift_videoobject_youtube_api_key'] )
8
-     || isset( $_POST['wordlift_videoobject_vimeo_api_key'] ) ) {
7
+if (isset($_POST['wordlift_videoobject_youtube_api_key'])
8
+     || isset($_POST['wordlift_videoobject_vimeo_api_key'])) {
9 9
 
10 10
 	/**
11 11
 	 * @todo: does this fields need to be encrypted before saving ?
@@ -13,84 +13,84 @@  discard block
 block discarded – undo
13 13
 	$youtube_api_key = (string) $_POST['wordlift_videoobject_youtube_api_key'];
14 14
 	$vimeo_api_key   = (string) $_POST['wordlift_videoobject_vimeo_api_key'];
15 15
 
16
-	if ( $youtube_api_key ) {
17
-		update_option( Youtube_Client::get_api_key_option_name(), $youtube_api_key );
16
+	if ($youtube_api_key) {
17
+		update_option(Youtube_Client::get_api_key_option_name(), $youtube_api_key);
18 18
 	}
19 19
 
20
-	if ( $vimeo_api_key ) {
21
-		update_option( Vimeo_Client::get_api_key_option_name(), $vimeo_api_key );
20
+	if ($vimeo_api_key) {
21
+		update_option(Vimeo_Client::get_api_key_option_name(), $vimeo_api_key);
22 22
 	}
23 23
 }
24 24
 
25
-if ( isset( $_POST['submit'] ) ) {
26
-	if ( isset( $_POST['wl_enable_video_sitemap'] ) ) {
27
-		update_option( "_wl_video_sitemap_generation", 1 );
25
+if (isset($_POST['submit'])) {
26
+	if (isset($_POST['wl_enable_video_sitemap'])) {
27
+		update_option("_wl_video_sitemap_generation", 1);
28 28
 		// flush the rewrite rules
29 29
 		flush_rewrite_rules();
30 30
 	} else {
31
-		update_option( "_wl_video_sitemap_generation", 0 );
31
+		update_option("_wl_video_sitemap_generation", 0);
32 32
 		// flush the rewrite rules
33 33
 		flush_rewrite_rules();
34 34
 	}
35 35
 }
36 36
 
37 37
 ?>
38
-<h1><?php _e( 'API Settings', 'wordlift' ); ?></h1>
39
-<p><?php _e( 'To let WordLift access metadata from YouTube or Vimeo you will need to add here your API Key.', 'wordlift' ); ?></p>
38
+<h1><?php _e('API Settings', 'wordlift'); ?></h1>
39
+<p><?php _e('To let WordLift access metadata from YouTube or Vimeo you will need to add here your API Key.', 'wordlift'); ?></p>
40 40
 <form method="post">
41 41
     <table>
42 42
         <tr>
43 43
             <td>
44
-				<?php _e( 'YouTube API Key', 'wordlift' ); ?>
44
+				<?php _e('YouTube API Key', 'wordlift'); ?>
45 45
             </td>
46 46
             <td>
47 47
 				<?php
48 48
 				$element = new Wordlift_Admin_Input_Element();
49
-				$element->render( array(
49
+				$element->render(array(
50 50
 					'id'    => 'wordlift_videoobject_youtube_api_key',
51 51
 					'name'  => 'wordlift_videoobject_youtube_api_key',
52 52
 					'value' => Youtube_Client::get_api_key()
53
-				) );
53
+				));
54 54
 				?>
55 55
             </td>
56 56
             <td>
57
-                <a href="https://developers.google.com/youtube/registering_an_application"><?php _e( 'Click here', 'wordlift' ); ?></a>
58
-				<?php _e( ' for instructions on getting your YouTube API Key', 'wordlift' ); ?>
57
+                <a href="https://developers.google.com/youtube/registering_an_application"><?php _e('Click here', 'wordlift'); ?></a>
58
+				<?php _e(' for instructions on getting your YouTube API Key', 'wordlift'); ?>
59 59
             </td>
60 60
         </tr>
61 61
 
62 62
         <tr>
63 63
             <td>
64
-				<?php _e( 'Vimeo API Key', 'wordlift' ); ?>
64
+				<?php _e('Vimeo API Key', 'wordlift'); ?>
65 65
             </td>
66 66
             <td>
67 67
 				<?php
68 68
 				$element = new Wordlift_Admin_Input_Element();
69
-				$element->render( array(
69
+				$element->render(array(
70 70
 					'id'    => 'wordlift_videoobject_vimeo_api_key',
71 71
 					'name'  => 'wordlift_videoobject_vimeo_api_key',
72 72
 					'value' => Vimeo_Client::get_api_key()
73
-				) );
73
+				));
74 74
 				?>
75 75
             </td>
76 76
             <td>
77
-                <a href="https://developer.vimeo.com/api/guides/start"><?php _e( 'Click here', 'wordlift' ); ?></a>
78
-				<?php _e( ' for instructions on getting your Vimeo API Key', 'wordlift' ); ?>
77
+                <a href="https://developer.vimeo.com/api/guides/start"><?php _e('Click here', 'wordlift'); ?></a>
78
+				<?php _e(' for instructions on getting your Vimeo API Key', 'wordlift'); ?>
79 79
             </td>
80 80
         </tr>
81 81
 
82 82
     </table>
83
-    <h1><?php _e( 'Video Sitemap', 'wordlift' ); ?></h1>
83
+    <h1><?php _e('Video Sitemap', 'wordlift'); ?></h1>
84 84
     <p>
85
-		<?php _e( 'The Video Sitemap works like any other XML Sitemap. Search engines will use it to display rich snippets in result pages.', 'wordlift'  ); ?>
85
+		<?php _e('The Video Sitemap works like any other XML Sitemap. Search engines will use it to display rich snippets in result pages.', 'wordlift'); ?>
86 86
     </p>
87
-	<?php $is_sitemap_enabled = get_option( '_wl_video_sitemap_generation', false ) ? 'checked' : '' ?>
88
-    <p> <?php _e( 'Enable Video Sitemap' ); ?>
87
+	<?php $is_sitemap_enabled = get_option('_wl_video_sitemap_generation', false) ? 'checked' : '' ?>
88
+    <p> <?php _e('Enable Video Sitemap'); ?>
89 89
         <input type="checkbox" name="wl_enable_video_sitemap" value="1" <?php echo $is_sitemap_enabled; ?> ></p>
90 90
     <p> <?php
91
-		if ( $is_sitemap_enabled ) {
92
-			$sitemap_link = get_home_url( null, 'wl-video-sitemap.xml' );
93
-			printf( __( 'Here is <a href="%s">link</a> to your Video Sitemap. Add it now, to Google Search Console.', 'wordlift' ), $sitemap_link );
91
+		if ($is_sitemap_enabled) {
92
+			$sitemap_link = get_home_url(null, 'wl-video-sitemap.xml');
93
+			printf(__('Here is <a href="%s">link</a> to your Video Sitemap. Add it now, to Google Search Console.', 'wordlift'), $sitemap_link);
94 94
 		}
95 95
 		?></p>
96 96
 
Please login to merge, or discard this patch.
src/admin/wordlift_admin_ajax_related_posts.php 2 patches
Indentation   +39 added lines, -39 removed lines patch added patch discarded remove patch
@@ -16,57 +16,57 @@
 block discarded – undo
16 16
  */
17 17
 function wordlift_ajax_related_posts( $http_raw_data = null ) {
18 18
 
19
-	// Extract filtering conditions
20
-	if ( ! isset( $_GET["post_id"] ) || ! is_numeric( $_GET["post_id"] ) ) {
21
-		wp_die( 'Post id missing or invalid!' );
19
+    // Extract filtering conditions
20
+    if ( ! isset( $_GET["post_id"] ) || ! is_numeric( $_GET["post_id"] ) ) {
21
+        wp_die( 'Post id missing or invalid!' );
22 22
 
23
-		return;
24
-	}
23
+        return;
24
+    }
25 25
 
26
-	// Get the current post
27
-	$post_id = (int) $_GET["post_id"];
28
-	$post    = get_post( $post_id );
26
+    // Get the current post
27
+    $post_id = (int) $_GET["post_id"];
28
+    $post    = get_post( $post_id );
29 29
 
30
-	Wordlift_Log_Service::get_logger( 'wordlift_ajax_related_posts' )->trace( "Going to find posts related to current with post id: $post_id ..." );
30
+    Wordlift_Log_Service::get_logger( 'wordlift_ajax_related_posts' )->trace( "Going to find posts related to current with post id: $post_id ..." );
31 31
 
32
-	// Extract filtering conditions
33
-	$filtering_entity_uris = ( null == $http_raw_data ) ? file_get_contents( "php://input" ) : $http_raw_data;
34
-	$filtering_entity_uris = json_decode( $filtering_entity_uris );
32
+    // Extract filtering conditions
33
+    $filtering_entity_uris = ( null == $http_raw_data ) ? file_get_contents( "php://input" ) : $http_raw_data;
34
+    $filtering_entity_uris = json_decode( $filtering_entity_uris );
35 35
 
36
-	$filtering_entity_ids = wl_get_entity_post_ids_by_uris( $filtering_entity_uris );
37
-	$related_posts        = array();
36
+    $filtering_entity_ids = wl_get_entity_post_ids_by_uris( $filtering_entity_uris );
37
+    $related_posts        = array();
38 38
 
39
-	// If the current post is an entity, related posts to the current entity are
40
-	// returned.
41
-	$entity_service = Wordlift_Entity_Service::get_instance();
39
+    // If the current post is an entity, related posts to the current entity are
40
+    // returned.
41
+    $entity_service = Wordlift_Entity_Service::get_instance();
42 42
 
43
-	if ( $entity_service->is_entity( $post->ID ) ) {
44
-		$filtering_entity_ids = array( $post_id );
45
-	}
43
+    if ( $entity_service->is_entity( $post->ID ) ) {
44
+        $filtering_entity_ids = array( $post_id );
45
+    }
46 46
 
47
-	if ( ! empty( $filtering_entity_ids ) ) {
47
+    if ( ! empty( $filtering_entity_ids ) ) {
48 48
 
49
-		$related_posts = Wordlift_Relation_Service::get_instance()
50
-		                                          ->get_article_subjects( $filtering_entity_ids, '*', null, 'publish', array( $post_id ), 5 );
49
+        $related_posts = Wordlift_Relation_Service::get_instance()
50
+                                                    ->get_article_subjects( $filtering_entity_ids, '*', null, 'publish', array( $post_id ), 5 );
51 51
 
52
-		foreach ( $related_posts as $post_obj ) {
52
+        foreach ( $related_posts as $post_obj ) {
53 53
 
54
-			/**
55
-			 * Use the thumbnail.
56
-			 *
57
-			 * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue.
58
-			 * @see https://github.com/insideout10/wordlift-plugin/issues/837
59
-			 *
60
-			 * @since 3.19.3 We're using the medium size image.
61
-			 */
62
-			$thumbnail           = get_the_post_thumbnail_url( $post_obj, 'medium' );
63
-			$post_obj->thumbnail = ( $thumbnail ) ? $thumbnail : WL_DEFAULT_THUMBNAIL_PATH;
64
-			$post_obj->link      = get_edit_post_link( $post_obj->ID, 'none' );
65
-			$post_obj->permalink = get_post_permalink( $post_obj->ID );
66
-		}
67
-	}
54
+            /**
55
+             * Use the thumbnail.
56
+             *
57
+             * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue.
58
+             * @see https://github.com/insideout10/wordlift-plugin/issues/837
59
+             *
60
+             * @since 3.19.3 We're using the medium size image.
61
+             */
62
+            $thumbnail           = get_the_post_thumbnail_url( $post_obj, 'medium' );
63
+            $post_obj->thumbnail = ( $thumbnail ) ? $thumbnail : WL_DEFAULT_THUMBNAIL_PATH;
64
+            $post_obj->link      = get_edit_post_link( $post_obj->ID, 'none' );
65
+            $post_obj->permalink = get_post_permalink( $post_obj->ID );
66
+        }
67
+    }
68 68
 
69
-	wl_core_send_json( $related_posts );
69
+    wl_core_send_json( $related_posts );
70 70
 
71 71
 }
72 72
 
Please login to merge, or discard this patch.
Spacing   +19 added lines, -19 removed lines patch added patch discarded remove patch
@@ -14,42 +14,42 @@  discard block
 block discarded – undo
14 14
  *
15 15
  * @param null $http_raw_data
16 16
  */
17
-function wordlift_ajax_related_posts( $http_raw_data = null ) {
17
+function wordlift_ajax_related_posts($http_raw_data = null) {
18 18
 
19 19
 	// Extract filtering conditions
20
-	if ( ! isset( $_GET["post_id"] ) || ! is_numeric( $_GET["post_id"] ) ) {
21
-		wp_die( 'Post id missing or invalid!' );
20
+	if ( ! isset($_GET["post_id"]) || ! is_numeric($_GET["post_id"])) {
21
+		wp_die('Post id missing or invalid!');
22 22
 
23 23
 		return;
24 24
 	}
25 25
 
26 26
 	// Get the current post
27 27
 	$post_id = (int) $_GET["post_id"];
28
-	$post    = get_post( $post_id );
28
+	$post    = get_post($post_id);
29 29
 
30
-	Wordlift_Log_Service::get_logger( 'wordlift_ajax_related_posts' )->trace( "Going to find posts related to current with post id: $post_id ..." );
30
+	Wordlift_Log_Service::get_logger('wordlift_ajax_related_posts')->trace("Going to find posts related to current with post id: $post_id ...");
31 31
 
32 32
 	// Extract filtering conditions
33
-	$filtering_entity_uris = ( null == $http_raw_data ) ? file_get_contents( "php://input" ) : $http_raw_data;
34
-	$filtering_entity_uris = json_decode( $filtering_entity_uris );
33
+	$filtering_entity_uris = (null == $http_raw_data) ? file_get_contents("php://input") : $http_raw_data;
34
+	$filtering_entity_uris = json_decode($filtering_entity_uris);
35 35
 
36
-	$filtering_entity_ids = wl_get_entity_post_ids_by_uris( $filtering_entity_uris );
36
+	$filtering_entity_ids = wl_get_entity_post_ids_by_uris($filtering_entity_uris);
37 37
 	$related_posts        = array();
38 38
 
39 39
 	// If the current post is an entity, related posts to the current entity are
40 40
 	// returned.
41 41
 	$entity_service = Wordlift_Entity_Service::get_instance();
42 42
 
43
-	if ( $entity_service->is_entity( $post->ID ) ) {
44
-		$filtering_entity_ids = array( $post_id );
43
+	if ($entity_service->is_entity($post->ID)) {
44
+		$filtering_entity_ids = array($post_id);
45 45
 	}
46 46
 
47
-	if ( ! empty( $filtering_entity_ids ) ) {
47
+	if ( ! empty($filtering_entity_ids)) {
48 48
 
49 49
 		$related_posts = Wordlift_Relation_Service::get_instance()
50
-		                                          ->get_article_subjects( $filtering_entity_ids, '*', null, 'publish', array( $post_id ), 5 );
50
+		                                          ->get_article_subjects($filtering_entity_ids, '*', null, 'publish', array($post_id), 5);
51 51
 
52
-		foreach ( $related_posts as $post_obj ) {
52
+		foreach ($related_posts as $post_obj) {
53 53
 
54 54
 			/**
55 55
 			 * Use the thumbnail.
@@ -59,15 +59,15 @@  discard block
 block discarded – undo
59 59
 			 *
60 60
 			 * @since 3.19.3 We're using the medium size image.
61 61
 			 */
62
-			$thumbnail           = get_the_post_thumbnail_url( $post_obj, 'medium' );
63
-			$post_obj->thumbnail = ( $thumbnail ) ? $thumbnail : WL_DEFAULT_THUMBNAIL_PATH;
64
-			$post_obj->link      = get_edit_post_link( $post_obj->ID, 'none' );
65
-			$post_obj->permalink = get_post_permalink( $post_obj->ID );
62
+			$thumbnail           = get_the_post_thumbnail_url($post_obj, 'medium');
63
+			$post_obj->thumbnail = ($thumbnail) ? $thumbnail : WL_DEFAULT_THUMBNAIL_PATH;
64
+			$post_obj->link      = get_edit_post_link($post_obj->ID, 'none');
65
+			$post_obj->permalink = get_post_permalink($post_obj->ID);
66 66
 		}
67 67
 	}
68 68
 
69
-	wl_core_send_json( $related_posts );
69
+	wl_core_send_json($related_posts);
70 70
 
71 71
 }
72 72
 
73
-add_action( 'wp_ajax_wordlift_related_posts', 'wordlift_ajax_related_posts' );
73
+add_action('wp_ajax_wordlift_related_posts', 'wordlift_ajax_related_posts');
Please login to merge, or discard this patch.
src/admin/class-wordlift-admin-term-adapter.php 2 patches
Indentation   +124 added lines, -124 removed lines patch added patch discarded remove patch
@@ -16,78 +16,78 @@  discard block
 block discarded – undo
16 16
  */
17 17
 class Wordlift_Admin_Term_Adapter {
18 18
 
19
-	/**
20
-	 * The meta key holding the entity id.
21
-	 *
22
-	 * @since 3.20.0
23
-	 */
24
-	const META_KEY = '_wl_entity_id';
25
-
26
-	/**
27
-	 * Create a Wordlift_Admin_Term_Adapter instance.
28
-	 *
29
-	 * @since 3.20.0
30
-	 */
31
-	public function __construct() {
32
-
33
-		add_action( 'registered_taxonomy', array( $this, 'add_action', ) );
34
-		add_action( 'edit_term', array( $this, 'edit_term', ), 10, 3 );
35
-		$this->add_settings();
36
-
37
-	}
38
-
39
-	/**
40
-	 * Hook in to WordLift admin settings and add the term page specific
41
-	 * settings.
42
-	 * @since 3.26.1
43
-	 */
44
-	public function add_settings() {
45
-		add_filter( 'wl_admin_settings', function ( $params ) {
46
-			$params['show_local_entities'] = true;
47
-			return $params;
48
-		} );
49
-	}
50
-
51
-	/**
52
-	 * Add the form fields to the entity edit screen.
53
-	 *
54
-	 * @param object $tag Current taxonomy term object.
55
-	 * @param string $taxonomy Current taxonomy slug.
56
-	 *
57
-	 * @since 3.20.0
58
-	 *
59
-	 */
60
-	public function edit_form_fields( $tag, $taxonomy ) {
61
-
62
-	    // If disabled via filter, return;
63
-		if ( ! apply_filters( 'wl_feature__enable__term-entity', true ) ) {
64
-		    return;
19
+    /**
20
+     * The meta key holding the entity id.
21
+     *
22
+     * @since 3.20.0
23
+     */
24
+    const META_KEY = '_wl_entity_id';
25
+
26
+    /**
27
+     * Create a Wordlift_Admin_Term_Adapter instance.
28
+     *
29
+     * @since 3.20.0
30
+     */
31
+    public function __construct() {
32
+
33
+        add_action( 'registered_taxonomy', array( $this, 'add_action', ) );
34
+        add_action( 'edit_term', array( $this, 'edit_term', ), 10, 3 );
35
+        $this->add_settings();
36
+
37
+    }
38
+
39
+    /**
40
+     * Hook in to WordLift admin settings and add the term page specific
41
+     * settings.
42
+     * @since 3.26.1
43
+     */
44
+    public function add_settings() {
45
+        add_filter( 'wl_admin_settings', function ( $params ) {
46
+            $params['show_local_entities'] = true;
47
+            return $params;
48
+        } );
49
+    }
50
+
51
+    /**
52
+     * Add the form fields to the entity edit screen.
53
+     *
54
+     * @param object $tag Current taxonomy term object.
55
+     * @param string $taxonomy Current taxonomy slug.
56
+     *
57
+     * @since 3.20.0
58
+     *
59
+     */
60
+    public function edit_form_fields( $tag, $taxonomy ) {
61
+
62
+        // If disabled via filter, return;
63
+        if ( ! apply_filters( 'wl_feature__enable__term-entity', true ) ) {
64
+            return;
65 65
         }
66 66
 
67
-		global $wp_version;
67
+        global $wp_version;
68 68
 
69
-		// Enqueue the JavaScript app.
70
-		if ( version_compare( $wp_version, '5.0', '>=' ) ) {
71
-			$term_asset = include plugin_dir_path( dirname( __FILE__ ) ) . 'js/dist/term.asset.php';
72
-			wp_enqueue_script( 'wl-term', plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/term.js', array_merge( array( 'wp-util' ), $term_asset['dependencies'] ), Wordlift::get_instance()->get_version(), true );
73
-		} else {
74
-			wp_enqueue_script( 'wl-term', plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/term.full.js', array( 'wp-util' ), Wordlift::get_instance()->get_version(), true );
75
-		}
69
+        // Enqueue the JavaScript app.
70
+        if ( version_compare( $wp_version, '5.0', '>=' ) ) {
71
+            $term_asset = include plugin_dir_path( dirname( __FILE__ ) ) . 'js/dist/term.asset.php';
72
+            wp_enqueue_script( 'wl-term', plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/term.js', array_merge( array( 'wp-util' ), $term_asset['dependencies'] ), Wordlift::get_instance()->get_version(), true );
73
+        } else {
74
+            wp_enqueue_script( 'wl-term', plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/term.full.js', array( 'wp-util' ), Wordlift::get_instance()->get_version(), true );
75
+        }
76 76
 
77
-		wp_enqueue_style( 'wl-term', plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/term.css', array(), Wordlift::get_instance()->get_version() );
77
+        wp_enqueue_style( 'wl-term', plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/term.css', array(), Wordlift::get_instance()->get_version() );
78 78
 
79
-		$values = get_term_meta( $tag->term_id, self::META_KEY );
79
+        $values = get_term_meta( $tag->term_id, self::META_KEY );
80 80
 
81
-		/**
82
-		 * @since 3.31.3
81
+        /**
82
+         * @since 3.31.3
83 83
          * @see https://github.com/insideout10/wordlift-plugin/issues/1446
84 84
          * This field should be hidden by default
85
-		 */
86
-		if ( ! $values ) {
87
-		    return;
85
+         */
86
+        if ( ! $values ) {
87
+            return;
88 88
         }
89 89
 
90
-		?>
90
+        ?>
91 91
         <tr class="form-field term-name-wrap">
92 92
             <th scope="row"><label for="wl-entity-id"><?php _ex( 'Entity', 'term entity', 'wordlift' ); ?></label></th>
93 93
             <td>
@@ -99,69 +99,69 @@  discard block
 block discarded – undo
99 99
             </td>
100 100
         </tr>
101 101
 		<?php
102
-	}
103
-
104
-	/**
105
-	 * Bind the new fields to the edit term screen.
106
-	 *
107
-	 * @since 3.20.0
108
-	 *
109
-	 * @param string $taxonomy The taxonomy name.
110
-	 */
111
-	public function add_action( $taxonomy ) {
112
-		/**
113
-		 * Filter wl_feature__enable__taxonomy_term_entity_mapping renamed to wl_feature__enable__term-entity.
102
+    }
103
+
104
+    /**
105
+     * Bind the new fields to the edit term screen.
106
+     *
107
+     * @since 3.20.0
108
+     *
109
+     * @param string $taxonomy The taxonomy name.
110
+     */
111
+    public function add_action( $taxonomy ) {
112
+        /**
113
+         * Filter wl_feature__enable__taxonomy_term_entity_mapping renamed to wl_feature__enable__term-entity.
114 114
          **/
115 115
 
116
-		add_action( "{$taxonomy}_edit_form_fields", array( $this, 'edit_form_fields' ), 10, 2 );
117
-
118
-	}
119
-
120
-	/**
121
-	 * Hook to the edit term to handle our own custom fields.
122
-	 *
123
-	 * @since 3.20.0
124
-	 *
125
-	 * @param int    $term_id The term id.
126
-	 * @param int    $tt_id The term taxonomy id.
127
-	 * @param string $taxonomy The taxonomy.
128
-	 */
129
-	public function edit_term( $term_id, $tt_id, $taxonomy ) {
130
-
131
-		// Bail if the action isn't related to the term currently being edited.
132
-		if ( ! isset( $_POST['tag_ID'] ) || $term_id !== (int) $_POST['tag_ID'] ) {
133
-			return;
134
-		}
135
-
136
-		// Delete.
137
-		if ( ! isset( $_POST['wl_entity_id'] ) || ! is_array( $_POST['wl_entity_id'] ) || empty( $_POST['wl_entity_id'] ) ) {
138
-			delete_term_meta( $term_id, self::META_KEY );
139
-
140
-			return;
141
-		}
142
-
143
-		// Update.
144
-		//
145
-		// Only use mb_* functions when mbstring is available.
146
-		//
147
-		// See https://github.com/insideout10/wordlift-plugin/issues/693.
148
-		if ( extension_loaded( 'mbstring' ) ) {
149
-			mb_regex_encoding( 'UTF-8' );
150
-
151
-			$merged = array_reduce( (array) $_POST['wl_entity_id'], function ( $carry, $item ) {
152
-				return array_merge( $carry, mb_split( "\x{2063}", wp_unslash( $item ) ) );
153
-			}, array() );
154
-		} else {
155
-			$merged = array_reduce( (array) $_POST['wl_entity_id'], function ( $carry, $item ) {
156
-				return array_merge( $carry, preg_split( "/\x{2063}/u", wp_unslash( $item ) ) );
157
-			}, array() );
158
-		}
159
-
160
-		delete_term_meta( $term_id, self::META_KEY );
161
-		foreach ( array_unique( array_filter( $merged ) ) as $single ) {
162
-			add_term_meta( $term_id, self::META_KEY, $single );
163
-		}
164
-
165
-	}
116
+        add_action( "{$taxonomy}_edit_form_fields", array( $this, 'edit_form_fields' ), 10, 2 );
117
+
118
+    }
119
+
120
+    /**
121
+     * Hook to the edit term to handle our own custom fields.
122
+     *
123
+     * @since 3.20.0
124
+     *
125
+     * @param int    $term_id The term id.
126
+     * @param int    $tt_id The term taxonomy id.
127
+     * @param string $taxonomy The taxonomy.
128
+     */
129
+    public function edit_term( $term_id, $tt_id, $taxonomy ) {
130
+
131
+        // Bail if the action isn't related to the term currently being edited.
132
+        if ( ! isset( $_POST['tag_ID'] ) || $term_id !== (int) $_POST['tag_ID'] ) {
133
+            return;
134
+        }
135
+
136
+        // Delete.
137
+        if ( ! isset( $_POST['wl_entity_id'] ) || ! is_array( $_POST['wl_entity_id'] ) || empty( $_POST['wl_entity_id'] ) ) {
138
+            delete_term_meta( $term_id, self::META_KEY );
139
+
140
+            return;
141
+        }
142
+
143
+        // Update.
144
+        //
145
+        // Only use mb_* functions when mbstring is available.
146
+        //
147
+        // See https://github.com/insideout10/wordlift-plugin/issues/693.
148
+        if ( extension_loaded( 'mbstring' ) ) {
149
+            mb_regex_encoding( 'UTF-8' );
150
+
151
+            $merged = array_reduce( (array) $_POST['wl_entity_id'], function ( $carry, $item ) {
152
+                return array_merge( $carry, mb_split( "\x{2063}", wp_unslash( $item ) ) );
153
+            }, array() );
154
+        } else {
155
+            $merged = array_reduce( (array) $_POST['wl_entity_id'], function ( $carry, $item ) {
156
+                return array_merge( $carry, preg_split( "/\x{2063}/u", wp_unslash( $item ) ) );
157
+            }, array() );
158
+        }
159
+
160
+        delete_term_meta( $term_id, self::META_KEY );
161
+        foreach ( array_unique( array_filter( $merged ) ) as $single ) {
162
+            add_term_meta( $term_id, self::META_KEY, $single );
163
+        }
164
+
165
+    }
166 166
 
167 167
 }
Please login to merge, or discard this patch.
Spacing   +33 added lines, -33 removed lines patch added patch discarded remove patch
@@ -30,8 +30,8 @@  discard block
 block discarded – undo
30 30
 	 */
31 31
 	public function __construct() {
32 32
 
33
-		add_action( 'registered_taxonomy', array( $this, 'add_action', ) );
34
-		add_action( 'edit_term', array( $this, 'edit_term', ), 10, 3 );
33
+		add_action('registered_taxonomy', array($this, 'add_action',));
34
+		add_action('edit_term', array($this, 'edit_term',), 10, 3);
35 35
 		$this->add_settings();
36 36
 
37 37
 	}
@@ -42,7 +42,7 @@  discard block
 block discarded – undo
42 42
 	 * @since 3.26.1
43 43
 	 */
44 44
 	public function add_settings() {
45
-		add_filter( 'wl_admin_settings', function ( $params ) {
45
+		add_filter('wl_admin_settings', function($params) {
46 46
 			$params['show_local_entities'] = true;
47 47
 			return $params;
48 48
 		} );
@@ -57,45 +57,45 @@  discard block
 block discarded – undo
57 57
 	 * @since 3.20.0
58 58
 	 *
59 59
 	 */
60
-	public function edit_form_fields( $tag, $taxonomy ) {
60
+	public function edit_form_fields($tag, $taxonomy) {
61 61
 
62 62
 	    // If disabled via filter, return;
63
-		if ( ! apply_filters( 'wl_feature__enable__term-entity', true ) ) {
63
+		if ( ! apply_filters('wl_feature__enable__term-entity', true)) {
64 64
 		    return;
65 65
         }
66 66
 
67 67
 		global $wp_version;
68 68
 
69 69
 		// Enqueue the JavaScript app.
70
-		if ( version_compare( $wp_version, '5.0', '>=' ) ) {
71
-			$term_asset = include plugin_dir_path( dirname( __FILE__ ) ) . 'js/dist/term.asset.php';
72
-			wp_enqueue_script( 'wl-term', plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/term.js', array_merge( array( 'wp-util' ), $term_asset['dependencies'] ), Wordlift::get_instance()->get_version(), true );
70
+		if (version_compare($wp_version, '5.0', '>=')) {
71
+			$term_asset = include plugin_dir_path(dirname(__FILE__)).'js/dist/term.asset.php';
72
+			wp_enqueue_script('wl-term', plugin_dir_url(dirname(__FILE__)).'js/dist/term.js', array_merge(array('wp-util'), $term_asset['dependencies']), Wordlift::get_instance()->get_version(), true);
73 73
 		} else {
74
-			wp_enqueue_script( 'wl-term', plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/term.full.js', array( 'wp-util' ), Wordlift::get_instance()->get_version(), true );
74
+			wp_enqueue_script('wl-term', plugin_dir_url(dirname(__FILE__)).'js/dist/term.full.js', array('wp-util'), Wordlift::get_instance()->get_version(), true);
75 75
 		}
76 76
 
77
-		wp_enqueue_style( 'wl-term', plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/term.css', array(), Wordlift::get_instance()->get_version() );
77
+		wp_enqueue_style('wl-term', plugin_dir_url(dirname(__FILE__)).'js/dist/term.css', array(), Wordlift::get_instance()->get_version());
78 78
 
79
-		$values = get_term_meta( $tag->term_id, self::META_KEY );
79
+		$values = get_term_meta($tag->term_id, self::META_KEY);
80 80
 
81 81
 		/**
82 82
 		 * @since 3.31.3
83 83
          * @see https://github.com/insideout10/wordlift-plugin/issues/1446
84 84
          * This field should be hidden by default
85 85
 		 */
86
-		if ( ! $values ) {
86
+		if ( ! $values) {
87 87
 		    return;
88 88
         }
89 89
 
90 90
 		?>
91 91
         <tr class="form-field term-name-wrap">
92
-            <th scope="row"><label for="wl-entity-id"><?php _ex( 'Entity', 'term entity', 'wordlift' ); ?></label></th>
92
+            <th scope="row"><label for="wl-entity-id"><?php _ex('Entity', 'term entity', 'wordlift'); ?></label></th>
93 93
             <td>
94
-				<?php foreach ( $values as $value ) { ?>
95
-                    <input type="text" name="wl_entity_id[]" value="<?php echo esc_attr( $value ); ?>"/>
94
+				<?php foreach ($values as $value) { ?>
95
+                    <input type="text" name="wl_entity_id[]" value="<?php echo esc_attr($value); ?>"/>
96 96
 				<?php } ?>
97 97
                 <div id="wl-term-entity-id"></div>
98
-                <p class="description"><?php _e( 'The entity bound to the term.', 'wordlift' ); ?></p>
98
+                <p class="description"><?php _e('The entity bound to the term.', 'wordlift'); ?></p>
99 99
             </td>
100 100
         </tr>
101 101
 		<?php
@@ -108,12 +108,12 @@  discard block
 block discarded – undo
108 108
 	 *
109 109
 	 * @param string $taxonomy The taxonomy name.
110 110
 	 */
111
-	public function add_action( $taxonomy ) {
111
+	public function add_action($taxonomy) {
112 112
 		/**
113 113
 		 * Filter wl_feature__enable__taxonomy_term_entity_mapping renamed to wl_feature__enable__term-entity.
114 114
          **/
115 115
 
116
-		add_action( "{$taxonomy}_edit_form_fields", array( $this, 'edit_form_fields' ), 10, 2 );
116
+		add_action("{$taxonomy}_edit_form_fields", array($this, 'edit_form_fields'), 10, 2);
117 117
 
118 118
 	}
119 119
 
@@ -126,16 +126,16 @@  discard block
 block discarded – undo
126 126
 	 * @param int    $tt_id The term taxonomy id.
127 127
 	 * @param string $taxonomy The taxonomy.
128 128
 	 */
129
-	public function edit_term( $term_id, $tt_id, $taxonomy ) {
129
+	public function edit_term($term_id, $tt_id, $taxonomy) {
130 130
 
131 131
 		// Bail if the action isn't related to the term currently being edited.
132
-		if ( ! isset( $_POST['tag_ID'] ) || $term_id !== (int) $_POST['tag_ID'] ) {
132
+		if ( ! isset($_POST['tag_ID']) || $term_id !== (int) $_POST['tag_ID']) {
133 133
 			return;
134 134
 		}
135 135
 
136 136
 		// Delete.
137
-		if ( ! isset( $_POST['wl_entity_id'] ) || ! is_array( $_POST['wl_entity_id'] ) || empty( $_POST['wl_entity_id'] ) ) {
138
-			delete_term_meta( $term_id, self::META_KEY );
137
+		if ( ! isset($_POST['wl_entity_id']) || ! is_array($_POST['wl_entity_id']) || empty($_POST['wl_entity_id'])) {
138
+			delete_term_meta($term_id, self::META_KEY);
139 139
 
140 140
 			return;
141 141
 		}
@@ -145,21 +145,21 @@  discard block
 block discarded – undo
145 145
 		// Only use mb_* functions when mbstring is available.
146 146
 		//
147 147
 		// See https://github.com/insideout10/wordlift-plugin/issues/693.
148
-		if ( extension_loaded( 'mbstring' ) ) {
149
-			mb_regex_encoding( 'UTF-8' );
148
+		if (extension_loaded('mbstring')) {
149
+			mb_regex_encoding('UTF-8');
150 150
 
151
-			$merged = array_reduce( (array) $_POST['wl_entity_id'], function ( $carry, $item ) {
152
-				return array_merge( $carry, mb_split( "\x{2063}", wp_unslash( $item ) ) );
153
-			}, array() );
151
+			$merged = array_reduce((array) $_POST['wl_entity_id'], function($carry, $item) {
152
+				return array_merge($carry, mb_split("\x{2063}", wp_unslash($item)));
153
+			}, array());
154 154
 		} else {
155
-			$merged = array_reduce( (array) $_POST['wl_entity_id'], function ( $carry, $item ) {
156
-				return array_merge( $carry, preg_split( "/\x{2063}/u", wp_unslash( $item ) ) );
157
-			}, array() );
155
+			$merged = array_reduce((array) $_POST['wl_entity_id'], function($carry, $item) {
156
+				return array_merge($carry, preg_split("/\x{2063}/u", wp_unslash($item)));
157
+			}, array());
158 158
 		}
159 159
 
160
-		delete_term_meta( $term_id, self::META_KEY );
161
-		foreach ( array_unique( array_filter( $merged ) ) as $single ) {
162
-			add_term_meta( $term_id, self::META_KEY, $single );
160
+		delete_term_meta($term_id, self::META_KEY);
161
+		foreach (array_unique(array_filter($merged)) as $single) {
162
+			add_term_meta($term_id, self::META_KEY, $single);
163 163
 		}
164 164
 
165 165
 	}
Please login to merge, or discard this patch.
src/admin/class-wordlift-entity-type-settings.php 2 patches
Indentation   +202 added lines, -202 removed lines patch added patch discarded remove patch
@@ -13,7 +13,7 @@  discard block
 block discarded – undo
13 13
  */
14 14
 
15 15
 if ( ! defined( 'ABSPATH' ) ) {
16
-	exit;
16
+    exit;
17 17
 }
18 18
 
19 19
 /**
@@ -30,217 +30,217 @@  discard block
 block discarded – undo
30 30
  */
31 31
 class Wordlift_Admin_Entity_Type_Settings {
32 32
 
33
-	/**
34
-	 * A {@link Wordlift_Log_Service} instance.
35
-	 *
36
-	 * @since  3.14.0
37
-	 * @access private
38
-	 * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
39
-	 */
40
-	private $log;
41
-
42
-	/**
43
-	 * Create a {@link Wordlift_Admin_Entity_Type_Settings} instance.
44
-	 *
45
-	 * @since 3.14.0
46
-	 */
47
-	public function __construct() {
48
-
49
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Admin_Entity_Type_Settings' );
50
-
51
-	}
52
-
53
-	/**
54
-	 * Handle menu registration.
55
-	 *
56
-	 * The registration is required, although we do not want to actually to add
57
-	 * an item to the menu, in order to "whitelist" the access to the settings page in
58
-	 * the admin.
59
-	 *
60
-	 * @since 3.11.0
61
-	 */
62
-	public function admin_menu() {
63
-
64
-		/*
33
+    /**
34
+     * A {@link Wordlift_Log_Service} instance.
35
+     *
36
+     * @since  3.14.0
37
+     * @access private
38
+     * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
39
+     */
40
+    private $log;
41
+
42
+    /**
43
+     * Create a {@link Wordlift_Admin_Entity_Type_Settings} instance.
44
+     *
45
+     * @since 3.14.0
46
+     */
47
+    public function __construct() {
48
+
49
+        $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Admin_Entity_Type_Settings' );
50
+
51
+    }
52
+
53
+    /**
54
+     * Handle menu registration.
55
+     *
56
+     * The registration is required, although we do not want to actually to add
57
+     * an item to the menu, in order to "whitelist" the access to the settings page in
58
+     * the admin.
59
+     *
60
+     * @since 3.11.0
61
+     */
62
+    public function admin_menu() {
63
+
64
+        /*
65 65
 		 * Before anything else check if an settings form was submitted.
66 66
 		 * This has to be done before any output happens in order to be able to
67 67
 		 * display proper "die" error messages and redirect.
68 68
 		 */
69
-		if ( isset( $_GET['page'] ) && ( 'wl_entity_type_settings' === $_GET['page'] ) ) {
70
-
71
-			// Validate inputs. Do not return on invalid parameters or capabilities.
72
-			$this->validate_proper_term();
73
-
74
-			// If proper form submission, handle it and redirect back to the settings page.
75
-			if ( isset( $_POST['action'] ) && ( 'wl_edit_entity_type_term' === $_POST['action'] ) ) {
76
-				$this->handle_form_submission();
77
-			}
78
-			/**
79
-			 * Filter: wl_feature__enable__notices.
80
-			 *
81
-			 * @param bool whether the notices needs to be enabled or not.
82
-			 *
83
-			 * @return bool
84
-			 * @since 3.27.6
85
-			 */
86
-			if ( apply_filters( 'wl_feature__enable__notices', true ) ) {
87
-				// Register admin notices handler.
88
-				add_action( 'admin_notices', array( $this, 'admin_notice' ) );
89
-			}
90
-
91
-		}
92
-
93
-		/*
69
+        if ( isset( $_GET['page'] ) && ( 'wl_entity_type_settings' === $_GET['page'] ) ) {
70
+
71
+            // Validate inputs. Do not return on invalid parameters or capabilities.
72
+            $this->validate_proper_term();
73
+
74
+            // If proper form submission, handle it and redirect back to the settings page.
75
+            if ( isset( $_POST['action'] ) && ( 'wl_edit_entity_type_term' === $_POST['action'] ) ) {
76
+                $this->handle_form_submission();
77
+            }
78
+            /**
79
+             * Filter: wl_feature__enable__notices.
80
+             *
81
+             * @param bool whether the notices needs to be enabled or not.
82
+             *
83
+             * @return bool
84
+             * @since 3.27.6
85
+             */
86
+            if ( apply_filters( 'wl_feature__enable__notices', true ) ) {
87
+                // Register admin notices handler.
88
+                add_action( 'admin_notices', array( $this, 'admin_notice' ) );
89
+            }
90
+
91
+        }
92
+
93
+        /*
94 94
 		 * Use a null parent slug to prevent the menu from actually appearing
95 95
 		 * in the admin menu.
96 96
 		 */
97
-		// @todo: use the new {@link Wordlift_Admin_Page}.
98
-		add_submenu_page(
99
-			null,
100
-			__( 'Edit Entity term', 'wordlift' ),
101
-			__( 'Edit Entity term', 'wordlift' ),
102
-			'manage_options',
103
-			'wl_entity_type_settings',
104
-			array( $this, 'render' )
105
-		);
106
-	}
107
-
108
-	/**
109
-	 * Output admin notices if needed, based on the message url parameter.
110
-	 * A value of 1 indicates that a successful save was done.
111
-	 *
112
-	 * @since 3.11.0
113
-	 */
114
-	function admin_notice() {
115
-		if ( isset( $_GET['message'] ) && ( '1' === $_GET['message'] ) ) {
116
-			?>
97
+        // @todo: use the new {@link Wordlift_Admin_Page}.
98
+        add_submenu_page(
99
+            null,
100
+            __( 'Edit Entity term', 'wordlift' ),
101
+            __( 'Edit Entity term', 'wordlift' ),
102
+            'manage_options',
103
+            'wl_entity_type_settings',
104
+            array( $this, 'render' )
105
+        );
106
+    }
107
+
108
+    /**
109
+     * Output admin notices if needed, based on the message url parameter.
110
+     * A value of 1 indicates that a successful save was done.
111
+     *
112
+     * @since 3.11.0
113
+     */
114
+    function admin_notice() {
115
+        if ( isset( $_GET['message'] ) && ( '1' === $_GET['message'] ) ) {
116
+            ?>
117 117
             <div class="notice notice-success is-dismissible">
118 118
                 <p><?php esc_html_e( 'Settings saved', 'wordlift' ) ?></p>
119 119
             </div>
120 120
 			<?php
121
-		}
122
-	}
123
-
124
-	/**
125
-	 * Validate the existence of the entity type indicated by the tag_ID url
126
-	 * parameter before doing any processing. Done before any output to mimic
127
-	 * the way WordPress handles same situation with "normal" term editing screens.
128
-	 *
129
-	 * @since 3.11.0
130
-	 */
131
-	function validate_proper_term() {
132
-
133
-		// Validate capabilities.
134
-		if ( ! current_user_can( 'manage_options' ) ) {
135
-			wp_die(
136
-				'<h1>' . __( 'Cheatin&#8217; uh?' ) . '</h1>' .
137
-				'<p>' . __( 'Sorry, you are not allowed to edit this item.' ) . '</p>',
138
-				403
139
-			);
140
-		}
141
-
142
-		// Get the term id and the actual term.
143
-		$term_id = (int) $_REQUEST['tag_ID'];
144
-
145
-		if ( ! term_exists( $term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ) ) {
146
-			wp_die( __( 'You attempted to edit an entity type term that doesn&#8217;t exist.', 'wordlift' ) );
147
-		}
148
-
149
-	}
150
-
151
-	/**
152
-	 * Handle the form submission of the settings form. On successful
153
-	 * handling redirect tp the setting edit page.
154
-	 *
155
-	 * @since 3.11.0
156
-	 */
157
-	function handle_form_submission() {
158
-
159
-		$term_id = (int) $_POST['tag_ID'];
160
-
161
-		// Check the nonce.
162
-		check_admin_referer( 'update-entity_type_term_' . $term_id );
163
-
164
-		$term = get_term( $term_id, 'wl_entity_type' );
165
-
166
-		$this->set_setting(
167
-			$term_id,
168
-			trim( wp_unslash( (string) $_POST['title'] ) ),
169
-			wp_unslash( (string) $_POST['description'] )
170
-		);
171
-
172
-		// Redirect back to the term settings page and indicate a save was done.
173
-		$url = admin_url( "admin.php?page=wl_entity_type_settings&tag_ID=$term->term_id&message=1" );
174
-
175
-		wp_redirect( $url );
176
-		exit;
177
-
178
-	}
179
-
180
-	/**
181
-	 * Render the settings page for the term.
182
-	 *
183
-	 * Access and parameter validity is assumed to be done earlier.
184
-	 *
185
-	 * @since 3.11.0
186
-	 */
187
-	function render() {
188
-
189
-		// Set variables used by the partial
190
-		$term_id  = absint( $_REQUEST['tag_ID'] );
191
-		$settings = $this->get_setting( $term_id );
192
-
193
-		include plugin_dir_path( dirname( __FILE__ ) ) . 'admin/partials/wordlift-admin-entity-type-settings.php';
194
-
195
-	}
196
-
197
-	/**
198
-	 * Store the entity type term settings in the DB
199
-	 *
200
-	 * @param integer $term_id The ID of the entity type term
201
-	 * @param string $title The override for the terms title.
202
-	 * @param string $description The override for the terms description.
203
-	 *
204
-	 * @since 3.11.0
205
-	 *
206
-	 */
207
-	function set_setting( $term_id, $title, $description ) {
208
-
209
-		$settings             = get_option( 'wl_entity_type_settings', array() );
210
-		$settings[ $term_id ] = array(
211
-			'title'       => $title,
212
-			'description' => $description,
213
-		);
214
-		update_option( 'wl_entity_type_settings', $settings );
215
-
216
-	}
217
-
218
-	/**
219
-	 * Retrieve the entity type term settings from the DB
220
-	 *
221
-	 * @param integer $term_id The ID of the entity type term
222
-	 *
223
-	 * @return    null|array {
224
-	 *                null is returned when there are no settings otherwise
225
-	 *                an array is returned with following fields
226
-	 *
227
-	 * @type    string    title    The overriding title for the term
228
-	 * @type    string    description    The overriding description for the term
229
-	 *            }
230
-	 * @since 3.11.0
231
-	 *
232
-	 */
233
-	function get_setting( $term_id ) {
234
-
235
-		$settings = get_option( 'wl_entity_type_settings', array() );
236
-
237
-		if ( isset( $settings[ $term_id ] ) ) {
238
-			return $settings[ $term_id ];
239
-		}
240
-
241
-		$this->log->warn( "No settings found for term id $term_id." );
242
-
243
-		return null;
244
-	}
121
+        }
122
+    }
123
+
124
+    /**
125
+     * Validate the existence of the entity type indicated by the tag_ID url
126
+     * parameter before doing any processing. Done before any output to mimic
127
+     * the way WordPress handles same situation with "normal" term editing screens.
128
+     *
129
+     * @since 3.11.0
130
+     */
131
+    function validate_proper_term() {
132
+
133
+        // Validate capabilities.
134
+        if ( ! current_user_can( 'manage_options' ) ) {
135
+            wp_die(
136
+                '<h1>' . __( 'Cheatin&#8217; uh?' ) . '</h1>' .
137
+                '<p>' . __( 'Sorry, you are not allowed to edit this item.' ) . '</p>',
138
+                403
139
+            );
140
+        }
141
+
142
+        // Get the term id and the actual term.
143
+        $term_id = (int) $_REQUEST['tag_ID'];
144
+
145
+        if ( ! term_exists( $term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ) ) {
146
+            wp_die( __( 'You attempted to edit an entity type term that doesn&#8217;t exist.', 'wordlift' ) );
147
+        }
148
+
149
+    }
150
+
151
+    /**
152
+     * Handle the form submission of the settings form. On successful
153
+     * handling redirect tp the setting edit page.
154
+     *
155
+     * @since 3.11.0
156
+     */
157
+    function handle_form_submission() {
158
+
159
+        $term_id = (int) $_POST['tag_ID'];
160
+
161
+        // Check the nonce.
162
+        check_admin_referer( 'update-entity_type_term_' . $term_id );
163
+
164
+        $term = get_term( $term_id, 'wl_entity_type' );
165
+
166
+        $this->set_setting(
167
+            $term_id,
168
+            trim( wp_unslash( (string) $_POST['title'] ) ),
169
+            wp_unslash( (string) $_POST['description'] )
170
+        );
171
+
172
+        // Redirect back to the term settings page and indicate a save was done.
173
+        $url = admin_url( "admin.php?page=wl_entity_type_settings&tag_ID=$term->term_id&message=1" );
174
+
175
+        wp_redirect( $url );
176
+        exit;
177
+
178
+    }
179
+
180
+    /**
181
+     * Render the settings page for the term.
182
+     *
183
+     * Access and parameter validity is assumed to be done earlier.
184
+     *
185
+     * @since 3.11.0
186
+     */
187
+    function render() {
188
+
189
+        // Set variables used by the partial
190
+        $term_id  = absint( $_REQUEST['tag_ID'] );
191
+        $settings = $this->get_setting( $term_id );
192
+
193
+        include plugin_dir_path( dirname( __FILE__ ) ) . 'admin/partials/wordlift-admin-entity-type-settings.php';
194
+
195
+    }
196
+
197
+    /**
198
+     * Store the entity type term settings in the DB
199
+     *
200
+     * @param integer $term_id The ID of the entity type term
201
+     * @param string $title The override for the terms title.
202
+     * @param string $description The override for the terms description.
203
+     *
204
+     * @since 3.11.0
205
+     *
206
+     */
207
+    function set_setting( $term_id, $title, $description ) {
208
+
209
+        $settings             = get_option( 'wl_entity_type_settings', array() );
210
+        $settings[ $term_id ] = array(
211
+            'title'       => $title,
212
+            'description' => $description,
213
+        );
214
+        update_option( 'wl_entity_type_settings', $settings );
215
+
216
+    }
217
+
218
+    /**
219
+     * Retrieve the entity type term settings from the DB
220
+     *
221
+     * @param integer $term_id The ID of the entity type term
222
+     *
223
+     * @return    null|array {
224
+     *                null is returned when there are no settings otherwise
225
+     *                an array is returned with following fields
226
+     *
227
+     * @type    string    title    The overriding title for the term
228
+     * @type    string    description    The overriding description for the term
229
+     *            }
230
+     * @since 3.11.0
231
+     *
232
+     */
233
+    function get_setting( $term_id ) {
234
+
235
+        $settings = get_option( 'wl_entity_type_settings', array() );
236
+
237
+        if ( isset( $settings[ $term_id ] ) ) {
238
+            return $settings[ $term_id ];
239
+        }
240
+
241
+        $this->log->warn( "No settings found for term id $term_id." );
242
+
243
+        return null;
244
+    }
245 245
 
246 246
 }
Please login to merge, or discard this patch.
Spacing   +34 added lines, -34 removed lines patch added patch discarded remove patch
@@ -12,7 +12,7 @@  discard block
 block discarded – undo
12 12
  * @since      3.11.0
13 13
  */
14 14
 
15
-if ( ! defined( 'ABSPATH' ) ) {
15
+if ( ! defined('ABSPATH')) {
16 16
 	exit;
17 17
 }
18 18
 
@@ -46,7 +46,7 @@  discard block
 block discarded – undo
46 46
 	 */
47 47
 	public function __construct() {
48 48
 
49
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Admin_Entity_Type_Settings' );
49
+		$this->log = Wordlift_Log_Service::get_logger('Wordlift_Admin_Entity_Type_Settings');
50 50
 
51 51
 	}
52 52
 
@@ -66,13 +66,13 @@  discard block
 block discarded – undo
66 66
 		 * This has to be done before any output happens in order to be able to
67 67
 		 * display proper "die" error messages and redirect.
68 68
 		 */
69
-		if ( isset( $_GET['page'] ) && ( 'wl_entity_type_settings' === $_GET['page'] ) ) {
69
+		if (isset($_GET['page']) && ('wl_entity_type_settings' === $_GET['page'])) {
70 70
 
71 71
 			// Validate inputs. Do not return on invalid parameters or capabilities.
72 72
 			$this->validate_proper_term();
73 73
 
74 74
 			// If proper form submission, handle it and redirect back to the settings page.
75
-			if ( isset( $_POST['action'] ) && ( 'wl_edit_entity_type_term' === $_POST['action'] ) ) {
75
+			if (isset($_POST['action']) && ('wl_edit_entity_type_term' === $_POST['action'])) {
76 76
 				$this->handle_form_submission();
77 77
 			}
78 78
 			/**
@@ -83,9 +83,9 @@  discard block
 block discarded – undo
83 83
 			 * @return bool
84 84
 			 * @since 3.27.6
85 85
 			 */
86
-			if ( apply_filters( 'wl_feature__enable__notices', true ) ) {
86
+			if (apply_filters('wl_feature__enable__notices', true)) {
87 87
 				// Register admin notices handler.
88
-				add_action( 'admin_notices', array( $this, 'admin_notice' ) );
88
+				add_action('admin_notices', array($this, 'admin_notice'));
89 89
 			}
90 90
 
91 91
 		}
@@ -97,11 +97,11 @@  discard block
 block discarded – undo
97 97
 		// @todo: use the new {@link Wordlift_Admin_Page}.
98 98
 		add_submenu_page(
99 99
 			null,
100
-			__( 'Edit Entity term', 'wordlift' ),
101
-			__( 'Edit Entity term', 'wordlift' ),
100
+			__('Edit Entity term', 'wordlift'),
101
+			__('Edit Entity term', 'wordlift'),
102 102
 			'manage_options',
103 103
 			'wl_entity_type_settings',
104
-			array( $this, 'render' )
104
+			array($this, 'render')
105 105
 		);
106 106
 	}
107 107
 
@@ -112,10 +112,10 @@  discard block
 block discarded – undo
112 112
 	 * @since 3.11.0
113 113
 	 */
114 114
 	function admin_notice() {
115
-		if ( isset( $_GET['message'] ) && ( '1' === $_GET['message'] ) ) {
115
+		if (isset($_GET['message']) && ('1' === $_GET['message'])) {
116 116
 			?>
117 117
             <div class="notice notice-success is-dismissible">
118
-                <p><?php esc_html_e( 'Settings saved', 'wordlift' ) ?></p>
118
+                <p><?php esc_html_e('Settings saved', 'wordlift') ?></p>
119 119
             </div>
120 120
 			<?php
121 121
 		}
@@ -131,10 +131,10 @@  discard block
 block discarded – undo
131 131
 	function validate_proper_term() {
132 132
 
133 133
 		// Validate capabilities.
134
-		if ( ! current_user_can( 'manage_options' ) ) {
134
+		if ( ! current_user_can('manage_options')) {
135 135
 			wp_die(
136
-				'<h1>' . __( 'Cheatin&#8217; uh?' ) . '</h1>' .
137
-				'<p>' . __( 'Sorry, you are not allowed to edit this item.' ) . '</p>',
136
+				'<h1>'.__('Cheatin&#8217; uh?').'</h1>'.
137
+				'<p>'.__('Sorry, you are not allowed to edit this item.').'</p>',
138 138
 				403
139 139
 			);
140 140
 		}
@@ -142,8 +142,8 @@  discard block
 block discarded – undo
142 142
 		// Get the term id and the actual term.
143 143
 		$term_id = (int) $_REQUEST['tag_ID'];
144 144
 
145
-		if ( ! term_exists( $term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ) ) {
146
-			wp_die( __( 'You attempted to edit an entity type term that doesn&#8217;t exist.', 'wordlift' ) );
145
+		if ( ! term_exists($term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME)) {
146
+			wp_die(__('You attempted to edit an entity type term that doesn&#8217;t exist.', 'wordlift'));
147 147
 		}
148 148
 
149 149
 	}
@@ -159,20 +159,20 @@  discard block
 block discarded – undo
159 159
 		$term_id = (int) $_POST['tag_ID'];
160 160
 
161 161
 		// Check the nonce.
162
-		check_admin_referer( 'update-entity_type_term_' . $term_id );
162
+		check_admin_referer('update-entity_type_term_'.$term_id);
163 163
 
164
-		$term = get_term( $term_id, 'wl_entity_type' );
164
+		$term = get_term($term_id, 'wl_entity_type');
165 165
 
166 166
 		$this->set_setting(
167 167
 			$term_id,
168
-			trim( wp_unslash( (string) $_POST['title'] ) ),
169
-			wp_unslash( (string) $_POST['description'] )
168
+			trim(wp_unslash((string) $_POST['title'])),
169
+			wp_unslash((string) $_POST['description'])
170 170
 		);
171 171
 
172 172
 		// Redirect back to the term settings page and indicate a save was done.
173
-		$url = admin_url( "admin.php?page=wl_entity_type_settings&tag_ID=$term->term_id&message=1" );
173
+		$url = admin_url("admin.php?page=wl_entity_type_settings&tag_ID=$term->term_id&message=1");
174 174
 
175
-		wp_redirect( $url );
175
+		wp_redirect($url);
176 176
 		exit;
177 177
 
178 178
 	}
@@ -187,10 +187,10 @@  discard block
 block discarded – undo
187 187
 	function render() {
188 188
 
189 189
 		// Set variables used by the partial
190
-		$term_id  = absint( $_REQUEST['tag_ID'] );
191
-		$settings = $this->get_setting( $term_id );
190
+		$term_id  = absint($_REQUEST['tag_ID']);
191
+		$settings = $this->get_setting($term_id);
192 192
 
193
-		include plugin_dir_path( dirname( __FILE__ ) ) . 'admin/partials/wordlift-admin-entity-type-settings.php';
193
+		include plugin_dir_path(dirname(__FILE__)).'admin/partials/wordlift-admin-entity-type-settings.php';
194 194
 
195 195
 	}
196 196
 
@@ -204,14 +204,14 @@  discard block
 block discarded – undo
204 204
 	 * @since 3.11.0
205 205
 	 *
206 206
 	 */
207
-	function set_setting( $term_id, $title, $description ) {
207
+	function set_setting($term_id, $title, $description) {
208 208
 
209
-		$settings             = get_option( 'wl_entity_type_settings', array() );
210
-		$settings[ $term_id ] = array(
209
+		$settings             = get_option('wl_entity_type_settings', array());
210
+		$settings[$term_id] = array(
211 211
 			'title'       => $title,
212 212
 			'description' => $description,
213 213
 		);
214
-		update_option( 'wl_entity_type_settings', $settings );
214
+		update_option('wl_entity_type_settings', $settings);
215 215
 
216 216
 	}
217 217
 
@@ -230,15 +230,15 @@  discard block
 block discarded – undo
230 230
 	 * @since 3.11.0
231 231
 	 *
232 232
 	 */
233
-	function get_setting( $term_id ) {
233
+	function get_setting($term_id) {
234 234
 
235
-		$settings = get_option( 'wl_entity_type_settings', array() );
235
+		$settings = get_option('wl_entity_type_settings', array());
236 236
 
237
-		if ( isset( $settings[ $term_id ] ) ) {
238
-			return $settings[ $term_id ];
237
+		if (isset($settings[$term_id])) {
238
+			return $settings[$term_id];
239 239
 		}
240 240
 
241
-		$this->log->warn( "No settings found for term id $term_id." );
241
+		$this->log->warn("No settings found for term id $term_id.");
242 242
 
243 243
 		return null;
244 244
 	}
Please login to merge, or discard this patch.
src/shortcodes/wordlift_shortcode_navigator.php 2 patches
Indentation   +268 added lines, -268 removed lines patch added patch discarded remove patch
@@ -20,32 +20,32 @@  discard block
 block discarded – undo
20 20
  */
21 21
 function wl_shortcode_navigator_data() {
22 22
 
23
-	// Create the cache key.
24
-	$cache_key_params = $_REQUEST;
25
-	unset( $cache_key_params['uniqid'] );
26
-	$cache_key = array( 'request_params' => $cache_key_params );
23
+    // Create the cache key.
24
+    $cache_key_params = $_REQUEST;
25
+    unset( $cache_key_params['uniqid'] );
26
+    $cache_key = array( 'request_params' => $cache_key_params );
27 27
 
28
-	// Create the TTL cache and try to get the results.
29
-	$cache         = new Ttl_Cache( "navigator", 8 * 60 * 60 ); // 8 hours.
30
-	$cache_results = $cache->get( $cache_key );
28
+    // Create the TTL cache and try to get the results.
29
+    $cache         = new Ttl_Cache( "navigator", 8 * 60 * 60 ); // 8 hours.
30
+    $cache_results = $cache->get( $cache_key );
31 31
 
32
-	// So that the endpoint can be used remotely
33
-	header( 'Access-Control-Allow-Origin: *' );
32
+    // So that the endpoint can be used remotely
33
+    header( 'Access-Control-Allow-Origin: *' );
34 34
 
35
-	if ( isset( $cache_results ) ) {
36
-		header( 'X-WordLift-Cache: HIT' );
35
+    if ( isset( $cache_results ) ) {
36
+        header( 'X-WordLift-Cache: HIT' );
37 37
 
38
-		return $cache_results;
39
-	}
38
+        return $cache_results;
39
+    }
40 40
 
41
-	header( 'X-WordLift-Cache: MISS' );
41
+    header( 'X-WordLift-Cache: MISS' );
42 42
 
43
-	$results = _wl_navigator_get_data();
43
+    $results = _wl_navigator_get_data();
44 44
 
45
-	// Put the result before sending the json to the client, since sending the json will terminate us.
46
-	$cache->put( $cache_key, $results );
45
+    // Put the result before sending the json to the client, since sending the json will terminate us.
46
+    $cache->put( $cache_key, $results );
47 47
 
48
-	return $results;
48
+    return $results;
49 49
 }
50 50
 
51 51
 /**
@@ -60,240 +60,240 @@  discard block
 block discarded – undo
60 60
  */
61 61
 function wl_network_navigator_wp_json( $request ) {
62 62
 
63
-	// Create the cache key.
64
-	$cache_key_params = $_REQUEST;
65
-	unset( $cache_key_params['uniqid'] );
66
-	$cache_key = array( 'request_params' => $cache_key_params );
63
+    // Create the cache key.
64
+    $cache_key_params = $_REQUEST;
65
+    unset( $cache_key_params['uniqid'] );
66
+    $cache_key = array( 'request_params' => $cache_key_params );
67 67
 
68
-	// Create the TTL cache and try to get the results.
69
-	$cache         = new Ttl_Cache( "network-navigator", 8 * 60 * 60 ); // 8 hours.
70
-	$cache_results = $cache->get( $cache_key );
68
+    // Create the TTL cache and try to get the results.
69
+    $cache         = new Ttl_Cache( "network-navigator", 8 * 60 * 60 ); // 8 hours.
70
+    $cache_results = $cache->get( $cache_key );
71 71
 
72
-	if ( isset( $cache_results ) ) {
73
-		header( 'X-WordLift-Cache: HIT' );
72
+    if ( isset( $cache_results ) ) {
73
+        header( 'X-WordLift-Cache: HIT' );
74 74
 
75
-		return $cache_results;
76
-	}
75
+        return $cache_results;
76
+    }
77 77
 
78
-	header( 'X-WordLift-Cache: MISS' );
78
+    header( 'X-WordLift-Cache: MISS' );
79 79
 
80
-	$results = _wl_network_navigator_get_data( $request );
80
+    $results = _wl_network_navigator_get_data( $request );
81 81
 
82
-	// Put the result before sending the json to the client, since sending the json will terminate us.
83
-	$cache->put( $cache_key, $results );
82
+    // Put the result before sending the json to the client, since sending the json will terminate us.
83
+    $cache->put( $cache_key, $results );
84 84
 
85
-	return $results;
85
+    return $results;
86 86
 
87 87
 }
88 88
 
89 89
 function _wl_navigator_get_data() {
90 90
 
91
-	// Post ID must be defined
92
-	if ( ! isset( $_GET['post_id'] ) ) {
93
-		wp_send_json_error( 'No post_id given' );
94
-
95
-		return array();
96
-	}
97
-
98
-	// Post ID must be defined
99
-	if ( ! isset( $_GET['uniqid'] ) ) {
100
-		wp_send_json_error( 'No uniqid given' );
101
-
102
-		return array();
103
-	}
104
-
105
-	// Limit the results (defaults to 4)
106
-	$navigator_length    = isset( $_GET['limit'] ) ? intval( $_GET['limit'] ) : 4;
107
-	$navigator_offset    = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0;
108
-	$order_by            = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC';
109
-	$post_types          = isset( $_GET['post_types'] ) ? (string) $_GET['post_types'] : '';
110
-	$post_types          = explode( ',', $post_types );
111
-	$existing_post_types = get_post_types();
112
-	$post_types          = array_values( array_intersect( $existing_post_types, $post_types ) );
113
-	$current_post_id     = (int) $_GET['post_id'];
114
-	$current_post        = get_post( $current_post_id );
115
-
116
-	$navigator_id = (string) $_GET['uniqid'];
117
-
118
-	// Post ID has to match an existing item
119
-	if ( null === $current_post ) {
120
-		wp_send_json_error( 'No valid post_id given' );
121
-
122
-		return array();
123
-	}
124
-
125
-	// Determine navigator type and call respective _get_results
126
-	if ( get_post_type( $current_post_id ) === Wordlift_Entity_Service::TYPE_NAME ) {
127
-
128
-		$referencing_posts = Navigator_Data::entity_navigator_get_results( $current_post_id, array(
129
-			'ID',
130
-			'post_title',
131
-		), $order_by, $navigator_length, $navigator_offset, $post_types );
132
-	} else {
133
-		$referencing_posts = Navigator_Data::post_navigator_get_results( $current_post_id, array(
134
-			'ID',
135
-			'post_title',
136
-		), $order_by, $navigator_length, $navigator_offset, $post_types );
137
-
138
-	}
139
-
140
-	// loop over them and take the first one which is not already in the $related_posts
141
-	$results = array();
142
-	foreach ( $referencing_posts as $referencing_post ) {
143
-		$serialized_entity = wl_serialize_entity( $referencing_post->entity_id );
144
-
145
-		/**
146
-		 * Use the thumbnail.
147
-		 *
148
-		 * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue.
149
-		 * @see https://github.com/insideout10/wordlift-plugin/issues/837
150
-		 *
151
-		 * @since 3.19.3 We're using the medium size image.
152
-		 */
153
-		$thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' );
154
-
155
-		$result = array(
156
-			'post'   => array(
157
-				'id'        => $referencing_post->ID,
158
-				'permalink' => get_permalink( $referencing_post->ID ),
159
-				'title'     => html_entity_decode( $referencing_post->post_title, ENT_QUOTES, 'UTF-8' ),
160
-				'thumbnail' => $thumbnail,
161
-				'srcset'    => Srcset_Util::get_srcset( $referencing_post->ID, Srcset_Util::NAVIGATOR_WIDGET )
162
-			),
163
-			'entity' => array(
164
-				'id'        => $referencing_post->entity_id,
165
-				'label'     => $serialized_entity['label'],
166
-				'mainType'  => $serialized_entity['mainType'],
167
-				'permalink' => get_permalink( $referencing_post->entity_id ),
168
-			),
169
-		);
170
-
171
-		$results[] = $result;
172
-	}
173
-
174
-
175
-	if ( count( $results ) < $navigator_length ) {
176
-		$results = apply_filters( 'wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length );
177
-	}
178
-
179
-	// Add filler posts if needed
180
-	$filler_count = $navigator_length - count( $results );
181
-	if ( $filler_count > 0 ) {
182
-		$referencing_post_ids = array_map( function ( $p ) {
183
-			return $p->ID;
184
-		}, $referencing_posts );
185
-		/**
186
-		 * @since 3.27.8
187
-		 * Filler posts are fetched using this util.
188
-		 */
189
-		$filler_posts_util       = new Filler_Posts_Util( $current_post_id, $post_types );
190
-		$post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids );
191
-		$filler_posts            = $filler_posts_util->get_filler_response( $filler_count, $post_ids_to_be_excluded );
192
-		$results                 = array_merge( $results, $filler_posts );
193
-	}
194
-
195
-	// Apply filters after fillers are added
196
-	foreach ( $results as $result_index => $result ) {
197
-		$results[ $result_index ]['post']   = apply_filters( 'wl_navigator_data_post', $result['post'], intval( $result['post']['id'] ), $navigator_id );
198
-		$results[ $result_index ]['entity'] = apply_filters( 'wl_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id );
199
-	}
200
-
201
-	$results = apply_filters( 'wl_navigator_results', $results, $navigator_id );
202
-
203
-	return $results;
91
+    // Post ID must be defined
92
+    if ( ! isset( $_GET['post_id'] ) ) {
93
+        wp_send_json_error( 'No post_id given' );
94
+
95
+        return array();
96
+    }
97
+
98
+    // Post ID must be defined
99
+    if ( ! isset( $_GET['uniqid'] ) ) {
100
+        wp_send_json_error( 'No uniqid given' );
101
+
102
+        return array();
103
+    }
104
+
105
+    // Limit the results (defaults to 4)
106
+    $navigator_length    = isset( $_GET['limit'] ) ? intval( $_GET['limit'] ) : 4;
107
+    $navigator_offset    = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0;
108
+    $order_by            = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC';
109
+    $post_types          = isset( $_GET['post_types'] ) ? (string) $_GET['post_types'] : '';
110
+    $post_types          = explode( ',', $post_types );
111
+    $existing_post_types = get_post_types();
112
+    $post_types          = array_values( array_intersect( $existing_post_types, $post_types ) );
113
+    $current_post_id     = (int) $_GET['post_id'];
114
+    $current_post        = get_post( $current_post_id );
115
+
116
+    $navigator_id = (string) $_GET['uniqid'];
117
+
118
+    // Post ID has to match an existing item
119
+    if ( null === $current_post ) {
120
+        wp_send_json_error( 'No valid post_id given' );
121
+
122
+        return array();
123
+    }
124
+
125
+    // Determine navigator type and call respective _get_results
126
+    if ( get_post_type( $current_post_id ) === Wordlift_Entity_Service::TYPE_NAME ) {
127
+
128
+        $referencing_posts = Navigator_Data::entity_navigator_get_results( $current_post_id, array(
129
+            'ID',
130
+            'post_title',
131
+        ), $order_by, $navigator_length, $navigator_offset, $post_types );
132
+    } else {
133
+        $referencing_posts = Navigator_Data::post_navigator_get_results( $current_post_id, array(
134
+            'ID',
135
+            'post_title',
136
+        ), $order_by, $navigator_length, $navigator_offset, $post_types );
137
+
138
+    }
139
+
140
+    // loop over them and take the first one which is not already in the $related_posts
141
+    $results = array();
142
+    foreach ( $referencing_posts as $referencing_post ) {
143
+        $serialized_entity = wl_serialize_entity( $referencing_post->entity_id );
144
+
145
+        /**
146
+         * Use the thumbnail.
147
+         *
148
+         * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue.
149
+         * @see https://github.com/insideout10/wordlift-plugin/issues/837
150
+         *
151
+         * @since 3.19.3 We're using the medium size image.
152
+         */
153
+        $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' );
154
+
155
+        $result = array(
156
+            'post'   => array(
157
+                'id'        => $referencing_post->ID,
158
+                'permalink' => get_permalink( $referencing_post->ID ),
159
+                'title'     => html_entity_decode( $referencing_post->post_title, ENT_QUOTES, 'UTF-8' ),
160
+                'thumbnail' => $thumbnail,
161
+                'srcset'    => Srcset_Util::get_srcset( $referencing_post->ID, Srcset_Util::NAVIGATOR_WIDGET )
162
+            ),
163
+            'entity' => array(
164
+                'id'        => $referencing_post->entity_id,
165
+                'label'     => $serialized_entity['label'],
166
+                'mainType'  => $serialized_entity['mainType'],
167
+                'permalink' => get_permalink( $referencing_post->entity_id ),
168
+            ),
169
+        );
170
+
171
+        $results[] = $result;
172
+    }
173
+
174
+
175
+    if ( count( $results ) < $navigator_length ) {
176
+        $results = apply_filters( 'wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length );
177
+    }
178
+
179
+    // Add filler posts if needed
180
+    $filler_count = $navigator_length - count( $results );
181
+    if ( $filler_count > 0 ) {
182
+        $referencing_post_ids = array_map( function ( $p ) {
183
+            return $p->ID;
184
+        }, $referencing_posts );
185
+        /**
186
+         * @since 3.27.8
187
+         * Filler posts are fetched using this util.
188
+         */
189
+        $filler_posts_util       = new Filler_Posts_Util( $current_post_id, $post_types );
190
+        $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids );
191
+        $filler_posts            = $filler_posts_util->get_filler_response( $filler_count, $post_ids_to_be_excluded );
192
+        $results                 = array_merge( $results, $filler_posts );
193
+    }
194
+
195
+    // Apply filters after fillers are added
196
+    foreach ( $results as $result_index => $result ) {
197
+        $results[ $result_index ]['post']   = apply_filters( 'wl_navigator_data_post', $result['post'], intval( $result['post']['id'] ), $navigator_id );
198
+        $results[ $result_index ]['entity'] = apply_filters( 'wl_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id );
199
+    }
200
+
201
+    $results = apply_filters( 'wl_navigator_results', $results, $navigator_id );
202
+
203
+    return $results;
204 204
 }
205 205
 
206 206
 function _wl_network_navigator_get_data( $request ) {
207 207
 
208
-	// Limit the results (defaults to 4)
209
-	$navigator_length = isset( $request['limit'] ) ? intval( $request['limit'] ) : 4;
210
-	$navigator_offset = isset( $request['offset'] ) ? intval( $request['offset'] ) : 0;
211
-	$navigator_id     = $request['uniqid'];
212
-	$order_by         = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC';
213
-
214
-	$entities = $request['entities'];
215
-
216
-	// Post ID has to match an existing item
217
-	if ( ! isset( $entities ) || empty( $entities ) ) {
218
-		wp_send_json_error( 'No valid entities provided' );
219
-	}
220
-
221
-	$referencing_posts = _wl_network_navigator_get_results( $entities, array(
222
-		'ID',
223
-		'post_title',
224
-	), $order_by, $navigator_length, $navigator_offset );
225
-
226
-	// loop over them and take the first one which is not already in the $related_posts
227
-	$results = array();
228
-	foreach ( $referencing_posts as $referencing_post ) {
229
-		$serialized_entity = wl_serialize_entity( $referencing_post->entity_id );
230
-
231
-		/**
232
-		 * Use the thumbnail.
233
-		 *
234
-		 * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue.
235
-		 * @see https://github.com/insideout10/wordlift-plugin/issues/837
236
-		 *
237
-		 * @since 3.19.3 We're using the medium size image.
238
-		 */
239
-		$thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' );
240
-
241
-		$result = array(
242
-			'post'   => array(
243
-				'permalink' => get_permalink( $referencing_post->ID ),
244
-				'title'     => $referencing_post->post_title,
245
-				'thumbnail' => $thumbnail,
246
-			),
247
-			'entity' => array(
248
-				'label'     => $serialized_entity['label'],
249
-				'mainType'  => $serialized_entity['mainType'],
250
-				'permalink' => get_permalink( $referencing_post->entity_id ),
251
-			),
252
-		);
253
-
254
-		$result['post']   = apply_filters( 'wl_network_navigator_data_post', $result['post'], intval( $referencing_post->ID ), $navigator_id );
255
-		$result['entity'] = apply_filters( 'wl_network_navigator_data_entity', $result['entity'], intval( $referencing_post->entity_id ), $navigator_id );
256
-
257
-		$results[] = $result;
258
-
259
-	}
260
-
261
-	if ( count( $results ) < $navigator_length ) {
262
-		$results = apply_filters( 'wl_network_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length );
263
-	}
264
-
265
-	$results = apply_filters( 'wl_network_navigator_results', $results, $navigator_id );
266
-
267
-	return $results;
208
+    // Limit the results (defaults to 4)
209
+    $navigator_length = isset( $request['limit'] ) ? intval( $request['limit'] ) : 4;
210
+    $navigator_offset = isset( $request['offset'] ) ? intval( $request['offset'] ) : 0;
211
+    $navigator_id     = $request['uniqid'];
212
+    $order_by         = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC';
213
+
214
+    $entities = $request['entities'];
215
+
216
+    // Post ID has to match an existing item
217
+    if ( ! isset( $entities ) || empty( $entities ) ) {
218
+        wp_send_json_error( 'No valid entities provided' );
219
+    }
220
+
221
+    $referencing_posts = _wl_network_navigator_get_results( $entities, array(
222
+        'ID',
223
+        'post_title',
224
+    ), $order_by, $navigator_length, $navigator_offset );
225
+
226
+    // loop over them and take the first one which is not already in the $related_posts
227
+    $results = array();
228
+    foreach ( $referencing_posts as $referencing_post ) {
229
+        $serialized_entity = wl_serialize_entity( $referencing_post->entity_id );
230
+
231
+        /**
232
+         * Use the thumbnail.
233
+         *
234
+         * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue.
235
+         * @see https://github.com/insideout10/wordlift-plugin/issues/837
236
+         *
237
+         * @since 3.19.3 We're using the medium size image.
238
+         */
239
+        $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' );
240
+
241
+        $result = array(
242
+            'post'   => array(
243
+                'permalink' => get_permalink( $referencing_post->ID ),
244
+                'title'     => $referencing_post->post_title,
245
+                'thumbnail' => $thumbnail,
246
+            ),
247
+            'entity' => array(
248
+                'label'     => $serialized_entity['label'],
249
+                'mainType'  => $serialized_entity['mainType'],
250
+                'permalink' => get_permalink( $referencing_post->entity_id ),
251
+            ),
252
+        );
253
+
254
+        $result['post']   = apply_filters( 'wl_network_navigator_data_post', $result['post'], intval( $referencing_post->ID ), $navigator_id );
255
+        $result['entity'] = apply_filters( 'wl_network_navigator_data_entity', $result['entity'], intval( $referencing_post->entity_id ), $navigator_id );
256
+
257
+        $results[] = $result;
258
+
259
+    }
260
+
261
+    if ( count( $results ) < $navigator_length ) {
262
+        $results = apply_filters( 'wl_network_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length );
263
+    }
264
+
265
+    $results = apply_filters( 'wl_network_navigator_results', $results, $navigator_id );
266
+
267
+    return $results;
268 268
 
269 269
 }
270 270
 
271 271
 function _wl_network_navigator_get_results(
272
-	$entities, $fields = array(
273
-	'ID',
274
-	'post_title',
272
+    $entities, $fields = array(
273
+    'ID',
274
+    'post_title',
275 275
 ), $order_by = 'ID DESC', $limit = 10, $offset = 0
276 276
 ) {
277
-	global $wpdb;
278
-
279
-	$select = implode( ', ', array_map( function ( $item ) {
280
-		return "p.$item AS $item";
281
-	}, (array) $fields ) );
282
-
283
-	$order_by = implode( ', ', array_map( function ( $item ) {
284
-		return "p.$item";
285
-	}, (array) $order_by ) );
286
-
287
-	$entities_in = implode( ',', array_map( function ( $item ) {
288
-		$entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( urldecode( $item ) );
289
-		if ( isset( $entity ) ) {
290
-			return $entity->ID;
291
-		}
292
-	}, $entities ) );
293
-
294
-	/** @noinspection SqlNoDataSourceInspection */
295
-	return $wpdb->get_results(
296
-		$wpdb->prepare( <<<EOF
277
+    global $wpdb;
278
+
279
+    $select = implode( ', ', array_map( function ( $item ) {
280
+        return "p.$item AS $item";
281
+    }, (array) $fields ) );
282
+
283
+    $order_by = implode( ', ', array_map( function ( $item ) {
284
+        return "p.$item";
285
+    }, (array) $order_by ) );
286
+
287
+    $entities_in = implode( ',', array_map( function ( $item ) {
288
+        $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( urldecode( $item ) );
289
+        if ( isset( $entity ) ) {
290
+            return $entity->ID;
291
+        }
292
+    }, $entities ) );
293
+
294
+    /** @noinspection SqlNoDataSourceInspection */
295
+    return $wpdb->get_results(
296
+        $wpdb->prepare( <<<EOF
297 297
 SELECT %3\$s, p2.ID as entity_id
298 298
  FROM {$wpdb->prefix}wl_relation_instances r1
299 299
 	-- get the ID of the post entity in common between the object and the subject 2. 
@@ -319,8 +319,8 @@  discard block
 block discarded – undo
319 319
  LIMIT %1\$d
320 320
  OFFSET %2\$d
321 321
 EOF
322
-			, $limit, $offset, $select, $order_by )
323
-	);
322
+            , $limit, $offset, $select, $order_by )
323
+    );
324 324
 
325 325
 }
326 326
 
@@ -332,9 +332,9 @@  discard block
 block discarded – undo
332 332
  */
333 333
 function wl_shortcode_navigator_ajax() {
334 334
 
335
-	// Temporary blocking the Navigator.
336
-	$results = wl_shortcode_navigator_data();
337
-	wl_core_send_json( $results );
335
+    // Temporary blocking the Navigator.
336
+    $results = wl_shortcode_navigator_data();
337
+    wl_core_send_json( $results );
338 338
 
339 339
 }
340 340
 
@@ -346,16 +346,16 @@  discard block
 block discarded – undo
346 346
  */
347 347
 function wl_shortcode_navigator_wp_json() {
348 348
 
349
-	$results = wl_shortcode_navigator_data();
350
-	if ( ob_get_contents() ) {
351
-		ob_clean();
352
-	}
349
+    $results = wl_shortcode_navigator_data();
350
+    if ( ob_get_contents() ) {
351
+        ob_clean();
352
+    }
353 353
 
354
-	return array(
355
-		'items' => array(
356
-			array( 'values' => $results ),
357
-		),
358
-	);
354
+    return array(
355
+        'items' => array(
356
+            array( 'values' => $results ),
357
+        ),
358
+    );
359 359
 
360 360
 }
361 361
 
@@ -363,22 +363,22 @@  discard block
 block discarded – undo
363 363
  * Adding `rest_api_init` action for amp backend of navigator
364 364
  */
365 365
 add_action( 'rest_api_init', function () {
366
-	register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array(
367
-		'methods'             => 'GET',
368
-		'permission_callback' => '__return_true',
369
-		'callback'            => 'wl_shortcode_navigator_wp_json'
370
-	) );
366
+    register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array(
367
+        'methods'             => 'GET',
368
+        'permission_callback' => '__return_true',
369
+        'callback'            => 'wl_shortcode_navigator_wp_json'
370
+    ) );
371 371
 } );
372 372
 
373 373
 /**
374 374
  * Adding `rest_api_init` action for backend of network navigator
375 375
  */
376 376
 add_action( 'rest_api_init', function () {
377
-	register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/network-navigator', array(
378
-		'methods'             => 'GET',
379
-		'callback'            => 'wl_network_navigator_wp_json',
380
-		'permission_callback' => '__return_true',
381
-	) );
377
+    register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/network-navigator', array(
378
+        'methods'             => 'GET',
379
+        'callback'            => 'wl_network_navigator_wp_json',
380
+        'permission_callback' => '__return_true',
381
+    ) );
382 382
 } );
383 383
 
384 384
 /**
@@ -387,23 +387,23 @@  discard block
 block discarded – undo
387 387
  * @since 2.2.0
388 388
  */
389 389
 add_action( 'plugins_loaded', function () {
390
-	$action = array_key_exists( 'action', $_REQUEST ) ? (string) $_REQUEST['action'] : '';
391
-	if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) {
392
-		return;
393
-	}
394
-
395
-	remove_action( 'plugins_loaded', 'rocket_init' );
396
-	remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 );
397
-	remove_action( 'plugins_loaded', 'wpseo_init', 14 );
390
+    $action = array_key_exists( 'action', $_REQUEST ) ? (string) $_REQUEST['action'] : '';
391
+    if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) {
392
+        return;
393
+    }
394
+
395
+    remove_action( 'plugins_loaded', 'rocket_init' );
396
+    remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 );
397
+    remove_action( 'plugins_loaded', 'wpseo_init', 14 );
398 398
 }, 0 );
399 399
 
400 400
 add_action( 'init', function () {
401
-	$action = array_key_exists( 'action', $_REQUEST ) ? (string) $_REQUEST['action'] : '';
402
-	if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) {
403
-		return;
404
-	}
401
+    $action = array_key_exists( 'action', $_REQUEST ) ? (string) $_REQUEST['action'] : '';
402
+    if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) {
403
+        return;
404
+    }
405 405
 
406
-	remove_action( 'init', 'wp_widgets_init', 1 );
407
-	remove_action( 'init', 'gglcptch_init' );
406
+    remove_action( 'init', 'wp_widgets_init', 1 );
407
+    remove_action( 'init', 'gglcptch_init' );
408 408
 }, 0 );
409 409
 
Please login to merge, or discard this patch.
Spacing   +110 added lines, -110 removed lines patch added patch discarded remove patch
@@ -22,28 +22,28 @@  discard block
 block discarded – undo
22 22
 
23 23
 	// Create the cache key.
24 24
 	$cache_key_params = $_REQUEST;
25
-	unset( $cache_key_params['uniqid'] );
26
-	$cache_key = array( 'request_params' => $cache_key_params );
25
+	unset($cache_key_params['uniqid']);
26
+	$cache_key = array('request_params' => $cache_key_params);
27 27
 
28 28
 	// Create the TTL cache and try to get the results.
29
-	$cache         = new Ttl_Cache( "navigator", 8 * 60 * 60 ); // 8 hours.
30
-	$cache_results = $cache->get( $cache_key );
29
+	$cache         = new Ttl_Cache("navigator", 8 * 60 * 60); // 8 hours.
30
+	$cache_results = $cache->get($cache_key);
31 31
 
32 32
 	// So that the endpoint can be used remotely
33
-	header( 'Access-Control-Allow-Origin: *' );
33
+	header('Access-Control-Allow-Origin: *');
34 34
 
35
-	if ( isset( $cache_results ) ) {
36
-		header( 'X-WordLift-Cache: HIT' );
35
+	if (isset($cache_results)) {
36
+		header('X-WordLift-Cache: HIT');
37 37
 
38 38
 		return $cache_results;
39 39
 	}
40 40
 
41
-	header( 'X-WordLift-Cache: MISS' );
41
+	header('X-WordLift-Cache: MISS');
42 42
 
43 43
 	$results = _wl_navigator_get_data();
44 44
 
45 45
 	// Put the result before sending the json to the client, since sending the json will terminate us.
46
-	$cache->put( $cache_key, $results );
46
+	$cache->put($cache_key, $results);
47 47
 
48 48
 	return $results;
49 49
 }
@@ -58,29 +58,29 @@  discard block
 block discarded – undo
58 58
  * @since 3.22.6
59 59
  *
60 60
  */
61
-function wl_network_navigator_wp_json( $request ) {
61
+function wl_network_navigator_wp_json($request) {
62 62
 
63 63
 	// Create the cache key.
64 64
 	$cache_key_params = $_REQUEST;
65
-	unset( $cache_key_params['uniqid'] );
66
-	$cache_key = array( 'request_params' => $cache_key_params );
65
+	unset($cache_key_params['uniqid']);
66
+	$cache_key = array('request_params' => $cache_key_params);
67 67
 
68 68
 	// Create the TTL cache and try to get the results.
69
-	$cache         = new Ttl_Cache( "network-navigator", 8 * 60 * 60 ); // 8 hours.
70
-	$cache_results = $cache->get( $cache_key );
69
+	$cache         = new Ttl_Cache("network-navigator", 8 * 60 * 60); // 8 hours.
70
+	$cache_results = $cache->get($cache_key);
71 71
 
72
-	if ( isset( $cache_results ) ) {
73
-		header( 'X-WordLift-Cache: HIT' );
72
+	if (isset($cache_results)) {
73
+		header('X-WordLift-Cache: HIT');
74 74
 
75 75
 		return $cache_results;
76 76
 	}
77 77
 
78
-	header( 'X-WordLift-Cache: MISS' );
78
+	header('X-WordLift-Cache: MISS');
79 79
 
80
-	$results = _wl_network_navigator_get_data( $request );
80
+	$results = _wl_network_navigator_get_data($request);
81 81
 
82 82
 	// Put the result before sending the json to the client, since sending the json will terminate us.
83
-	$cache->put( $cache_key, $results );
83
+	$cache->put($cache_key, $results);
84 84
 
85 85
 	return $results;
86 86
 
@@ -89,58 +89,58 @@  discard block
 block discarded – undo
89 89
 function _wl_navigator_get_data() {
90 90
 
91 91
 	// Post ID must be defined
92
-	if ( ! isset( $_GET['post_id'] ) ) {
93
-		wp_send_json_error( 'No post_id given' );
92
+	if ( ! isset($_GET['post_id'])) {
93
+		wp_send_json_error('No post_id given');
94 94
 
95 95
 		return array();
96 96
 	}
97 97
 
98 98
 	// Post ID must be defined
99
-	if ( ! isset( $_GET['uniqid'] ) ) {
100
-		wp_send_json_error( 'No uniqid given' );
99
+	if ( ! isset($_GET['uniqid'])) {
100
+		wp_send_json_error('No uniqid given');
101 101
 
102 102
 		return array();
103 103
 	}
104 104
 
105 105
 	// Limit the results (defaults to 4)
106
-	$navigator_length    = isset( $_GET['limit'] ) ? intval( $_GET['limit'] ) : 4;
107
-	$navigator_offset    = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0;
108
-	$order_by            = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC';
109
-	$post_types          = isset( $_GET['post_types'] ) ? (string) $_GET['post_types'] : '';
110
-	$post_types          = explode( ',', $post_types );
106
+	$navigator_length    = isset($_GET['limit']) ? intval($_GET['limit']) : 4;
107
+	$navigator_offset    = isset($_GET['offset']) ? intval($_GET['offset']) : 0;
108
+	$order_by            = isset($_GET['sort']) ? sanitize_sql_orderby($_GET['sort']) : 'ID DESC';
109
+	$post_types          = isset($_GET['post_types']) ? (string) $_GET['post_types'] : '';
110
+	$post_types          = explode(',', $post_types);
111 111
 	$existing_post_types = get_post_types();
112
-	$post_types          = array_values( array_intersect( $existing_post_types, $post_types ) );
112
+	$post_types          = array_values(array_intersect($existing_post_types, $post_types));
113 113
 	$current_post_id     = (int) $_GET['post_id'];
114
-	$current_post        = get_post( $current_post_id );
114
+	$current_post        = get_post($current_post_id);
115 115
 
116 116
 	$navigator_id = (string) $_GET['uniqid'];
117 117
 
118 118
 	// Post ID has to match an existing item
119
-	if ( null === $current_post ) {
120
-		wp_send_json_error( 'No valid post_id given' );
119
+	if (null === $current_post) {
120
+		wp_send_json_error('No valid post_id given');
121 121
 
122 122
 		return array();
123 123
 	}
124 124
 
125 125
 	// Determine navigator type and call respective _get_results
126
-	if ( get_post_type( $current_post_id ) === Wordlift_Entity_Service::TYPE_NAME ) {
126
+	if (get_post_type($current_post_id) === Wordlift_Entity_Service::TYPE_NAME) {
127 127
 
128
-		$referencing_posts = Navigator_Data::entity_navigator_get_results( $current_post_id, array(
128
+		$referencing_posts = Navigator_Data::entity_navigator_get_results($current_post_id, array(
129 129
 			'ID',
130 130
 			'post_title',
131
-		), $order_by, $navigator_length, $navigator_offset, $post_types );
131
+		), $order_by, $navigator_length, $navigator_offset, $post_types);
132 132
 	} else {
133
-		$referencing_posts = Navigator_Data::post_navigator_get_results( $current_post_id, array(
133
+		$referencing_posts = Navigator_Data::post_navigator_get_results($current_post_id, array(
134 134
 			'ID',
135 135
 			'post_title',
136
-		), $order_by, $navigator_length, $navigator_offset, $post_types );
136
+		), $order_by, $navigator_length, $navigator_offset, $post_types);
137 137
 
138 138
 	}
139 139
 
140 140
 	// loop over them and take the first one which is not already in the $related_posts
141 141
 	$results = array();
142
-	foreach ( $referencing_posts as $referencing_post ) {
143
-		$serialized_entity = wl_serialize_entity( $referencing_post->entity_id );
142
+	foreach ($referencing_posts as $referencing_post) {
143
+		$serialized_entity = wl_serialize_entity($referencing_post->entity_id);
144 144
 
145 145
 		/**
146 146
 		 * Use the thumbnail.
@@ -150,21 +150,21 @@  discard block
 block discarded – undo
150 150
 		 *
151 151
 		 * @since 3.19.3 We're using the medium size image.
152 152
 		 */
153
-		$thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' );
153
+		$thumbnail = get_the_post_thumbnail_url($referencing_post, 'medium');
154 154
 
155 155
 		$result = array(
156 156
 			'post'   => array(
157 157
 				'id'        => $referencing_post->ID,
158
-				'permalink' => get_permalink( $referencing_post->ID ),
159
-				'title'     => html_entity_decode( $referencing_post->post_title, ENT_QUOTES, 'UTF-8' ),
158
+				'permalink' => get_permalink($referencing_post->ID),
159
+				'title'     => html_entity_decode($referencing_post->post_title, ENT_QUOTES, 'UTF-8'),
160 160
 				'thumbnail' => $thumbnail,
161
-				'srcset'    => Srcset_Util::get_srcset( $referencing_post->ID, Srcset_Util::NAVIGATOR_WIDGET )
161
+				'srcset'    => Srcset_Util::get_srcset($referencing_post->ID, Srcset_Util::NAVIGATOR_WIDGET)
162 162
 			),
163 163
 			'entity' => array(
164 164
 				'id'        => $referencing_post->entity_id,
165 165
 				'label'     => $serialized_entity['label'],
166 166
 				'mainType'  => $serialized_entity['mainType'],
167
-				'permalink' => get_permalink( $referencing_post->entity_id ),
167
+				'permalink' => get_permalink($referencing_post->entity_id),
168 168
 			),
169 169
 		);
170 170
 
@@ -172,61 +172,61 @@  discard block
 block discarded – undo
172 172
 	}
173 173
 
174 174
 
175
-	if ( count( $results ) < $navigator_length ) {
176
-		$results = apply_filters( 'wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length );
175
+	if (count($results) < $navigator_length) {
176
+		$results = apply_filters('wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length);
177 177
 	}
178 178
 
179 179
 	// Add filler posts if needed
180
-	$filler_count = $navigator_length - count( $results );
181
-	if ( $filler_count > 0 ) {
182
-		$referencing_post_ids = array_map( function ( $p ) {
180
+	$filler_count = $navigator_length - count($results);
181
+	if ($filler_count > 0) {
182
+		$referencing_post_ids = array_map(function($p) {
183 183
 			return $p->ID;
184
-		}, $referencing_posts );
184
+		}, $referencing_posts);
185 185
 		/**
186 186
 		 * @since 3.27.8
187 187
 		 * Filler posts are fetched using this util.
188 188
 		 */
189
-		$filler_posts_util       = new Filler_Posts_Util( $current_post_id, $post_types );
190
-		$post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids );
191
-		$filler_posts            = $filler_posts_util->get_filler_response( $filler_count, $post_ids_to_be_excluded );
192
-		$results                 = array_merge( $results, $filler_posts );
189
+		$filler_posts_util       = new Filler_Posts_Util($current_post_id, $post_types);
190
+		$post_ids_to_be_excluded = array_merge(array($current_post_id), $referencing_post_ids);
191
+		$filler_posts            = $filler_posts_util->get_filler_response($filler_count, $post_ids_to_be_excluded);
192
+		$results                 = array_merge($results, $filler_posts);
193 193
 	}
194 194
 
195 195
 	// Apply filters after fillers are added
196
-	foreach ( $results as $result_index => $result ) {
197
-		$results[ $result_index ]['post']   = apply_filters( 'wl_navigator_data_post', $result['post'], intval( $result['post']['id'] ), $navigator_id );
198
-		$results[ $result_index ]['entity'] = apply_filters( 'wl_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id );
196
+	foreach ($results as $result_index => $result) {
197
+		$results[$result_index]['post']   = apply_filters('wl_navigator_data_post', $result['post'], intval($result['post']['id']), $navigator_id);
198
+		$results[$result_index]['entity'] = apply_filters('wl_navigator_data_entity', $result['entity'], intval($result['entity']['id']), $navigator_id);
199 199
 	}
200 200
 
201
-	$results = apply_filters( 'wl_navigator_results', $results, $navigator_id );
201
+	$results = apply_filters('wl_navigator_results', $results, $navigator_id);
202 202
 
203 203
 	return $results;
204 204
 }
205 205
 
206
-function _wl_network_navigator_get_data( $request ) {
206
+function _wl_network_navigator_get_data($request) {
207 207
 
208 208
 	// Limit the results (defaults to 4)
209
-	$navigator_length = isset( $request['limit'] ) ? intval( $request['limit'] ) : 4;
210
-	$navigator_offset = isset( $request['offset'] ) ? intval( $request['offset'] ) : 0;
209
+	$navigator_length = isset($request['limit']) ? intval($request['limit']) : 4;
210
+	$navigator_offset = isset($request['offset']) ? intval($request['offset']) : 0;
211 211
 	$navigator_id     = $request['uniqid'];
212
-	$order_by         = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC';
212
+	$order_by         = isset($_GET['sort']) ? sanitize_sql_orderby($_GET['sort']) : 'ID DESC';
213 213
 
214 214
 	$entities = $request['entities'];
215 215
 
216 216
 	// Post ID has to match an existing item
217
-	if ( ! isset( $entities ) || empty( $entities ) ) {
218
-		wp_send_json_error( 'No valid entities provided' );
217
+	if ( ! isset($entities) || empty($entities)) {
218
+		wp_send_json_error('No valid entities provided');
219 219
 	}
220 220
 
221
-	$referencing_posts = _wl_network_navigator_get_results( $entities, array(
221
+	$referencing_posts = _wl_network_navigator_get_results($entities, array(
222 222
 		'ID',
223 223
 		'post_title',
224
-	), $order_by, $navigator_length, $navigator_offset );
224
+	), $order_by, $navigator_length, $navigator_offset);
225 225
 
226 226
 	// loop over them and take the first one which is not already in the $related_posts
227 227
 	$results = array();
228
-	foreach ( $referencing_posts as $referencing_post ) {
229
-		$serialized_entity = wl_serialize_entity( $referencing_post->entity_id );
228
+	foreach ($referencing_posts as $referencing_post) {
229
+		$serialized_entity = wl_serialize_entity($referencing_post->entity_id);
230 230
 
231 231
 		/**
232 232
 		 * Use the thumbnail.
@@ -236,33 +236,33 @@  discard block
 block discarded – undo
236 236
 		 *
237 237
 		 * @since 3.19.3 We're using the medium size image.
238 238
 		 */
239
-		$thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' );
239
+		$thumbnail = get_the_post_thumbnail_url($referencing_post, 'medium');
240 240
 
241 241
 		$result = array(
242 242
 			'post'   => array(
243
-				'permalink' => get_permalink( $referencing_post->ID ),
243
+				'permalink' => get_permalink($referencing_post->ID),
244 244
 				'title'     => $referencing_post->post_title,
245 245
 				'thumbnail' => $thumbnail,
246 246
 			),
247 247
 			'entity' => array(
248 248
 				'label'     => $serialized_entity['label'],
249 249
 				'mainType'  => $serialized_entity['mainType'],
250
-				'permalink' => get_permalink( $referencing_post->entity_id ),
250
+				'permalink' => get_permalink($referencing_post->entity_id),
251 251
 			),
252 252
 		);
253 253
 
254
-		$result['post']   = apply_filters( 'wl_network_navigator_data_post', $result['post'], intval( $referencing_post->ID ), $navigator_id );
255
-		$result['entity'] = apply_filters( 'wl_network_navigator_data_entity', $result['entity'], intval( $referencing_post->entity_id ), $navigator_id );
254
+		$result['post']   = apply_filters('wl_network_navigator_data_post', $result['post'], intval($referencing_post->ID), $navigator_id);
255
+		$result['entity'] = apply_filters('wl_network_navigator_data_entity', $result['entity'], intval($referencing_post->entity_id), $navigator_id);
256 256
 
257 257
 		$results[] = $result;
258 258
 
259 259
 	}
260 260
 
261
-	if ( count( $results ) < $navigator_length ) {
262
-		$results = apply_filters( 'wl_network_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length );
261
+	if (count($results) < $navigator_length) {
262
+		$results = apply_filters('wl_network_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length);
263 263
 	}
264 264
 
265
-	$results = apply_filters( 'wl_network_navigator_results', $results, $navigator_id );
265
+	$results = apply_filters('wl_network_navigator_results', $results, $navigator_id);
266 266
 
267 267
 	return $results;
268 268
 
@@ -276,24 +276,24 @@  discard block
 block discarded – undo
276 276
 ) {
277 277
 	global $wpdb;
278 278
 
279
-	$select = implode( ', ', array_map( function ( $item ) {
279
+	$select = implode(', ', array_map(function($item) {
280 280
 		return "p.$item AS $item";
281
-	}, (array) $fields ) );
281
+	}, (array) $fields));
282 282
 
283
-	$order_by = implode( ', ', array_map( function ( $item ) {
283
+	$order_by = implode(', ', array_map(function($item) {
284 284
 		return "p.$item";
285
-	}, (array) $order_by ) );
285
+	}, (array) $order_by));
286 286
 
287
-	$entities_in = implode( ',', array_map( function ( $item ) {
288
-		$entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( urldecode( $item ) );
289
-		if ( isset( $entity ) ) {
287
+	$entities_in = implode(',', array_map(function($item) {
288
+		$entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri(urldecode($item));
289
+		if (isset($entity)) {
290 290
 			return $entity->ID;
291 291
 		}
292
-	}, $entities ) );
292
+	}, $entities));
293 293
 
294 294
 	/** @noinspection SqlNoDataSourceInspection */
295 295
 	return $wpdb->get_results(
296
-		$wpdb->prepare( <<<EOF
296
+		$wpdb->prepare(<<<EOF
297 297
 SELECT %3\$s, p2.ID as entity_id
298 298
  FROM {$wpdb->prefix}wl_relation_instances r1
299 299
 	-- get the ID of the post entity in common between the object and the subject 2. 
@@ -319,7 +319,7 @@  discard block
 block discarded – undo
319 319
  LIMIT %1\$d
320 320
  OFFSET %2\$d
321 321
 EOF
322
-			, $limit, $offset, $select, $order_by )
322
+			, $limit, $offset, $select, $order_by)
323 323
 	);
324 324
 
325 325
 }
@@ -334,12 +334,12 @@  discard block
 block discarded – undo
334 334
 
335 335
 	// Temporary blocking the Navigator.
336 336
 	$results = wl_shortcode_navigator_data();
337
-	wl_core_send_json( $results );
337
+	wl_core_send_json($results);
338 338
 
339 339
 }
340 340
 
341
-add_action( 'wp_ajax_wl_navigator', 'wl_shortcode_navigator_ajax' );
342
-add_action( 'wp_ajax_nopriv_wl_navigator', 'wl_shortcode_navigator_ajax' );
341
+add_action('wp_ajax_wl_navigator', 'wl_shortcode_navigator_ajax');
342
+add_action('wp_ajax_nopriv_wl_navigator', 'wl_shortcode_navigator_ajax');
343 343
 
344 344
 /**
345 345
  * wp-json call for the navigator widget
@@ -347,13 +347,13 @@  discard block
 block discarded – undo
347 347
 function wl_shortcode_navigator_wp_json() {
348 348
 
349 349
 	$results = wl_shortcode_navigator_data();
350
-	if ( ob_get_contents() ) {
350
+	if (ob_get_contents()) {
351 351
 		ob_clean();
352 352
 	}
353 353
 
354 354
 	return array(
355 355
 		'items' => array(
356
-			array( 'values' => $results ),
356
+			array('values' => $results),
357 357
 		),
358 358
 	);
359 359
 
@@ -362,23 +362,23 @@  discard block
 block discarded – undo
362 362
 /**
363 363
  * Adding `rest_api_init` action for amp backend of navigator
364 364
  */
365
-add_action( 'rest_api_init', function () {
366
-	register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array(
365
+add_action('rest_api_init', function() {
366
+	register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array(
367 367
 		'methods'             => 'GET',
368 368
 		'permission_callback' => '__return_true',
369 369
 		'callback'            => 'wl_shortcode_navigator_wp_json'
370
-	) );
370
+	));
371 371
 } );
372 372
 
373 373
 /**
374 374
  * Adding `rest_api_init` action for backend of network navigator
375 375
  */
376
-add_action( 'rest_api_init', function () {
377
-	register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/network-navigator', array(
376
+add_action('rest_api_init', function() {
377
+	register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/network-navigator', array(
378 378
 		'methods'             => 'GET',
379 379
 		'callback'            => 'wl_network_navigator_wp_json',
380 380
 		'permission_callback' => '__return_true',
381
-	) );
381
+	));
382 382
 } );
383 383
 
384 384
 /**
@@ -386,24 +386,24 @@  discard block
 block discarded – undo
386 386
  *
387 387
  * @since 2.2.0
388 388
  */
389
-add_action( 'plugins_loaded', function () {
390
-	$action = array_key_exists( 'action', $_REQUEST ) ? (string) $_REQUEST['action'] : '';
391
-	if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) {
389
+add_action('plugins_loaded', function() {
390
+	$action = array_key_exists('action', $_REQUEST) ? (string) $_REQUEST['action'] : '';
391
+	if ( ! defined('DOING_AJAX') || ! DOING_AJAX || 'wl_navigator' !== $action) {
392 392
 		return;
393 393
 	}
394 394
 
395
-	remove_action( 'plugins_loaded', 'rocket_init' );
396
-	remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 );
397
-	remove_action( 'plugins_loaded', 'wpseo_init', 14 );
398
-}, 0 );
395
+	remove_action('plugins_loaded', 'rocket_init');
396
+	remove_action('plugins_loaded', 'wpseo_premium_init', 14);
397
+	remove_action('plugins_loaded', 'wpseo_init', 14);
398
+}, 0);
399 399
 
400
-add_action( 'init', function () {
401
-	$action = array_key_exists( 'action', $_REQUEST ) ? (string) $_REQUEST['action'] : '';
402
-	if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) {
400
+add_action('init', function() {
401
+	$action = array_key_exists('action', $_REQUEST) ? (string) $_REQUEST['action'] : '';
402
+	if ( ! defined('DOING_AJAX') || ! DOING_AJAX || 'wl_navigator' !== $action) {
403 403
 		return;
404 404
 	}
405 405
 
406
-	remove_action( 'init', 'wp_widgets_init', 1 );
407
-	remove_action( 'init', 'gglcptch_init' );
408
-}, 0 );
406
+	remove_action('init', 'wp_widgets_init', 1);
407
+	remove_action('init', 'gglcptch_init');
408
+}, 0);
409 409
 
Please login to merge, or discard this patch.
src/shortcodes/wordlift_shortcode_geomap.php 2 patches
Indentation   +73 added lines, -73 removed lines patch added patch discarded remove patch
@@ -11,42 +11,42 @@  discard block
 block discarded – undo
11 11
  * Print both global or post related places in json. It's executed via Ajax
12 12
  */
13 13
 function wl_shortcode_geomap_ajax() {
14
-	// Get the post Id.
15
-	$post_id = ( isset( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : null );
14
+    // Get the post Id.
15
+    $post_id = ( isset( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : null );
16 16
 
17
-	$map_data = ( is_numeric( $post_id )
18
-		? wl_shortcode_geomap_ajax_single_post( (int) $post_id )
19
-		: wl_shortcode_geomap_ajax_all_posts() );
17
+    $map_data = ( is_numeric( $post_id )
18
+        ? wl_shortcode_geomap_ajax_single_post( (int) $post_id )
19
+        : wl_shortcode_geomap_ajax_all_posts() );
20 20
 
21
-	wl_core_send_json( wl_shortcode_geomap_format_results( $map_data, $post_id ) );
21
+    wl_core_send_json( wl_shortcode_geomap_format_results( $map_data, $post_id ) );
22 22
 
23 23
 }
24 24
 
25 25
 function wl_shortcode_geomap_ajax_all_posts() {
26
-	global $wpdb;
27
-
28
-	return $wpdb->get_results( $wpdb->prepare(
29
-		"SELECT DISTINCT p1.ID, p1.post_title, pm1.meta_value AS longitude, pm2.meta_value AS latitude"
30
-		. " FROM {$wpdb->posts} p1 "
31
-		. " INNER JOIN {$wpdb->prefix}wl_relation_instances ri"
32
-		. "  ON ri.object_id = p1.ID AND ri.predicate = %s"
33
-		. " INNER JOIN {$wpdb->postmeta} pm1"
34
-		. "  ON pm1.post_id = p1.ID AND pm1.meta_key = %s AND '0' != pm1.meta_value"
35
-		. " INNER JOIN {$wpdb->postmeta} pm2"
36
-		. "  ON pm2.post_id = p1.ID AND pm2.meta_key = %s AND '0' != pm2.meta_value"
37
-		. " WHERE p1.post_status = %s",
38
-		'where',
39
-		'wl_geo_latitude',
40
-		'wl_geo_longitude',
41
-		'publish'
42
-	) );
26
+    global $wpdb;
27
+
28
+    return $wpdb->get_results( $wpdb->prepare(
29
+        "SELECT DISTINCT p1.ID, p1.post_title, pm1.meta_value AS longitude, pm2.meta_value AS latitude"
30
+        . " FROM {$wpdb->posts} p1 "
31
+        . " INNER JOIN {$wpdb->prefix}wl_relation_instances ri"
32
+        . "  ON ri.object_id = p1.ID AND ri.predicate = %s"
33
+        . " INNER JOIN {$wpdb->postmeta} pm1"
34
+        . "  ON pm1.post_id = p1.ID AND pm1.meta_key = %s AND '0' != pm1.meta_value"
35
+        . " INNER JOIN {$wpdb->postmeta} pm2"
36
+        . "  ON pm2.post_id = p1.ID AND pm2.meta_key = %s AND '0' != pm2.meta_value"
37
+        . " WHERE p1.post_status = %s",
38
+        'where',
39
+        'wl_geo_latitude',
40
+        'wl_geo_longitude',
41
+        'publish'
42
+    ) );
43 43
 }
44 44
 
45 45
 function wl_shortcode_geomap_ajax_single_post( $post_id ) {
46
-	global $wpdb;
46
+    global $wpdb;
47 47
 
48
-	return $wpdb->get_results( $wpdb->prepare(
49
-		"
48
+    return $wpdb->get_results( $wpdb->prepare(
49
+        "
50 50
 		SELECT p2.ID, p2.post_title, pm1.meta_value AS longitude, pm2.meta_value AS latitude
51 51
 		 FROM {$wpdb->prefix}wl_relation_instances ri
52 52
 		 INNER JOIN {$wpdb->posts} p2
@@ -65,69 +65,69 @@  discard block
 block discarded – undo
65 65
 		  ON pm2.post_id = p.ID AND pm2.meta_key = %s AND '0' != pm2.meta_value
66 66
 		 WHERE p.ID = %s
67 67
 		",
68
-		'publish',
69
-		'wl_geo_latitude',
70
-		'wl_geo_longitude',
71
-		$post_id,
72
-		'where',
73
-		// UNION
74
-		'wl_geo_latitude',
75
-		'wl_geo_longitude',
76
-		$post_id
77
-	) );
68
+        'publish',
69
+        'wl_geo_latitude',
70
+        'wl_geo_longitude',
71
+        $post_id,
72
+        'where',
73
+        // UNION
74
+        'wl_geo_latitude',
75
+        'wl_geo_longitude',
76
+        $post_id
77
+    ) );
78 78
 }
79 79
 
80 80
 function wl_shortcode_geomap_get_subjects( $post_id, $exclude_post_id ) {
81 81
 
82
-	global $wpdb;
83
-
84
-	return $wpdb->get_results( $wpdb->prepare(
85
-		"SELECT p.ID, p.post_title"
86
-		. " FROM {$wpdb->prefix}wl_relation_instances ri" .
87
-		" INNER JOIN {$wpdb->posts} p" .
88
-		"  ON p.ID = ri.subject_id AND p.post_status = %s AND p.ID != %d" .
89
-		" WHERE ri.object_id = %d AND ri.predicate = %s",
90
-		'publish',
91
-		$exclude_post_id,
92
-		$post_id,
93
-		'where'
94
-	) );
82
+    global $wpdb;
83
+
84
+    return $wpdb->get_results( $wpdb->prepare(
85
+        "SELECT p.ID, p.post_title"
86
+        . " FROM {$wpdb->prefix}wl_relation_instances ri" .
87
+        " INNER JOIN {$wpdb->posts} p" .
88
+        "  ON p.ID = ri.subject_id AND p.post_status = %s AND p.ID != %d" .
89
+        " WHERE ri.object_id = %d AND ri.predicate = %s",
90
+        'publish',
91
+        $exclude_post_id,
92
+        $post_id,
93
+        'where'
94
+    ) );
95 95
 }
96 96
 
97 97
 function wl_shortcode_geomap_format_results( $results, $post_id = null ) {
98 98
 
99
-	$boundaries = array();
100
-	$features   = array_map( function ( $item ) use ( &$boundaries, $post_id ) {
99
+    $boundaries = array();
100
+    $features   = array_map( function ( $item ) use ( &$boundaries, $post_id ) {
101 101
 
102
-		$thumbnail_url  = get_the_post_thumbnail_url( $item->ID );
103
-		$thumbnail_html = ( $thumbnail_url ? "<img src='$thumbnail_url' width='100%'>" : '' );
102
+        $thumbnail_url  = get_the_post_thumbnail_url( $item->ID );
103
+        $thumbnail_html = ( $thumbnail_url ? "<img src='$thumbnail_url' width='100%'>" : '' );
104 104
 
105
-		// Related posts.
106
-		$subjects            = wl_shortcode_geomap_get_subjects( $item->ID, $post_id );
107
-		$subjects_inner_html = array_reduce( $subjects, function ( $carry, $subject ) {
105
+        // Related posts.
106
+        $subjects            = wl_shortcode_geomap_get_subjects( $item->ID, $post_id );
107
+        $subjects_inner_html = array_reduce( $subjects, function ( $carry, $subject ) {
108 108
 
109
-			$permalink = get_permalink( $subject->ID );
109
+            $permalink = get_permalink( $subject->ID );
110 110
 
111
-			return $carry . sprintf( '<li><a href="%s">%s</a></li>', $permalink, esc_html( $subject->post_title ) );
112
-		}, '' );
113
-		$subjects_html       = ( ! empty( $subjects_inner_html ) ? '<ul>' . $subjects_inner_html . '</ul>' : '' );
111
+            return $carry . sprintf( '<li><a href="%s">%s</a></li>', $permalink, esc_html( $subject->post_title ) );
112
+        }, '' );
113
+        $subjects_html       = ( ! empty( $subjects_inner_html ) ? '<ul>' . $subjects_inner_html . '</ul>' : '' );
114 114
 
115
-		$popup_content = sprintf( '<a href="%s"><h6>%s</h6>%s</a>%s', get_permalink( $item->ID ), $thumbnail_html, esc_html( $item->post_title ), $subjects_html );
116
-		$latitude      = floatval( $item->latitude );
117
-		$longitude     = floatval( $item->longitude );
118
-		$coordinates   = array( $latitude, $longitude, );
119
-		$geometry      = array( 'type' => 'Point', 'coordinates' => $coordinates, );
115
+        $popup_content = sprintf( '<a href="%s"><h6>%s</h6>%s</a>%s', get_permalink( $item->ID ), $thumbnail_html, esc_html( $item->post_title ), $subjects_html );
116
+        $latitude      = floatval( $item->latitude );
117
+        $longitude     = floatval( $item->longitude );
118
+        $coordinates   = array( $latitude, $longitude, );
119
+        $geometry      = array( 'type' => 'Point', 'coordinates' => $coordinates, );
120 120
 
121
-		$boundaries[] = array( $longitude, $latitude, );
121
+        $boundaries[] = array( $longitude, $latitude, );
122 122
 
123
-		return array(
124
-			'type'       => 'Feature',
125
-			'properties' => array( 'popupContent' => $popup_content ),
126
-			'geometry'   => $geometry,
127
-		);
128
-	}, $results );
123
+        return array(
124
+            'type'       => 'Feature',
125
+            'properties' => array( 'popupContent' => $popup_content ),
126
+            'geometry'   => $geometry,
127
+        );
128
+    }, $results );
129 129
 
130
-	return array( 'features' => $features, 'boundaries' => $boundaries, );
130
+    return array( 'features' => $features, 'boundaries' => $boundaries, );
131 131
 }
132 132
 
133 133
 add_action( 'wp_ajax_wl_geomap', 'wl_shortcode_geomap_ajax' );
Please login to merge, or discard this patch.
Spacing   +37 added lines, -37 removed lines patch added patch discarded remove patch
@@ -12,20 +12,20 @@  discard block
 block discarded – undo
12 12
  */
13 13
 function wl_shortcode_geomap_ajax() {
14 14
 	// Get the post Id.
15
-	$post_id = ( isset( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : null );
15
+	$post_id = (isset($_REQUEST['post_id']) ? $_REQUEST['post_id'] : null);
16 16
 
17
-	$map_data = ( is_numeric( $post_id )
18
-		? wl_shortcode_geomap_ajax_single_post( (int) $post_id )
19
-		: wl_shortcode_geomap_ajax_all_posts() );
17
+	$map_data = (is_numeric($post_id)
18
+		? wl_shortcode_geomap_ajax_single_post((int) $post_id)
19
+		: wl_shortcode_geomap_ajax_all_posts());
20 20
 
21
-	wl_core_send_json( wl_shortcode_geomap_format_results( $map_data, $post_id ) );
21
+	wl_core_send_json(wl_shortcode_geomap_format_results($map_data, $post_id));
22 22
 
23 23
 }
24 24
 
25 25
 function wl_shortcode_geomap_ajax_all_posts() {
26 26
 	global $wpdb;
27 27
 
28
-	return $wpdb->get_results( $wpdb->prepare(
28
+	return $wpdb->get_results($wpdb->prepare(
29 29
 		"SELECT DISTINCT p1.ID, p1.post_title, pm1.meta_value AS longitude, pm2.meta_value AS latitude"
30 30
 		. " FROM {$wpdb->posts} p1 "
31 31
 		. " INNER JOIN {$wpdb->prefix}wl_relation_instances ri"
@@ -39,13 +39,13 @@  discard block
 block discarded – undo
39 39
 		'wl_geo_latitude',
40 40
 		'wl_geo_longitude',
41 41
 		'publish'
42
-	) );
42
+	));
43 43
 }
44 44
 
45
-function wl_shortcode_geomap_ajax_single_post( $post_id ) {
45
+function wl_shortcode_geomap_ajax_single_post($post_id) {
46 46
 	global $wpdb;
47 47
 
48
-	return $wpdb->get_results( $wpdb->prepare(
48
+	return $wpdb->get_results($wpdb->prepare(
49 49
 		"
50 50
 		SELECT p2.ID, p2.post_title, pm1.meta_value AS longitude, pm2.meta_value AS latitude
51 51
 		 FROM {$wpdb->prefix}wl_relation_instances ri
@@ -74,61 +74,61 @@  discard block
 block discarded – undo
74 74
 		'wl_geo_latitude',
75 75
 		'wl_geo_longitude',
76 76
 		$post_id
77
-	) );
77
+	));
78 78
 }
79 79
 
80
-function wl_shortcode_geomap_get_subjects( $post_id, $exclude_post_id ) {
80
+function wl_shortcode_geomap_get_subjects($post_id, $exclude_post_id) {
81 81
 
82 82
 	global $wpdb;
83 83
 
84
-	return $wpdb->get_results( $wpdb->prepare(
84
+	return $wpdb->get_results($wpdb->prepare(
85 85
 		"SELECT p.ID, p.post_title"
86
-		. " FROM {$wpdb->prefix}wl_relation_instances ri" .
87
-		" INNER JOIN {$wpdb->posts} p" .
88
-		"  ON p.ID = ri.subject_id AND p.post_status = %s AND p.ID != %d" .
86
+		. " FROM {$wpdb->prefix}wl_relation_instances ri".
87
+		" INNER JOIN {$wpdb->posts} p".
88
+		"  ON p.ID = ri.subject_id AND p.post_status = %s AND p.ID != %d".
89 89
 		" WHERE ri.object_id = %d AND ri.predicate = %s",
90 90
 		'publish',
91 91
 		$exclude_post_id,
92 92
 		$post_id,
93 93
 		'where'
94
-	) );
94
+	));
95 95
 }
96 96
 
97
-function wl_shortcode_geomap_format_results( $results, $post_id = null ) {
97
+function wl_shortcode_geomap_format_results($results, $post_id = null) {
98 98
 
99 99
 	$boundaries = array();
100
-	$features   = array_map( function ( $item ) use ( &$boundaries, $post_id ) {
100
+	$features   = array_map(function($item) use (&$boundaries, $post_id) {
101 101
 
102
-		$thumbnail_url  = get_the_post_thumbnail_url( $item->ID );
103
-		$thumbnail_html = ( $thumbnail_url ? "<img src='$thumbnail_url' width='100%'>" : '' );
102
+		$thumbnail_url  = get_the_post_thumbnail_url($item->ID);
103
+		$thumbnail_html = ($thumbnail_url ? "<img src='$thumbnail_url' width='100%'>" : '');
104 104
 
105 105
 		// Related posts.
106
-		$subjects            = wl_shortcode_geomap_get_subjects( $item->ID, $post_id );
107
-		$subjects_inner_html = array_reduce( $subjects, function ( $carry, $subject ) {
106
+		$subjects            = wl_shortcode_geomap_get_subjects($item->ID, $post_id);
107
+		$subjects_inner_html = array_reduce($subjects, function($carry, $subject) {
108 108
 
109
-			$permalink = get_permalink( $subject->ID );
109
+			$permalink = get_permalink($subject->ID);
110 110
 
111
-			return $carry . sprintf( '<li><a href="%s">%s</a></li>', $permalink, esc_html( $subject->post_title ) );
112
-		}, '' );
113
-		$subjects_html       = ( ! empty( $subjects_inner_html ) ? '<ul>' . $subjects_inner_html . '</ul>' : '' );
111
+			return $carry.sprintf('<li><a href="%s">%s</a></li>', $permalink, esc_html($subject->post_title));
112
+		}, '');
113
+		$subjects_html = ( ! empty($subjects_inner_html) ? '<ul>'.$subjects_inner_html.'</ul>' : '');
114 114
 
115
-		$popup_content = sprintf( '<a href="%s"><h6>%s</h6>%s</a>%s', get_permalink( $item->ID ), $thumbnail_html, esc_html( $item->post_title ), $subjects_html );
116
-		$latitude      = floatval( $item->latitude );
117
-		$longitude     = floatval( $item->longitude );
118
-		$coordinates   = array( $latitude, $longitude, );
119
-		$geometry      = array( 'type' => 'Point', 'coordinates' => $coordinates, );
115
+		$popup_content = sprintf('<a href="%s"><h6>%s</h6>%s</a>%s', get_permalink($item->ID), $thumbnail_html, esc_html($item->post_title), $subjects_html);
116
+		$latitude      = floatval($item->latitude);
117
+		$longitude     = floatval($item->longitude);
118
+		$coordinates   = array($latitude, $longitude,);
119
+		$geometry      = array('type' => 'Point', 'coordinates' => $coordinates,);
120 120
 
121
-		$boundaries[] = array( $longitude, $latitude, );
121
+		$boundaries[] = array($longitude, $latitude,);
122 122
 
123 123
 		return array(
124 124
 			'type'       => 'Feature',
125
-			'properties' => array( 'popupContent' => $popup_content ),
125
+			'properties' => array('popupContent' => $popup_content),
126 126
 			'geometry'   => $geometry,
127 127
 		);
128
-	}, $results );
128
+	}, $results);
129 129
 
130
-	return array( 'features' => $features, 'boundaries' => $boundaries, );
130
+	return array('features' => $features, 'boundaries' => $boundaries,);
131 131
 }
132 132
 
133
-add_action( 'wp_ajax_wl_geomap', 'wl_shortcode_geomap_ajax' );
134
-add_action( 'wp_ajax_nopriv_wl_geomap', 'wl_shortcode_geomap_ajax' );
133
+add_action('wp_ajax_wl_geomap', 'wl_shortcode_geomap_ajax');
134
+add_action('wp_ajax_nopriv_wl_geomap', 'wl_shortcode_geomap_ajax');
Please login to merge, or discard this patch.
src/shortcodes/class-wordlift-shortcode-rest.php 2 patches
Indentation   +83 added lines, -83 removed lines patch added patch discarded remove patch
@@ -10,116 +10,116 @@
 block discarded – undo
10 10
  */
11 11
 abstract class Wordlift_Shortcode_REST {
12 12
 
13
-	/**
14
-	 * The cache_ttl, set by extending classes.
15
-	 */
16
-	const CACHE_TTL = 86400; // 24 hours
13
+    /**
14
+     * The cache_ttl, set by extending classes.
15
+     */
16
+    const CACHE_TTL = 86400; // 24 hours
17 17
 
18
-	public function __construct( $endpoint, $args ) {
18
+    public function __construct( $endpoint, $args ) {
19 19
 
20
-		$scope          = $this;
21
-		$this->endpoint = $endpoint;
22
-		$this->args     = $args;
20
+        $scope          = $this;
21
+        $this->endpoint = $endpoint;
22
+        $this->args     = $args;
23 23
 
24
-		// Register rest route with callback
25
-		add_action( 'rest_api_init', function () use ( $scope ) {
26
-			register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, $scope->endpoint, array(
27
-				'methods'  => WP_REST_Server::READABLE,
28
-				'permission_callback' => '__return_true',
29
-				'callback' => array( $scope, 'rest_callback' ),
30
-				'args'     => $scope->args
31
-			) );
32
-		} );
24
+        // Register rest route with callback
25
+        add_action( 'rest_api_init', function () use ( $scope ) {
26
+            register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, $scope->endpoint, array(
27
+                'methods'  => WP_REST_Server::READABLE,
28
+                'permission_callback' => '__return_true',
29
+                'callback' => array( $scope, 'rest_callback' ),
30
+                'args'     => $scope->args
31
+            ) );
32
+        } );
33 33
 
34
-		// Optimizations: disable unneeded plugins on this specific REST call. WPSeo is slowing down the responses quite a bit.
35
-		add_action( 'plugins_loaded', function () use ( $scope ) {
34
+        // Optimizations: disable unneeded plugins on this specific REST call. WPSeo is slowing down the responses quite a bit.
35
+        add_action( 'plugins_loaded', function () use ( $scope ) {
36 36
 
37
-			if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST || ! $scope->is_endpoint() ) {
38
-				return;
39
-			}
37
+            if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST || ! $scope->is_endpoint() ) {
38
+                return;
39
+            }
40 40
 
41
-			remove_action( 'plugins_loaded', 'rocket_init' );
42
-			remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 );
43
-			remove_action( 'plugins_loaded', 'wpseo_init', 14 );
44
-		}, 0 );
41
+            remove_action( 'plugins_loaded', 'rocket_init' );
42
+            remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 );
43
+            remove_action( 'plugins_loaded', 'wpseo_init', 14 );
44
+        }, 0 );
45 45
 
46
-		add_action( 'init', function () use ( $scope ) {
46
+        add_action( 'init', function () use ( $scope ) {
47 47
 
48
-			if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST || ! $scope->is_endpoint() ) {
49
-				return;
50
-			}
48
+            if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST || ! $scope->is_endpoint() ) {
49
+                return;
50
+            }
51 51
 
52
-			remove_action( 'init', 'wp_widgets_init', 1 );
53
-			remove_action( 'init', 'gglcptch_init' );
54
-		}, 0 );
52
+            remove_action( 'init', 'wp_widgets_init', 1 );
53
+            remove_action( 'init', 'gglcptch_init' );
54
+        }, 0 );
55 55
 
56
-	}
56
+    }
57 57
 
58
-	public abstract function get_data( $request );
58
+    public abstract function get_data( $request );
59 59
 
60
-	public function rest_callback( WP_REST_Request $request ) {
60
+    public function rest_callback( WP_REST_Request $request ) {
61 61
 
62
-		// Respond from origin if TTL is 0
63
-		if ( static::CACHE_TTL == 0 ) {
62
+        // Respond from origin if TTL is 0
63
+        if ( static::CACHE_TTL == 0 ) {
64 64
 
65
-			$data     = $this->get_data( $request );
66
-			$response = rest_ensure_response( $data );
67
-			if ( is_wp_error( $data ) ) {
68
-				return $response;
69
-			}
70
-			$response->header( 'Access-Control-Allow-Origin', '*' );
71
-			$response->header( 'X-WordLift-Cache', 'MISS' );
65
+            $data     = $this->get_data( $request );
66
+            $response = rest_ensure_response( $data );
67
+            if ( is_wp_error( $data ) ) {
68
+                return $response;
69
+            }
70
+            $response->header( 'Access-Control-Allow-Origin', '*' );
71
+            $response->header( 'X-WordLift-Cache', 'MISS' );
72 72
 
73
-			return $response;
74
-		}
73
+            return $response;
74
+        }
75 75
 
76
-		// Create the cache key.
77
-		$cache_key_params = $request->get_params();
78
-		unset( $cache_key_params['uniqid'] );
79
-		unset( $cache_key_params['rest_route'] );
80
-		$cache_key = array( 'request_params' => $cache_key_params );
76
+        // Create the cache key.
77
+        $cache_key_params = $request->get_params();
78
+        unset( $cache_key_params['uniqid'] );
79
+        unset( $cache_key_params['rest_route'] );
80
+        $cache_key = array( 'request_params' => $cache_key_params );
81 81
 
82
-		// Create the TTL cache and try to get the results.
83
-		$cache         = new Ttl_Cache( $this->endpoint, static::CACHE_TTL );
84
-		$cache_results = $cache->get( $cache_key );
82
+        // Create the TTL cache and try to get the results.
83
+        $cache         = new Ttl_Cache( $this->endpoint, static::CACHE_TTL );
84
+        $cache_results = $cache->get( $cache_key );
85 85
 
86
-		if ( isset( $cache_results ) ) {
86
+        if ( isset( $cache_results ) ) {
87 87
 
88
-			$response = rest_ensure_response( $cache_results );
89
-			$response->header( 'Access-Control-Allow-Origin', '*' );
90
-			$response->header( 'X-WordLift-Cache', 'HIT' );
88
+            $response = rest_ensure_response( $cache_results );
89
+            $response->header( 'Access-Control-Allow-Origin', '*' );
90
+            $response->header( 'X-WordLift-Cache', 'HIT' );
91 91
 
92
-			return $response;
93
-		}
92
+            return $response;
93
+        }
94 94
 
95
-		$data     = $this->get_data( $request );
96
-		$response = rest_ensure_response( $data );
97
-		if ( is_wp_error( $data ) ) {
98
-			return $response;
99
-		}
100
-		$response->header( 'Access-Control-Allow-Origin', '*' );
101
-		$response->header( 'X-WordLift-Cache', 'MISS' );
95
+        $data     = $this->get_data( $request );
96
+        $response = rest_ensure_response( $data );
97
+        if ( is_wp_error( $data ) ) {
98
+            return $response;
99
+        }
100
+        $response->header( 'Access-Control-Allow-Origin', '*' );
101
+        $response->header( 'X-WordLift-Cache', 'MISS' );
102 102
 
103
-		// Put the result before sending the json to the client, since sending the json will terminate us.
104
-		$cache->put( $cache_key, $data );
103
+        // Put the result before sending the json to the client, since sending the json will terminate us.
104
+        $cache->put( $cache_key, $data );
105 105
 
106
-		return $response;
106
+        return $response;
107 107
 
108
-	}
108
+    }
109 109
 
110
-	private function is_endpoint() {
111
-		$compare_route = WL_REST_ROUTE_DEFAULT_NAMESPACE . $this->endpoint;
110
+    private function is_endpoint() {
111
+        $compare_route = WL_REST_ROUTE_DEFAULT_NAMESPACE . $this->endpoint;
112 112
 
113
-		// Directly accessing $_SERVER['REQUEST_URI'] or $_GET['rest_route'] here as it's too early to use global $wp reliably
113
+        // Directly accessing $_SERVER['REQUEST_URI'] or $_GET['rest_route'] here as it's too early to use global $wp reliably
114 114
 
115
-		if ( $_SERVER['REQUEST_URI'] && strpos( (string) $_SERVER['REQUEST_URI'], $compare_route ) ) {
116
-			return true;
117
-		}
118
-		if ( ! empty( $_GET['rest_route'] ) && strpos( (string) $_GET['rest_route'], $compare_route ) ) {
119
-			return true;
120
-		}
115
+        if ( $_SERVER['REQUEST_URI'] && strpos( (string) $_SERVER['REQUEST_URI'], $compare_route ) ) {
116
+            return true;
117
+        }
118
+        if ( ! empty( $_GET['rest_route'] ) && strpos( (string) $_GET['rest_route'], $compare_route ) ) {
119
+            return true;
120
+        }
121 121
 
122
-		return false;
123
-	}
122
+        return false;
123
+    }
124 124
 
125 125
 }
Please login to merge, or discard this patch.
Spacing   +42 added lines, -42 removed lines patch added patch discarded remove patch
@@ -15,107 +15,107 @@
 block discarded – undo
15 15
 	 */
16 16
 	const CACHE_TTL = 86400; // 24 hours
17 17
 
18
-	public function __construct( $endpoint, $args ) {
18
+	public function __construct($endpoint, $args) {
19 19
 
20 20
 		$scope          = $this;
21 21
 		$this->endpoint = $endpoint;
22 22
 		$this->args     = $args;
23 23
 
24 24
 		// Register rest route with callback
25
-		add_action( 'rest_api_init', function () use ( $scope ) {
26
-			register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, $scope->endpoint, array(
25
+		add_action('rest_api_init', function() use ($scope) {
26
+			register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, $scope->endpoint, array(
27 27
 				'methods'  => WP_REST_Server::READABLE,
28 28
 				'permission_callback' => '__return_true',
29
-				'callback' => array( $scope, 'rest_callback' ),
29
+				'callback' => array($scope, 'rest_callback'),
30 30
 				'args'     => $scope->args
31
-			) );
31
+			));
32 32
 		} );
33 33
 
34 34
 		// Optimizations: disable unneeded plugins on this specific REST call. WPSeo is slowing down the responses quite a bit.
35
-		add_action( 'plugins_loaded', function () use ( $scope ) {
35
+		add_action('plugins_loaded', function() use ($scope) {
36 36
 
37
-			if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST || ! $scope->is_endpoint() ) {
37
+			if ( ! defined('REST_REQUEST') || ! REST_REQUEST || ! $scope->is_endpoint()) {
38 38
 				return;
39 39
 			}
40 40
 
41
-			remove_action( 'plugins_loaded', 'rocket_init' );
42
-			remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 );
43
-			remove_action( 'plugins_loaded', 'wpseo_init', 14 );
44
-		}, 0 );
41
+			remove_action('plugins_loaded', 'rocket_init');
42
+			remove_action('plugins_loaded', 'wpseo_premium_init', 14);
43
+			remove_action('plugins_loaded', 'wpseo_init', 14);
44
+		}, 0);
45 45
 
46
-		add_action( 'init', function () use ( $scope ) {
46
+		add_action('init', function() use ($scope) {
47 47
 
48
-			if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST || ! $scope->is_endpoint() ) {
48
+			if ( ! defined('REST_REQUEST') || ! REST_REQUEST || ! $scope->is_endpoint()) {
49 49
 				return;
50 50
 			}
51 51
 
52
-			remove_action( 'init', 'wp_widgets_init', 1 );
53
-			remove_action( 'init', 'gglcptch_init' );
54
-		}, 0 );
52
+			remove_action('init', 'wp_widgets_init', 1);
53
+			remove_action('init', 'gglcptch_init');
54
+		}, 0);
55 55
 
56 56
 	}
57 57
 
58
-	public abstract function get_data( $request );
58
+	public abstract function get_data($request);
59 59
 
60
-	public function rest_callback( WP_REST_Request $request ) {
60
+	public function rest_callback(WP_REST_Request $request) {
61 61
 
62 62
 		// Respond from origin if TTL is 0
63
-		if ( static::CACHE_TTL == 0 ) {
63
+		if (static::CACHE_TTL == 0) {
64 64
 
65
-			$data     = $this->get_data( $request );
66
-			$response = rest_ensure_response( $data );
67
-			if ( is_wp_error( $data ) ) {
65
+			$data     = $this->get_data($request);
66
+			$response = rest_ensure_response($data);
67
+			if (is_wp_error($data)) {
68 68
 				return $response;
69 69
 			}
70
-			$response->header( 'Access-Control-Allow-Origin', '*' );
71
-			$response->header( 'X-WordLift-Cache', 'MISS' );
70
+			$response->header('Access-Control-Allow-Origin', '*');
71
+			$response->header('X-WordLift-Cache', 'MISS');
72 72
 
73 73
 			return $response;
74 74
 		}
75 75
 
76 76
 		// Create the cache key.
77 77
 		$cache_key_params = $request->get_params();
78
-		unset( $cache_key_params['uniqid'] );
79
-		unset( $cache_key_params['rest_route'] );
80
-		$cache_key = array( 'request_params' => $cache_key_params );
78
+		unset($cache_key_params['uniqid']);
79
+		unset($cache_key_params['rest_route']);
80
+		$cache_key = array('request_params' => $cache_key_params);
81 81
 
82 82
 		// Create the TTL cache and try to get the results.
83
-		$cache         = new Ttl_Cache( $this->endpoint, static::CACHE_TTL );
84
-		$cache_results = $cache->get( $cache_key );
83
+		$cache         = new Ttl_Cache($this->endpoint, static::CACHE_TTL);
84
+		$cache_results = $cache->get($cache_key);
85 85
 
86
-		if ( isset( $cache_results ) ) {
86
+		if (isset($cache_results)) {
87 87
 
88
-			$response = rest_ensure_response( $cache_results );
89
-			$response->header( 'Access-Control-Allow-Origin', '*' );
90
-			$response->header( 'X-WordLift-Cache', 'HIT' );
88
+			$response = rest_ensure_response($cache_results);
89
+			$response->header('Access-Control-Allow-Origin', '*');
90
+			$response->header('X-WordLift-Cache', 'HIT');
91 91
 
92 92
 			return $response;
93 93
 		}
94 94
 
95
-		$data     = $this->get_data( $request );
96
-		$response = rest_ensure_response( $data );
97
-		if ( is_wp_error( $data ) ) {
95
+		$data     = $this->get_data($request);
96
+		$response = rest_ensure_response($data);
97
+		if (is_wp_error($data)) {
98 98
 			return $response;
99 99
 		}
100
-		$response->header( 'Access-Control-Allow-Origin', '*' );
101
-		$response->header( 'X-WordLift-Cache', 'MISS' );
100
+		$response->header('Access-Control-Allow-Origin', '*');
101
+		$response->header('X-WordLift-Cache', 'MISS');
102 102
 
103 103
 		// Put the result before sending the json to the client, since sending the json will terminate us.
104
-		$cache->put( $cache_key, $data );
104
+		$cache->put($cache_key, $data);
105 105
 
106 106
 		return $response;
107 107
 
108 108
 	}
109 109
 
110 110
 	private function is_endpoint() {
111
-		$compare_route = WL_REST_ROUTE_DEFAULT_NAMESPACE . $this->endpoint;
111
+		$compare_route = WL_REST_ROUTE_DEFAULT_NAMESPACE.$this->endpoint;
112 112
 
113 113
 		// Directly accessing $_SERVER['REQUEST_URI'] or $_GET['rest_route'] here as it's too early to use global $wp reliably
114 114
 
115
-		if ( $_SERVER['REQUEST_URI'] && strpos( (string) $_SERVER['REQUEST_URI'], $compare_route ) ) {
115
+		if ($_SERVER['REQUEST_URI'] && strpos((string) $_SERVER['REQUEST_URI'], $compare_route)) {
116 116
 			return true;
117 117
 		}
118
-		if ( ! empty( $_GET['rest_route'] ) && strpos( (string) $_GET['rest_route'], $compare_route ) ) {
118
+		if ( ! empty($_GET['rest_route']) && strpos((string) $_GET['rest_route'], $compare_route)) {
119 119
 			return true;
120 120
 		}
121 121
 
Please login to merge, or discard this patch.
src/modules/core/wordlift_core_entity_api.php 2 patches
Indentation   +84 added lines, -84 removed lines patch added patch discarded remove patch
@@ -20,54 +20,54 @@  discard block
 block discarded – undo
20 20
  */
21 21
 function wl_entity_get_by_title( $title, $autocomplete = false, $include_alias = true ) {
22 22
 
23
-	global $wpdb;
24
-
25
-	// Search by substring
26
-	if ( $autocomplete ) {
27
-		$title = '%' . $title . '%';
28
-	}
29
-
30
-	// The title is a LIKE query.
31
-	$query = "SELECT DISTINCT p.ID AS id, p.post_title AS title, t.name AS schema_type_name, t.slug AS type_slug"
32
-			 . " FROM $wpdb->posts p, $wpdb->term_taxonomy tt, $wpdb->term_relationships tr, $wpdb->terms t"
33
-			 . "  WHERE p.post_title LIKE %s"
34
-			 . "   AND t.term_id = tt.term_id"
35
-			 . "   AND tt.taxonomy = %s"
36
-			 . "   AND tt.term_taxonomy_id = tr.term_taxonomy_id"
37
-			 . "   AND tr.object_id = p.ID"
38
-			 // Ensure we don't load entities from the trash, see https://github.com/insideout10/wordlift-plugin/issues/278.
39
-			 . "   AND p.post_status != 'trash'";
40
-
41
-	$params = array(
42
-		$title,
43
-		Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
44
-	);
45
-
46
-	if ( $include_alias ) {
47
-
48
-		$query .= " UNION"
49
-				  . "  SELECT DISTINCT p.ID AS id, CONCAT( m.meta_value, ' (', p.post_title, ')' ) AS title, t.name AS schema_type_name, t.slug AS type_slug"
50
-				  . "  FROM $wpdb->posts p, $wpdb->term_taxonomy tt, $wpdb->term_relationships tr, $wpdb->terms t, $wpdb->postmeta m"
51
-				  . "   WHERE m.meta_key = %s AND m.meta_value LIKE %s"
52
-				  . "    AND m.post_id = p.ID"
53
-				  . "    AND t.term_id = tt.term_id"
54
-				  . "    AND tt.taxonomy = %s"
55
-				  . "    AND tt.term_taxonomy_id = tr.term_taxonomy_id"
56
-				  . "    AND tr.object_id = p.ID"
57
-				  // Ensure we don't load entities from the trash, see https://github.com/insideout10/wordlift-plugin/issues/278.
58
-				  . "    AND p.post_status != 'trash'";
59
-
60
-		$params = array_merge( $params, array(
61
-			Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY,
62
-			$title,
63
-			Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
64
-		) );
65
-	}
66
-
67
-	return $wpdb->get_results( $wpdb->prepare(
68
-		$query,
69
-		$params
70
-	) );
23
+    global $wpdb;
24
+
25
+    // Search by substring
26
+    if ( $autocomplete ) {
27
+        $title = '%' . $title . '%';
28
+    }
29
+
30
+    // The title is a LIKE query.
31
+    $query = "SELECT DISTINCT p.ID AS id, p.post_title AS title, t.name AS schema_type_name, t.slug AS type_slug"
32
+                . " FROM $wpdb->posts p, $wpdb->term_taxonomy tt, $wpdb->term_relationships tr, $wpdb->terms t"
33
+                . "  WHERE p.post_title LIKE %s"
34
+                . "   AND t.term_id = tt.term_id"
35
+                . "   AND tt.taxonomy = %s"
36
+                . "   AND tt.term_taxonomy_id = tr.term_taxonomy_id"
37
+                . "   AND tr.object_id = p.ID"
38
+                // Ensure we don't load entities from the trash, see https://github.com/insideout10/wordlift-plugin/issues/278.
39
+                . "   AND p.post_status != 'trash'";
40
+
41
+    $params = array(
42
+        $title,
43
+        Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
44
+    );
45
+
46
+    if ( $include_alias ) {
47
+
48
+        $query .= " UNION"
49
+                    . "  SELECT DISTINCT p.ID AS id, CONCAT( m.meta_value, ' (', p.post_title, ')' ) AS title, t.name AS schema_type_name, t.slug AS type_slug"
50
+                    . "  FROM $wpdb->posts p, $wpdb->term_taxonomy tt, $wpdb->term_relationships tr, $wpdb->terms t, $wpdb->postmeta m"
51
+                    . "   WHERE m.meta_key = %s AND m.meta_value LIKE %s"
52
+                    . "    AND m.post_id = p.ID"
53
+                    . "    AND t.term_id = tt.term_id"
54
+                    . "    AND tt.taxonomy = %s"
55
+                    . "    AND tt.term_taxonomy_id = tr.term_taxonomy_id"
56
+                    . "    AND tr.object_id = p.ID"
57
+                    // Ensure we don't load entities from the trash, see https://github.com/insideout10/wordlift-plugin/issues/278.
58
+                    . "    AND p.post_status != 'trash'";
59
+
60
+        $params = array_merge( $params, array(
61
+            Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY,
62
+            $title,
63
+            Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
64
+        ) );
65
+    }
66
+
67
+    return $wpdb->get_results( $wpdb->prepare(
68
+        $query,
69
+        $params
70
+    ) );
71 71
 }
72 72
 
73 73
 /**
@@ -77,42 +77,42 @@  discard block
 block discarded – undo
77 77
  */
78 78
 function wl_entity_ajax_get_by_title() {
79 79
 
80
-	// `wl_entity_metaboxes_utilities.js` still uses `GET`.
81
-	//
82
-	// See https://github.com/insideout10/wordlift-plugin/issues/438.
83
-	// Get the title to search.
84
-	if ( empty( $_POST['title'] ) && empty( $_GET['title'] ) ) {
85
-		@ob_clean();
86
-		wp_send_json_error( 'The title parameter is required.' );
87
-	}
88
-
89
-	// `wl_entity_metaboxes_utilities.js` still uses `GET`.
90
-	//
91
-	// See https://github.com/insideout10/wordlift-plugin/issues/438.
92
-	$title = $_POST['title'] ?: $_GET['title'];
93
-	$title = (string) $title;
94
-
95
-	// Are we searching for a specific title or for a containing title?
96
-	$autocomplete = isset( $_GET['autocomplete'] );
97
-
98
-	// Are we searching also for the aliases?
99
-	$include_alias = isset( $_GET['alias'] );
100
-
101
-	// Get the edit link.
102
-	$post_type_object = get_post_type_object( Wordlift_Entity_Service::TYPE_NAME );
103
-	$edit_link        = $post_type_object->_edit_link . '&action=edit';
104
-
105
-	// Prepare the response with the edit link.
106
-	$response = array(
107
-		'edit_link' => $edit_link,
108
-		'results'   => wl_entity_get_by_title( $title, $autocomplete, $include_alias ),
109
-	);
110
-
111
-	// Clean any buffer.
112
-	@ob_clean();
113
-
114
-	// Send the success response.
115
-	wp_send_json_success( $response );
80
+    // `wl_entity_metaboxes_utilities.js` still uses `GET`.
81
+    //
82
+    // See https://github.com/insideout10/wordlift-plugin/issues/438.
83
+    // Get the title to search.
84
+    if ( empty( $_POST['title'] ) && empty( $_GET['title'] ) ) {
85
+        @ob_clean();
86
+        wp_send_json_error( 'The title parameter is required.' );
87
+    }
88
+
89
+    // `wl_entity_metaboxes_utilities.js` still uses `GET`.
90
+    //
91
+    // See https://github.com/insideout10/wordlift-plugin/issues/438.
92
+    $title = $_POST['title'] ?: $_GET['title'];
93
+    $title = (string) $title;
94
+
95
+    // Are we searching for a specific title or for a containing title?
96
+    $autocomplete = isset( $_GET['autocomplete'] );
97
+
98
+    // Are we searching also for the aliases?
99
+    $include_alias = isset( $_GET['alias'] );
100
+
101
+    // Get the edit link.
102
+    $post_type_object = get_post_type_object( Wordlift_Entity_Service::TYPE_NAME );
103
+    $edit_link        = $post_type_object->_edit_link . '&action=edit';
104
+
105
+    // Prepare the response with the edit link.
106
+    $response = array(
107
+        'edit_link' => $edit_link,
108
+        'results'   => wl_entity_get_by_title( $title, $autocomplete, $include_alias ),
109
+    );
110
+
111
+    // Clean any buffer.
112
+    @ob_clean();
113
+
114
+    // Send the success response.
115
+    wp_send_json_success( $response );
116 116
 
117 117
 }
118 118
 
Please login to merge, or discard this patch.
Spacing   +17 added lines, -17 removed lines patch added patch discarded remove patch
@@ -18,13 +18,13 @@  discard block
 block discarded – undo
18 18
  *
19 19
  * @return array An array of WP_Post instances.
20 20
  */
21
-function wl_entity_get_by_title( $title, $autocomplete = false, $include_alias = true ) {
21
+function wl_entity_get_by_title($title, $autocomplete = false, $include_alias = true) {
22 22
 
23 23
 	global $wpdb;
24 24
 
25 25
 	// Search by substring
26
-	if ( $autocomplete ) {
27
-		$title = '%' . $title . '%';
26
+	if ($autocomplete) {
27
+		$title = '%'.$title.'%';
28 28
 	}
29 29
 
30 30
 	// The title is a LIKE query.
@@ -43,7 +43,7 @@  discard block
 block discarded – undo
43 43
 		Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
44 44
 	);
45 45
 
46
-	if ( $include_alias ) {
46
+	if ($include_alias) {
47 47
 
48 48
 		$query .= " UNION"
49 49
 				  . "  SELECT DISTINCT p.ID AS id, CONCAT( m.meta_value, ' (', p.post_title, ')' ) AS title, t.name AS schema_type_name, t.slug AS type_slug"
@@ -57,17 +57,17 @@  discard block
 block discarded – undo
57 57
 				  // Ensure we don't load entities from the trash, see https://github.com/insideout10/wordlift-plugin/issues/278.
58 58
 				  . "    AND p.post_status != 'trash'";
59 59
 
60
-		$params = array_merge( $params, array(
60
+		$params = array_merge($params, array(
61 61
 			Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY,
62 62
 			$title,
63 63
 			Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
64
-		) );
64
+		));
65 65
 	}
66 66
 
67
-	return $wpdb->get_results( $wpdb->prepare(
67
+	return $wpdb->get_results($wpdb->prepare(
68 68
 		$query,
69 69
 		$params
70
-	) );
70
+	));
71 71
 }
72 72
 
73 73
 /**
@@ -81,9 +81,9 @@  discard block
 block discarded – undo
81 81
 	//
82 82
 	// See https://github.com/insideout10/wordlift-plugin/issues/438.
83 83
 	// Get the title to search.
84
-	if ( empty( $_POST['title'] ) && empty( $_GET['title'] ) ) {
84
+	if (empty($_POST['title']) && empty($_GET['title'])) {
85 85
 		@ob_clean();
86
-		wp_send_json_error( 'The title parameter is required.' );
86
+		wp_send_json_error('The title parameter is required.');
87 87
 	}
88 88
 
89 89
 	// `wl_entity_metaboxes_utilities.js` still uses `GET`.
@@ -93,27 +93,27 @@  discard block
 block discarded – undo
93 93
 	$title = (string) $title;
94 94
 
95 95
 	// Are we searching for a specific title or for a containing title?
96
-	$autocomplete = isset( $_GET['autocomplete'] );
96
+	$autocomplete = isset($_GET['autocomplete']);
97 97
 
98 98
 	// Are we searching also for the aliases?
99
-	$include_alias = isset( $_GET['alias'] );
99
+	$include_alias = isset($_GET['alias']);
100 100
 
101 101
 	// Get the edit link.
102
-	$post_type_object = get_post_type_object( Wordlift_Entity_Service::TYPE_NAME );
103
-	$edit_link        = $post_type_object->_edit_link . '&action=edit';
102
+	$post_type_object = get_post_type_object(Wordlift_Entity_Service::TYPE_NAME);
103
+	$edit_link        = $post_type_object->_edit_link.'&action=edit';
104 104
 
105 105
 	// Prepare the response with the edit link.
106 106
 	$response = array(
107 107
 		'edit_link' => $edit_link,
108
-		'results'   => wl_entity_get_by_title( $title, $autocomplete, $include_alias ),
108
+		'results'   => wl_entity_get_by_title($title, $autocomplete, $include_alias),
109 109
 	);
110 110
 
111 111
 	// Clean any buffer.
112 112
 	@ob_clean();
113 113
 
114 114
 	// Send the success response.
115
-	wp_send_json_success( $response );
115
+	wp_send_json_success($response);
116 116
 
117 117
 }
118 118
 
119
-add_action( 'wp_ajax_entity_by_title', 'wl_entity_ajax_get_by_title' );
119
+add_action('wp_ajax_entity_by_title', 'wl_entity_ajax_get_by_title');
Please login to merge, or discard this patch.
src/modules/linked_data/wordlift_linked_data.php 2 patches
Indentation   +425 added lines, -425 removed lines patch added patch discarded remove patch
@@ -20,60 +20,60 @@  discard block
 block discarded – undo
20 20
  */
21 21
 function wl_linked_data_save_post( $post_id ) {
22 22
 
23
-	$log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post' );
24
-
25
-	$log->trace( "Saving post $post_id to Linked Data..." );
26
-
27
-	// If it's not numeric exit from here.
28
-	if ( ! is_numeric( $post_id ) || is_numeric( wp_is_post_revision( $post_id ) ) ) {
29
-		$log->debug( "Skipping $post_id, because id is not numeric or is a post revision." );
30
-
31
-		return;
32
-	}
33
-
34
-	// Get the post type and check whether it supports the editor.
35
-	//
36
-	// @see https://github.com/insideout10/wordlift-plugin/issues/659.
37
-	$post_type = get_post_type( $post_id );
38
-	/**
39
-	 * Use `wl_post_type_supports_editor` which calls a filter to allow 3rd parties to change the setting.
40
-	 *
41
-	 * @since 3.19.4
42
-	 *
43
-	 * @see https://github.com/insideout10/wordlift-plugin/issues/847.
44
-	 */
45
-	$is_editor_supported = wl_post_type_supports_editor( $post_type );
46
-
47
-	// Bail out if it's not an entity.
48
-	if ( ! $is_editor_supported ) {
49
-		$log->debug( "Skipping $post_id, because $post_type doesn't support the editor (content)." );
50
-
51
-		return;
52
-	}
53
-
54
-	/**
55
-	 * Only process valid post types
56
-	 *
57
-	 * @since 3.25.6
58
-	 *
59
-	 */
60
-	$supported_types = Wordlift_Entity_Service::valid_entity_post_types();
61
-
62
-	// Bail out if it's not a valid entity.
63
-	if ( ! in_array( $post_type, $supported_types ) ) {
64
-		$log->debug( "Skipping $post_id, because $post_type is not a valid entity." );
65
-
66
-		return;
67
-	}
68
-
69
-	// Unhook this function so it doesn't loop infinitely.
70
-	remove_action( 'save_post', 'wl_linked_data_save_post' );
71
-
72
-	// raise the *wl_linked_data_save_post* event.
73
-	do_action( 'wl_linked_data_save_post', $post_id );
74
-
75
-	// Re-hook this function.
76
-	add_action( 'save_post', 'wl_linked_data_save_post' );
23
+    $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post' );
24
+
25
+    $log->trace( "Saving post $post_id to Linked Data..." );
26
+
27
+    // If it's not numeric exit from here.
28
+    if ( ! is_numeric( $post_id ) || is_numeric( wp_is_post_revision( $post_id ) ) ) {
29
+        $log->debug( "Skipping $post_id, because id is not numeric or is a post revision." );
30
+
31
+        return;
32
+    }
33
+
34
+    // Get the post type and check whether it supports the editor.
35
+    //
36
+    // @see https://github.com/insideout10/wordlift-plugin/issues/659.
37
+    $post_type = get_post_type( $post_id );
38
+    /**
39
+     * Use `wl_post_type_supports_editor` which calls a filter to allow 3rd parties to change the setting.
40
+     *
41
+     * @since 3.19.4
42
+     *
43
+     * @see https://github.com/insideout10/wordlift-plugin/issues/847.
44
+     */
45
+    $is_editor_supported = wl_post_type_supports_editor( $post_type );
46
+
47
+    // Bail out if it's not an entity.
48
+    if ( ! $is_editor_supported ) {
49
+        $log->debug( "Skipping $post_id, because $post_type doesn't support the editor (content)." );
50
+
51
+        return;
52
+    }
53
+
54
+    /**
55
+     * Only process valid post types
56
+     *
57
+     * @since 3.25.6
58
+     *
59
+     */
60
+    $supported_types = Wordlift_Entity_Service::valid_entity_post_types();
61
+
62
+    // Bail out if it's not a valid entity.
63
+    if ( ! in_array( $post_type, $supported_types ) ) {
64
+        $log->debug( "Skipping $post_id, because $post_type is not a valid entity." );
65
+
66
+        return;
67
+    }
68
+
69
+    // Unhook this function so it doesn't loop infinitely.
70
+    remove_action( 'save_post', 'wl_linked_data_save_post' );
71
+
72
+    // raise the *wl_linked_data_save_post* event.
73
+    do_action( 'wl_linked_data_save_post', $post_id );
74
+
75
+    // Re-hook this function.
76
+    add_action( 'save_post', 'wl_linked_data_save_post' );
77 77
 }
78 78
 
79 79
 add_action( 'save_post', 'wl_linked_data_save_post' );
@@ -88,176 +88,176 @@  discard block
 block discarded – undo
88 88
  */
89 89
 function wl_linked_data_save_post_and_related_entities( $post_id ) {
90 90
 
91
-	$log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post_and_related_entities' );
91
+    $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post_and_related_entities' );
92 92
 
93
-	$log->trace( "Saving $post_id to Linked Data along with related entities..." );
93
+    $log->trace( "Saving $post_id to Linked Data along with related entities..." );
94 94
 
95
-	// Ignore auto-saves
96
-	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
97
-		$log->trace( 'Doing autosave, skipping...' );
95
+    // Ignore auto-saves
96
+    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
97
+        $log->trace( 'Doing autosave, skipping...' );
98 98
 
99
-		return;
100
-	}
99
+        return;
100
+    }
101 101
 
102
-	// get the current post.
103
-	$post = get_post( $post_id );
102
+    // get the current post.
103
+    $post = get_post( $post_id );
104 104
 
105
-	remove_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' );
105
+    remove_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' );
106 106
 
107
-	// wl_write_log( "[ post id :: $post_id ][ autosave :: false ][ post type :: $post->post_type ]" );
107
+    // wl_write_log( "[ post id :: $post_id ][ autosave :: false ][ post type :: $post->post_type ]" );
108 108
 
109
-	// Get the entity service instance.
110
-	$entity_service = Wordlift_Entity_Service::get_instance();
109
+    // Get the entity service instance.
110
+    $entity_service = Wordlift_Entity_Service::get_instance();
111 111
 
112
-	// Store mapping between tmp new entities uris and real new entities uri
113
-	$entities_uri_mapping = array();
112
+    // Store mapping between tmp new entities uris and real new entities uri
113
+    $entities_uri_mapping = array();
114 114
 
115
-	// Save the entities coming with POST data.
116
-	if ( isset( $_POST['wl_entities'] ) && isset( $_POST['wl_boxes'] ) ) {
117
-
118
-		wl_write_log( "[ post id :: $post_id ][ POST(wl_entities) :: " );
119
-		wl_write_log( json_encode( $_POST['wl_entities'] ) );
120
-		wl_write_log( "]" );
121
-		wl_write_log( "[ post id :: $post_id ][ POST(wl_boxes) :: " );
122
-		wl_write_log( json_encode( $_POST['wl_boxes'], true ) );
123
-		wl_write_log( "]" );
115
+    // Save the entities coming with POST data.
116
+    if ( isset( $_POST['wl_entities'] ) && isset( $_POST['wl_boxes'] ) ) {
117
+
118
+        wl_write_log( "[ post id :: $post_id ][ POST(wl_entities) :: " );
119
+        wl_write_log( json_encode( $_POST['wl_entities'] ) );
120
+        wl_write_log( "]" );
121
+        wl_write_log( "[ post id :: $post_id ][ POST(wl_boxes) :: " );
122
+        wl_write_log( json_encode( $_POST['wl_boxes'], true ) );
123
+        wl_write_log( "]" );
124 124
 
125
-		$entities_via_post = $_POST['wl_entities'];
126
-
127
-		foreach ( $entities_via_post as $entity_uri => $entity ) {
125
+        $entities_via_post = $_POST['wl_entities'];
126
+
127
+        foreach ( $entities_via_post as $entity_uri => $entity ) {
128 128
 
129
-			// Only if the current entity is created from scratch let's avoid to
130
-			// create more than one entity with same label & entity type.
131
-			$entity_type = ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) ?
132
-				$entity['main_type'] : null;
133
-
134
-			// Look if current entity uri matches an internal existing entity, meaning:
135
-			// 1. when $entity_uri is an internal uri
136
-			// 2. when $entity_uri is an external uri used as sameAs of an internal entity
137
-			$ie = $entity_service->get_entity_post_by_uri( $entity_uri );
129
+            // Only if the current entity is created from scratch let's avoid to
130
+            // create more than one entity with same label & entity type.
131
+            $entity_type = ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) ?
132
+                $entity['main_type'] : null;
133
+
134
+            // Look if current entity uri matches an internal existing entity, meaning:
135
+            // 1. when $entity_uri is an internal uri
136
+            // 2. when $entity_uri is an external uri used as sameAs of an internal entity
137
+            $ie = $entity_service->get_entity_post_by_uri( $entity_uri );
138 138
 
139
-			// Dont save the entities which are not found, but also local.
140
-			if ( $ie === null &&  Wordlift_Entity_Uri_Service::get_instance()->is_internal( $entity_uri ) ) {
141
-				continue;
142
-			}
143
-
144
-			// Detect the uri depending if is an existing or a new entity
145
-			$uri = ( null === $ie ) ?
146
-				Wordlift_Uri_Service::get_instance()->build_uri(
147
-					$entity['label'],
148
-					Wordlift_Entity_Service::TYPE_NAME,
149
-					$entity_type
150
-				) : wl_get_entity_uri( $ie->ID );
151
-
152
-			wl_write_log( "Map $entity_uri on $uri" );
153
-			$entities_uri_mapping[ $entity_uri ] = $uri;
154
-
155
-			// Local entities have a tmp uri with 'local-entity-' prefix
156
-			// These uris need to be rewritten here and replaced in the content
157
-			if ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) {
158
-				// Override the entity obj
159
-				$entity['uri'] = $uri;
160
-			}
161
-
162
-			// Update entity data with related post
163
-			$entity['related_post_id'] = $post_id;
164
-
165
-			// Save the entity if is a new entity
166
-			if ( null === $ie ) {
167
-				wl_save_entity( $entity );
168
-			}
169
-
170
-		}
171
-
172
-	}
173
-
174
-	// Replace tmp uris in content post if needed
175
-	$updated_post_content = $post->post_content;
176
-
177
-	// Update the post content if we found mappings of new entities.
178
-	if ( ! empty( $entities_uri_mapping ) ) {
179
-		// Save each entity and store the post id.
180
-		foreach ( $entities_uri_mapping as $tmp_uri => $uri ) {
181
-			if ( 1 !== preg_match( '@^(https?://|local-entity-)@', $tmp_uri ) ) {
182
-				continue;
183
-			}
184
-
185
-			$updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content );
186
-		}
187
-
188
-		// Update the post content.
189
-		/**
190
-		 * Note: wp_update_post do stripslashes before saving the
191
-		 * content, so add the slashes to prevent back slash getting
192
-		 * removed.
193
-		 */
194
-		wp_update_post( array(
195
-			'ID'           => $post->ID,
196
-			'post_content' => addslashes( $updated_post_content ),
197
-		) );
198
-	}
199
-	// Reset previously saved instances.
200
-	wl_core_delete_relation_instances( $post_id );
201
-
202
-
203
-	$relations = Object_Relation_Service::get_instance()
204
-	                                    ->get_relations_from_content( $updated_post_content, Object_Type_Enum::POST );
205
-
206
-	// Save relation instances
207
-	foreach ( $relations as $relation ) {
208
-
209
-		wl_core_add_relation_instance(
210
-			// subject id.
211
-			$post_id,
212
-			// what, where, when, who
213
-			$relation->get_relation_type(),
214
-			// object id.
215
-			$relation->get_object_id(),
216
-			// Subject type.
217
-			$relation->get_subject_type(),
218
-			// Object type.
219
-			$relation->get_object_type()
220
-		);
221
-
222
-	}
223
-
224
-	if ( isset( $_POST['wl_entities'] ) ) {
225
-		// Save post metadata if available
226
-		$metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ?
227
-			$_POST['wl_metadata'] : array();
228
-
229
-		$fields = array(
230
-			Wordlift_Schema_Service::FIELD_LOCATION_CREATED,
231
-			Wordlift_Schema_Service::FIELD_TOPIC,
232
-		);
233
-
234
-		// Unlink topic taxonomy terms
235
-		Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for( $post->ID );
236
-
237
-		foreach ( $fields as $field ) {
238
-
239
-			// Delete current values
240
-			delete_post_meta( $post->ID, $field );
241
-			// Retrieve the entity uri
242
-			$uri = ( isset( $metadata_via_post[ $field ] ) ) ?
243
-				stripslashes( $metadata_via_post[ $field ] ) : '';
244
-
245
-			$entity = $entity_service->get_entity_post_by_uri( $uri );
246
-
247
-			if ( $entity ) {
248
-				add_post_meta( $post->ID, $field, $entity->ID, true );
249
-				// Set also the topic taxonomy
250
-				if ( $field === Wordlift_Schema_Service::FIELD_TOPIC ) {
251
-					Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for( $post->ID, $entity );
252
-				}
253
-			}
254
-		}
255
-	}
256
-
257
-	// Push the post to Redlink.
258
-	do_action( 'wl_legacy_linked_data__push', $post->ID );
259
-
260
-	add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' );
139
+            // Dont save the entities which are not found, but also local.
140
+            if ( $ie === null &&  Wordlift_Entity_Uri_Service::get_instance()->is_internal( $entity_uri ) ) {
141
+                continue;
142
+            }
143
+
144
+            // Detect the uri depending if is an existing or a new entity
145
+            $uri = ( null === $ie ) ?
146
+                Wordlift_Uri_Service::get_instance()->build_uri(
147
+                    $entity['label'],
148
+                    Wordlift_Entity_Service::TYPE_NAME,
149
+                    $entity_type
150
+                ) : wl_get_entity_uri( $ie->ID );
151
+
152
+            wl_write_log( "Map $entity_uri on $uri" );
153
+            $entities_uri_mapping[ $entity_uri ] = $uri;
154
+
155
+            // Local entities have a tmp uri with 'local-entity-' prefix
156
+            // These uris need to be rewritten here and replaced in the content
157
+            if ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) {
158
+                // Override the entity obj
159
+                $entity['uri'] = $uri;
160
+            }
161
+
162
+            // Update entity data with related post
163
+            $entity['related_post_id'] = $post_id;
164
+
165
+            // Save the entity if is a new entity
166
+            if ( null === $ie ) {
167
+                wl_save_entity( $entity );
168
+            }
169
+
170
+        }
171
+
172
+    }
173
+
174
+    // Replace tmp uris in content post if needed
175
+    $updated_post_content = $post->post_content;
176
+
177
+    // Update the post content if we found mappings of new entities.
178
+    if ( ! empty( $entities_uri_mapping ) ) {
179
+        // Save each entity and store the post id.
180
+        foreach ( $entities_uri_mapping as $tmp_uri => $uri ) {
181
+            if ( 1 !== preg_match( '@^(https?://|local-entity-)@', $tmp_uri ) ) {
182
+                continue;
183
+            }
184
+
185
+            $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content );
186
+        }
187
+
188
+        // Update the post content.
189
+        /**
190
+         * Note: wp_update_post do stripslashes before saving the
191
+         * content, so add the slashes to prevent back slash getting
192
+         * removed.
193
+         */
194
+        wp_update_post( array(
195
+            'ID'           => $post->ID,
196
+            'post_content' => addslashes( $updated_post_content ),
197
+        ) );
198
+    }
199
+    // Reset previously saved instances.
200
+    wl_core_delete_relation_instances( $post_id );
201
+
202
+
203
+    $relations = Object_Relation_Service::get_instance()
204
+                                        ->get_relations_from_content( $updated_post_content, Object_Type_Enum::POST );
205
+
206
+    // Save relation instances
207
+    foreach ( $relations as $relation ) {
208
+
209
+        wl_core_add_relation_instance(
210
+            // subject id.
211
+            $post_id,
212
+            // what, where, when, who
213
+            $relation->get_relation_type(),
214
+            // object id.
215
+            $relation->get_object_id(),
216
+            // Subject type.
217
+            $relation->get_subject_type(),
218
+            // Object type.
219
+            $relation->get_object_type()
220
+        );
221
+
222
+    }
223
+
224
+    if ( isset( $_POST['wl_entities'] ) ) {
225
+        // Save post metadata if available
226
+        $metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ?
227
+            $_POST['wl_metadata'] : array();
228
+
229
+        $fields = array(
230
+            Wordlift_Schema_Service::FIELD_LOCATION_CREATED,
231
+            Wordlift_Schema_Service::FIELD_TOPIC,
232
+        );
233
+
234
+        // Unlink topic taxonomy terms
235
+        Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for( $post->ID );
236
+
237
+        foreach ( $fields as $field ) {
238
+
239
+            // Delete current values
240
+            delete_post_meta( $post->ID, $field );
241
+            // Retrieve the entity uri
242
+            $uri = ( isset( $metadata_via_post[ $field ] ) ) ?
243
+                stripslashes( $metadata_via_post[ $field ] ) : '';
244
+
245
+            $entity = $entity_service->get_entity_post_by_uri( $uri );
246
+
247
+            if ( $entity ) {
248
+                add_post_meta( $post->ID, $field, $entity->ID, true );
249
+                // Set also the topic taxonomy
250
+                if ( $field === Wordlift_Schema_Service::FIELD_TOPIC ) {
251
+                    Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for( $post->ID, $entity );
252
+                }
253
+            }
254
+        }
255
+    }
256
+
257
+    // Push the post to Redlink.
258
+    do_action( 'wl_legacy_linked_data__push', $post->ID );
259
+
260
+    add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' );
261 261
 }
262 262
 
263 263
 add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' );
@@ -280,203 +280,203 @@  discard block
 block discarded – undo
280 280
  */
281 281
 function wl_save_entity( $entity_data ) {
282 282
 
283
-	// Required for REST API calls
284
-	if ( ! function_exists( 'wp_crop_image' ) ) {
285
-		require_once( ABSPATH . 'wp-admin/includes/image.php' );
286
-	}
283
+    // Required for REST API calls
284
+    if ( ! function_exists( 'wp_crop_image' ) ) {
285
+        require_once( ABSPATH . 'wp-admin/includes/image.php' );
286
+    }
287 287
 
288
-	$log = Wordlift_Log_Service::get_logger( 'wl_save_entity' );
288
+    $log = Wordlift_Log_Service::get_logger( 'wl_save_entity' );
289 289
 
290
-	$uri = $entity_data['uri'];
291
-	/*
290
+    $uri = $entity_data['uri'];
291
+    /*
292 292
 	 * Data is coming from a $_POST, sanitize it.
293 293
 	 *
294 294
 	 * @since 3.19.4
295 295
 	 *
296 296
 	 * @see https://github.com/insideout10/wordlift-plugin/issues/841
297 297
 	 */
298
-	$label            = preg_replace( '/\xEF\xBB\xBF/', '', sanitize_text_field( $entity_data['label'] ) );
299
-	$type_uri         = $entity_data['main_type'];
300
-	$entity_types     = isset( $entity_data['type'] ) ? $entity_data['type'] : array();
301
-	$description      = $entity_data['description'];
302
-	$images           = isset( $entity_data['image'] ) ? (array) $entity_data['image'] : array();
303
-	$same_as          = isset( $entity_data['sameas'] ) ? (array) $entity_data['sameas'] : array();
304
-	$related_post_id  = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null;
305
-	$other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array();
306
-
307
-	// Get the synonyms.
308
-	$synonyms = isset( $entity_data['synonym'] ) ? $entity_data['synonym'] : array();
309
-
310
-	// Check whether an entity already exists with the provided URI.
311
-	if ( null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ) ) {
312
-		$log->debug( "Post already exists for URI $uri." );
313
-
314
-		return $post;
315
-	}
316
-
317
-	// Prepare properties of the new entity.
318
-	$params = array(
319
-		'post_status'  => ( is_numeric( $related_post_id ) ? get_post_status( $related_post_id ) : 'draft' ),
320
-		'post_type'    => Wordlift_Entity_Service::TYPE_NAME,
321
-		'post_title'   => $label,
322
-		'post_content' => $description,
323
-		'post_excerpt' => '',
324
-		// Ensure we're using a valid slug. We're not overwriting an existing
325
-		// entity with a post_name already set, since this call is made only for
326
-		// new entities.
327
-		//
328
-		// See https://github.com/insideout10/wordlift-plugin/issues/282
329
-		'post_name'    => sanitize_title( $label ),
330
-	);
331
-
332
-	// If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over"
333
-	// the local SEO form values to the created entity (https://github.com/insideout10/wordlift-plugin/issues/156)
334
-	// Same thing applies to SEO Ultimate (https://github.com/insideout10/wordlift-plugin/issues/148)
335
-	// This does NOT affect saving an entity from the entity admin page since this function is called when an entity
336
-	// is created when saving a post.
337
-	global $wpseo_metabox, $seo_ultimate;
338
-	if ( isset( $wpseo_metabox ) ) {
339
-		remove_action( 'wp_insert_post', array(
340
-			$wpseo_metabox,
341
-			'save_postdata',
342
-		) );
343
-	}
344
-
345
-	if ( isset( $seo_ultimate ) ) {
346
-		remove_action( 'save_post', array(
347
-			$seo_ultimate,
348
-			'save_postmeta_box',
349
-		) );
350
-	}
351
-
352
-	// The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to
353
-	// save_post in order to save additional inputs from the edit page. In order to avoid issues, we pop all the hooks
354
-	// to the save_post and restore them after we saved the entity.
355
-	// see https://github.com/insideout10/wordlift-plugin/issues/203
356
-	// see https://github.com/insideout10/wordlift-plugin/issues/156
357
-	// see https://github.com/insideout10/wordlift-plugin/issues/148
358
-	global $wp_filter;
359
-	$save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks;
360
-	is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters();
361
-
362
-
363
-	$log->trace( 'Going to insert new post...' );
364
-
365
-	// create or update the post.
366
-	$post_id = wp_insert_post( $params, true );
367
-
368
-	// Setting the alternative labels for this entity.
369
-	Wordlift_Entity_Service::get_instance()
370
-	                       ->set_alternative_labels( $post_id, $synonyms );
371
-
372
-	// Restore all the existing filters.
373
-	is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters;
374
-
375
-	// If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156)
376
-	if ( isset( $wpseo_metabox ) ) {
377
-		add_action( 'wp_insert_post', array(
378
-			$wpseo_metabox,
379
-			'save_postdata',
380
-		) );
381
-	}
382
-
383
-	// If SEO Ultimate is installed, add back the hook we removed a few lines above.
384
-	if ( isset( $seo_ultimate ) ) {
385
-		add_action( 'save_post', array(
386
-			$seo_ultimate,
387
-			'save_postmeta_box',
388
-		), 10, 2 );
389
-	}
390
-
391
-	// TODO: handle errors.
392
-	if ( is_wp_error( $post_id ) ) {
393
-		$log->error( 'An error occurred: ' . $post_id->get_error_message() );
394
-
395
-		// inform an error occurred.
396
-		return null;
397
-	}
398
-
399
-	wl_set_entity_main_type( $post_id, $type_uri );
400
-
401
-	// Save the entity types.
402
-	wl_set_entity_rdf_types( $post_id, $entity_types );
403
-
404
-	// Get a dataset URI for the entity.
405
-	$wl_uri = Wordlift_Entity_Service::get_instance()->get_uri( $post_id );
406
-
407
-	// Add the uri to the sameAs data if it's not a local URI.
408
-	if ( $wl_uri !== $uri ) {
409
-		array_push( $same_as, $uri );
410
-	}
411
-
412
-	// Save the sameAs data for the entity.
413
-	wl_schema_set_value( $post_id, 'sameAs', $same_as );
414
-
415
-	// Save the other properties (latitude, langitude, startDate, endDate, etc.)
416
-	foreach ( $other_properties as $property_name => $property_value ) {
417
-		wl_schema_set_value( $post_id, $property_name, $property_value );
418
-	}
419
-
420
-	// Call hooks.
421
-	do_action( 'wl_save_entity', $post_id );
422
-
423
-	foreach ( $images as $image_remote_url ) {
424
-
425
-		// Check if image is already present in local DB
426
-		if ( strpos( $image_remote_url, site_url() ) !== false ) {
427
-			// Do nothing.
428
-			continue;
429
-		}
430
-
431
-		// Check if there is an existing attachment for this post ID and source URL.
432
-		$existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url );
433
-
434
-		// Skip if an existing image is found.
435
-		if ( null !== $existing_image ) {
436
-			continue;
437
-		}
438
-
439
-		// Save the image and get the local path.
440
-		$image = Wordlift_Remote_Image_Service::save_from_url( $image_remote_url );
441
-
442
-		if ( false === $image || is_wp_error( $image ) ) {
443
-			continue;
444
-		}
445
-
446
-		// Get the local URL.
447
-		$filename     = $image['path'];
448
-		$url          = $image['url'];
449
-		$content_type = $image['content_type'];
450
-
451
-		$attachment = array(
452
-			'guid'           => $url,
453
-			// post_title, post_content (the value for this key should be the empty string), post_status and post_mime_type
454
-			'post_title'     => $label,
455
-			// Set the title to the post title.
456
-			'post_content'   => '',
457
-			'post_status'    => 'inherit',
458
-			'post_mime_type' => $content_type,
459
-		);
298
+    $label            = preg_replace( '/\xEF\xBB\xBF/', '', sanitize_text_field( $entity_data['label'] ) );
299
+    $type_uri         = $entity_data['main_type'];
300
+    $entity_types     = isset( $entity_data['type'] ) ? $entity_data['type'] : array();
301
+    $description      = $entity_data['description'];
302
+    $images           = isset( $entity_data['image'] ) ? (array) $entity_data['image'] : array();
303
+    $same_as          = isset( $entity_data['sameas'] ) ? (array) $entity_data['sameas'] : array();
304
+    $related_post_id  = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null;
305
+    $other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array();
306
+
307
+    // Get the synonyms.
308
+    $synonyms = isset( $entity_data['synonym'] ) ? $entity_data['synonym'] : array();
309
+
310
+    // Check whether an entity already exists with the provided URI.
311
+    if ( null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ) ) {
312
+        $log->debug( "Post already exists for URI $uri." );
313
+
314
+        return $post;
315
+    }
316
+
317
+    // Prepare properties of the new entity.
318
+    $params = array(
319
+        'post_status'  => ( is_numeric( $related_post_id ) ? get_post_status( $related_post_id ) : 'draft' ),
320
+        'post_type'    => Wordlift_Entity_Service::TYPE_NAME,
321
+        'post_title'   => $label,
322
+        'post_content' => $description,
323
+        'post_excerpt' => '',
324
+        // Ensure we're using a valid slug. We're not overwriting an existing
325
+        // entity with a post_name already set, since this call is made only for
326
+        // new entities.
327
+        //
328
+        // See https://github.com/insideout10/wordlift-plugin/issues/282
329
+        'post_name'    => sanitize_title( $label ),
330
+    );
331
+
332
+    // If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over"
333
+    // the local SEO form values to the created entity (https://github.com/insideout10/wordlift-plugin/issues/156)
334
+    // Same thing applies to SEO Ultimate (https://github.com/insideout10/wordlift-plugin/issues/148)
335
+    // This does NOT affect saving an entity from the entity admin page since this function is called when an entity
336
+    // is created when saving a post.
337
+    global $wpseo_metabox, $seo_ultimate;
338
+    if ( isset( $wpseo_metabox ) ) {
339
+        remove_action( 'wp_insert_post', array(
340
+            $wpseo_metabox,
341
+            'save_postdata',
342
+        ) );
343
+    }
344
+
345
+    if ( isset( $seo_ultimate ) ) {
346
+        remove_action( 'save_post', array(
347
+            $seo_ultimate,
348
+            'save_postmeta_box',
349
+        ) );
350
+    }
351
+
352
+    // The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to
353
+    // save_post in order to save additional inputs from the edit page. In order to avoid issues, we pop all the hooks
354
+    // to the save_post and restore them after we saved the entity.
355
+    // see https://github.com/insideout10/wordlift-plugin/issues/203
356
+    // see https://github.com/insideout10/wordlift-plugin/issues/156
357
+    // see https://github.com/insideout10/wordlift-plugin/issues/148
358
+    global $wp_filter;
359
+    $save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks;
360
+    is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters();
361
+
362
+
363
+    $log->trace( 'Going to insert new post...' );
364
+
365
+    // create or update the post.
366
+    $post_id = wp_insert_post( $params, true );
367
+
368
+    // Setting the alternative labels for this entity.
369
+    Wordlift_Entity_Service::get_instance()
370
+                            ->set_alternative_labels( $post_id, $synonyms );
371
+
372
+    // Restore all the existing filters.
373
+    is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters;
374
+
375
+    // If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156)
376
+    if ( isset( $wpseo_metabox ) ) {
377
+        add_action( 'wp_insert_post', array(
378
+            $wpseo_metabox,
379
+            'save_postdata',
380
+        ) );
381
+    }
382
+
383
+    // If SEO Ultimate is installed, add back the hook we removed a few lines above.
384
+    if ( isset( $seo_ultimate ) ) {
385
+        add_action( 'save_post', array(
386
+            $seo_ultimate,
387
+            'save_postmeta_box',
388
+        ), 10, 2 );
389
+    }
390
+
391
+    // TODO: handle errors.
392
+    if ( is_wp_error( $post_id ) ) {
393
+        $log->error( 'An error occurred: ' . $post_id->get_error_message() );
394
+
395
+        // inform an error occurred.
396
+        return null;
397
+    }
398
+
399
+    wl_set_entity_main_type( $post_id, $type_uri );
400
+
401
+    // Save the entity types.
402
+    wl_set_entity_rdf_types( $post_id, $entity_types );
403
+
404
+    // Get a dataset URI for the entity.
405
+    $wl_uri = Wordlift_Entity_Service::get_instance()->get_uri( $post_id );
406
+
407
+    // Add the uri to the sameAs data if it's not a local URI.
408
+    if ( $wl_uri !== $uri ) {
409
+        array_push( $same_as, $uri );
410
+    }
411
+
412
+    // Save the sameAs data for the entity.
413
+    wl_schema_set_value( $post_id, 'sameAs', $same_as );
414
+
415
+    // Save the other properties (latitude, langitude, startDate, endDate, etc.)
416
+    foreach ( $other_properties as $property_name => $property_value ) {
417
+        wl_schema_set_value( $post_id, $property_name, $property_value );
418
+    }
419
+
420
+    // Call hooks.
421
+    do_action( 'wl_save_entity', $post_id );
422
+
423
+    foreach ( $images as $image_remote_url ) {
424
+
425
+        // Check if image is already present in local DB
426
+        if ( strpos( $image_remote_url, site_url() ) !== false ) {
427
+            // Do nothing.
428
+            continue;
429
+        }
430
+
431
+        // Check if there is an existing attachment for this post ID and source URL.
432
+        $existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url );
433
+
434
+        // Skip if an existing image is found.
435
+        if ( null !== $existing_image ) {
436
+            continue;
437
+        }
438
+
439
+        // Save the image and get the local path.
440
+        $image = Wordlift_Remote_Image_Service::save_from_url( $image_remote_url );
441
+
442
+        if ( false === $image || is_wp_error( $image ) ) {
443
+            continue;
444
+        }
445
+
446
+        // Get the local URL.
447
+        $filename     = $image['path'];
448
+        $url          = $image['url'];
449
+        $content_type = $image['content_type'];
450
+
451
+        $attachment = array(
452
+            'guid'           => $url,
453
+            // post_title, post_content (the value for this key should be the empty string), post_status and post_mime_type
454
+            'post_title'     => $label,
455
+            // Set the title to the post title.
456
+            'post_content'   => '',
457
+            'post_status'    => 'inherit',
458
+            'post_mime_type' => $content_type,
459
+        );
460 460
 
461
-		// Create the attachment in WordPress and generate the related metadata.
462
-		$attachment_id = wp_insert_attachment( $attachment, $filename, $post_id );
463
-
464
-		// Set the source URL for the image.
465
-		wl_set_source_url( $attachment_id, $image_remote_url );
466
-
467
-		$attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
468
-		wp_update_attachment_metadata( $attachment_id, $attachment_data );
469
-
470
-		// Set it as the featured image.
471
-		set_post_thumbnail( $post_id, $attachment_id );
472
-	}
473
-
474
-	// The entity is pushed to Redlink on save by the function hooked to save_post.
475
-	// save the entity in the triple store.
476
-	do_action( 'wl_legacy_linked_data__push', $post_id );
477
-
478
-	// finally return the entity post.
479
-	return get_post( $post_id );
461
+        // Create the attachment in WordPress and generate the related metadata.
462
+        $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id );
463
+
464
+        // Set the source URL for the image.
465
+        wl_set_source_url( $attachment_id, $image_remote_url );
466
+
467
+        $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
468
+        wp_update_attachment_metadata( $attachment_id, $attachment_data );
469
+
470
+        // Set it as the featured image.
471
+        set_post_thumbnail( $post_id, $attachment_id );
472
+    }
473
+
474
+    // The entity is pushed to Redlink on save by the function hooked to save_post.
475
+    // save the entity in the triple store.
476
+    do_action( 'wl_legacy_linked_data__push', $post_id );
477
+
478
+    // finally return the entity post.
479
+    return get_post( $post_id );
480 480
 }
481 481
 
482 482
 /**
@@ -490,41 +490,41 @@  discard block
 block discarded – undo
490 490
  */
491 491
 function wl_linked_data_content_get_embedded_entities( $content ) {
492 492
 
493
-	// Remove quote escapes.
494
-	$content = str_replace( '\\"', '"', $content );
493
+    // Remove quote escapes.
494
+    $content = str_replace( '\\"', '"', $content );
495 495
 
496
-	// Match all itemid attributes.
497
-	$pattern = '/<\w+[^>]*\sitemid="([^"]+)"[^>]*>/im';
496
+    // Match all itemid attributes.
497
+    $pattern = '/<\w+[^>]*\sitemid="([^"]+)"[^>]*>/im';
498 498
 
499
-	//	wl_write_log( "Getting entities embedded into content [ pattern :: $pattern ]" );
499
+    //	wl_write_log( "Getting entities embedded into content [ pattern :: $pattern ]" );
500 500
 
501
-	// Remove the pattern while it is found (match nested annotations).
502
-	$matches = array();
501
+    // Remove the pattern while it is found (match nested annotations).
502
+    $matches = array();
503 503
 
504
-	// In case of errors, return an empty array.
505
-	if ( false === preg_match_all( $pattern, $content, $matches ) ) {
506
-		wl_write_log( "Found no entities embedded in content" );
504
+    // In case of errors, return an empty array.
505
+    if ( false === preg_match_all( $pattern, $content, $matches ) ) {
506
+        wl_write_log( "Found no entities embedded in content" );
507 507
 
508
-		return array();
509
-	}
508
+        return array();
509
+    }
510 510
 
511 511
 //    wl_write_log("wl_update_related_entities [ content :: $content ][ data :: " . var_export($data, true). " ][ matches :: " . var_export($matches, true) . " ]");
512 512
 
513
-	$uris  = $matches[1];
514
-	// Collect the entities.
515
-	$entities = array();
516
-	foreach ( $uris as $uri ) {
517
-		$uri_d = html_entity_decode( $uri );
513
+    $uris  = $matches[1];
514
+    // Collect the entities.
515
+    $entities = array();
516
+    foreach ( $uris as $uri ) {
517
+        $uri_d = html_entity_decode( $uri );
518 518
 
519
-		$entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri_d );
519
+        $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri_d );
520 520
 
521
-		if ( null !== $entity ) {
522
-			array_push( $entities, $entity->ID );
523
-		}
524
-	}
521
+        if ( null !== $entity ) {
522
+            array_push( $entities, $entity->ID );
523
+        }
524
+    }
525 525
 
526
-	// $count = sizeof( $entities );
527
-	// wl_write_log( "Found $count entities embedded in content" );
526
+    // $count = sizeof( $entities );
527
+    // wl_write_log( "Found $count entities embedded in content" );
528 528
 
529
-	return $entities;
529
+    return $entities;
530 530
 }
531 531
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +132 added lines, -132 removed lines patch added patch discarded remove patch
@@ -18,15 +18,15 @@  discard block
 block discarded – undo
18 18
  * @since 3.0.0
19 19
  *
20 20
  */
21
-function wl_linked_data_save_post( $post_id ) {
21
+function wl_linked_data_save_post($post_id) {
22 22
 
23
-	$log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post' );
23
+	$log = Wordlift_Log_Service::get_logger('wl_linked_data_save_post');
24 24
 
25
-	$log->trace( "Saving post $post_id to Linked Data..." );
25
+	$log->trace("Saving post $post_id to Linked Data...");
26 26
 
27 27
 	// If it's not numeric exit from here.
28
-	if ( ! is_numeric( $post_id ) || is_numeric( wp_is_post_revision( $post_id ) ) ) {
29
-		$log->debug( "Skipping $post_id, because id is not numeric or is a post revision." );
28
+	if ( ! is_numeric($post_id) || is_numeric(wp_is_post_revision($post_id))) {
29
+		$log->debug("Skipping $post_id, because id is not numeric or is a post revision.");
30 30
 
31 31
 		return;
32 32
 	}
@@ -34,7 +34,7 @@  discard block
 block discarded – undo
34 34
 	// Get the post type and check whether it supports the editor.
35 35
 	//
36 36
 	// @see https://github.com/insideout10/wordlift-plugin/issues/659.
37
-	$post_type = get_post_type( $post_id );
37
+	$post_type = get_post_type($post_id);
38 38
 	/**
39 39
 	 * Use `wl_post_type_supports_editor` which calls a filter to allow 3rd parties to change the setting.
40 40
 	 *
@@ -42,11 +42,11 @@  discard block
 block discarded – undo
42 42
 	 *
43 43
 	 * @see https://github.com/insideout10/wordlift-plugin/issues/847.
44 44
 	 */
45
-	$is_editor_supported = wl_post_type_supports_editor( $post_type );
45
+	$is_editor_supported = wl_post_type_supports_editor($post_type);
46 46
 
47 47
 	// Bail out if it's not an entity.
48
-	if ( ! $is_editor_supported ) {
49
-		$log->debug( "Skipping $post_id, because $post_type doesn't support the editor (content)." );
48
+	if ( ! $is_editor_supported) {
49
+		$log->debug("Skipping $post_id, because $post_type doesn't support the editor (content).");
50 50
 
51 51
 		return;
52 52
 	}
@@ -60,23 +60,23 @@  discard block
 block discarded – undo
60 60
 	$supported_types = Wordlift_Entity_Service::valid_entity_post_types();
61 61
 
62 62
 	// Bail out if it's not a valid entity.
63
-	if ( ! in_array( $post_type, $supported_types ) ) {
64
-		$log->debug( "Skipping $post_id, because $post_type is not a valid entity." );
63
+	if ( ! in_array($post_type, $supported_types)) {
64
+		$log->debug("Skipping $post_id, because $post_type is not a valid entity.");
65 65
 
66 66
 		return;
67 67
 	}
68 68
 
69 69
 	// Unhook this function so it doesn't loop infinitely.
70
-	remove_action( 'save_post', 'wl_linked_data_save_post' );
70
+	remove_action('save_post', 'wl_linked_data_save_post');
71 71
 
72 72
 	// raise the *wl_linked_data_save_post* event.
73
-	do_action( 'wl_linked_data_save_post', $post_id );
73
+	do_action('wl_linked_data_save_post', $post_id);
74 74
 
75 75
 	// Re-hook this function.
76
-	add_action( 'save_post', 'wl_linked_data_save_post' );
76
+	add_action('save_post', 'wl_linked_data_save_post');
77 77
 }
78 78
 
79
-add_action( 'save_post', 'wl_linked_data_save_post' );
79
+add_action('save_post', 'wl_linked_data_save_post');
80 80
 
81 81
 /**
82 82
  * Save the post to the triple store. Also saves the entities locally and on the triple store.
@@ -86,23 +86,23 @@  discard block
 block discarded – undo
86 86
  * @since 3.0.0
87 87
  *
88 88
  */
89
-function wl_linked_data_save_post_and_related_entities( $post_id ) {
89
+function wl_linked_data_save_post_and_related_entities($post_id) {
90 90
 
91
-	$log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post_and_related_entities' );
91
+	$log = Wordlift_Log_Service::get_logger('wl_linked_data_save_post_and_related_entities');
92 92
 
93
-	$log->trace( "Saving $post_id to Linked Data along with related entities..." );
93
+	$log->trace("Saving $post_id to Linked Data along with related entities...");
94 94
 
95 95
 	// Ignore auto-saves
96
-	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
97
-		$log->trace( 'Doing autosave, skipping...' );
96
+	if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
97
+		$log->trace('Doing autosave, skipping...');
98 98
 
99 99
 		return;
100 100
 	}
101 101
 
102 102
 	// get the current post.
103
-	$post = get_post( $post_id );
103
+	$post = get_post($post_id);
104 104
 
105
-	remove_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' );
105
+	remove_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities');
106 106
 
107 107
 	// wl_write_log( "[ post id :: $post_id ][ autosave :: false ][ post type :: $post->post_type ]" );
108 108
 
@@ -113,48 +113,48 @@  discard block
 block discarded – undo
113 113
 	$entities_uri_mapping = array();
114 114
 
115 115
 	// Save the entities coming with POST data.
116
-	if ( isset( $_POST['wl_entities'] ) && isset( $_POST['wl_boxes'] ) ) {
116
+	if (isset($_POST['wl_entities']) && isset($_POST['wl_boxes'])) {
117 117
 
118
-		wl_write_log( "[ post id :: $post_id ][ POST(wl_entities) :: " );
119
-		wl_write_log( json_encode( $_POST['wl_entities'] ) );
120
-		wl_write_log( "]" );
121
-		wl_write_log( "[ post id :: $post_id ][ POST(wl_boxes) :: " );
122
-		wl_write_log( json_encode( $_POST['wl_boxes'], true ) );
123
-		wl_write_log( "]" );
118
+		wl_write_log("[ post id :: $post_id ][ POST(wl_entities) :: ");
119
+		wl_write_log(json_encode($_POST['wl_entities']));
120
+		wl_write_log("]");
121
+		wl_write_log("[ post id :: $post_id ][ POST(wl_boxes) :: ");
122
+		wl_write_log(json_encode($_POST['wl_boxes'], true));
123
+		wl_write_log("]");
124 124
 
125 125
 		$entities_via_post = $_POST['wl_entities'];
126 126
 
127
-		foreach ( $entities_via_post as $entity_uri => $entity ) {
127
+		foreach ($entities_via_post as $entity_uri => $entity) {
128 128
 
129 129
 			// Only if the current entity is created from scratch let's avoid to
130 130
 			// create more than one entity with same label & entity type.
131
-			$entity_type = ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) ?
131
+			$entity_type = (preg_match('/^local-entity-.+/', $entity_uri) > 0) ?
132 132
 				$entity['main_type'] : null;
133 133
 
134 134
 			// Look if current entity uri matches an internal existing entity, meaning:
135 135
 			// 1. when $entity_uri is an internal uri
136 136
 			// 2. when $entity_uri is an external uri used as sameAs of an internal entity
137
-			$ie = $entity_service->get_entity_post_by_uri( $entity_uri );
137
+			$ie = $entity_service->get_entity_post_by_uri($entity_uri);
138 138
 
139 139
 			// Dont save the entities which are not found, but also local.
140
-			if ( $ie === null &&  Wordlift_Entity_Uri_Service::get_instance()->is_internal( $entity_uri ) ) {
140
+			if ($ie === null && Wordlift_Entity_Uri_Service::get_instance()->is_internal($entity_uri)) {
141 141
 				continue;
142 142
 			}
143 143
 
144 144
 			// Detect the uri depending if is an existing or a new entity
145
-			$uri = ( null === $ie ) ?
145
+			$uri = (null === $ie) ?
146 146
 				Wordlift_Uri_Service::get_instance()->build_uri(
147 147
 					$entity['label'],
148 148
 					Wordlift_Entity_Service::TYPE_NAME,
149 149
 					$entity_type
150
-				) : wl_get_entity_uri( $ie->ID );
150
+				) : wl_get_entity_uri($ie->ID);
151 151
 
152
-			wl_write_log( "Map $entity_uri on $uri" );
153
-			$entities_uri_mapping[ $entity_uri ] = $uri;
152
+			wl_write_log("Map $entity_uri on $uri");
153
+			$entities_uri_mapping[$entity_uri] = $uri;
154 154
 
155 155
 			// Local entities have a tmp uri with 'local-entity-' prefix
156 156
 			// These uris need to be rewritten here and replaced in the content
157
-			if ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) {
157
+			if (preg_match('/^local-entity-.+/', $entity_uri) > 0) {
158 158
 				// Override the entity obj
159 159
 				$entity['uri'] = $uri;
160 160
 			}
@@ -163,8 +163,8 @@  discard block
 block discarded – undo
163 163
 			$entity['related_post_id'] = $post_id;
164 164
 
165 165
 			// Save the entity if is a new entity
166
-			if ( null === $ie ) {
167
-				wl_save_entity( $entity );
166
+			if (null === $ie) {
167
+				wl_save_entity($entity);
168 168
 			}
169 169
 
170 170
 		}
@@ -175,14 +175,14 @@  discard block
 block discarded – undo
175 175
 	$updated_post_content = $post->post_content;
176 176
 
177 177
 	// Update the post content if we found mappings of new entities.
178
-	if ( ! empty( $entities_uri_mapping ) ) {
178
+	if ( ! empty($entities_uri_mapping)) {
179 179
 		// Save each entity and store the post id.
180
-		foreach ( $entities_uri_mapping as $tmp_uri => $uri ) {
181
-			if ( 1 !== preg_match( '@^(https?://|local-entity-)@', $tmp_uri ) ) {
180
+		foreach ($entities_uri_mapping as $tmp_uri => $uri) {
181
+			if (1 !== preg_match('@^(https?://|local-entity-)@', $tmp_uri)) {
182 182
 				continue;
183 183
 			}
184 184
 
185
-			$updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content );
185
+			$updated_post_content = str_replace($tmp_uri, $uri, $updated_post_content);
186 186
 		}
187 187
 
188 188
 		// Update the post content.
@@ -191,20 +191,20 @@  discard block
 block discarded – undo
191 191
 		 * content, so add the slashes to prevent back slash getting
192 192
 		 * removed.
193 193
 		 */
194
-		wp_update_post( array(
194
+		wp_update_post(array(
195 195
 			'ID'           => $post->ID,
196
-			'post_content' => addslashes( $updated_post_content ),
197
-		) );
196
+			'post_content' => addslashes($updated_post_content),
197
+		));
198 198
 	}
199 199
 	// Reset previously saved instances.
200
-	wl_core_delete_relation_instances( $post_id );
200
+	wl_core_delete_relation_instances($post_id);
201 201
 
202 202
 
203 203
 	$relations = Object_Relation_Service::get_instance()
204
-	                                    ->get_relations_from_content( $updated_post_content, Object_Type_Enum::POST );
204
+	                                    ->get_relations_from_content($updated_post_content, Object_Type_Enum::POST);
205 205
 
206 206
 	// Save relation instances
207
-	foreach ( $relations as $relation ) {
207
+	foreach ($relations as $relation) {
208 208
 
209 209
 		wl_core_add_relation_instance(
210 210
 			// subject id.
@@ -221,9 +221,9 @@  discard block
 block discarded – undo
221 221
 
222 222
 	}
223 223
 
224
-	if ( isset( $_POST['wl_entities'] ) ) {
224
+	if (isset($_POST['wl_entities'])) {
225 225
 		// Save post metadata if available
226
-		$metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ?
226
+		$metadata_via_post = (isset($_POST['wl_metadata'])) ?
227 227
 			$_POST['wl_metadata'] : array();
228 228
 
229 229
 		$fields = array(
@@ -232,35 +232,35 @@  discard block
 block discarded – undo
232 232
 		);
233 233
 
234 234
 		// Unlink topic taxonomy terms
235
-		Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for( $post->ID );
235
+		Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for($post->ID);
236 236
 
237
-		foreach ( $fields as $field ) {
237
+		foreach ($fields as $field) {
238 238
 
239 239
 			// Delete current values
240
-			delete_post_meta( $post->ID, $field );
240
+			delete_post_meta($post->ID, $field);
241 241
 			// Retrieve the entity uri
242
-			$uri = ( isset( $metadata_via_post[ $field ] ) ) ?
243
-				stripslashes( $metadata_via_post[ $field ] ) : '';
242
+			$uri = (isset($metadata_via_post[$field])) ?
243
+				stripslashes($metadata_via_post[$field]) : '';
244 244
 
245
-			$entity = $entity_service->get_entity_post_by_uri( $uri );
245
+			$entity = $entity_service->get_entity_post_by_uri($uri);
246 246
 
247
-			if ( $entity ) {
248
-				add_post_meta( $post->ID, $field, $entity->ID, true );
247
+			if ($entity) {
248
+				add_post_meta($post->ID, $field, $entity->ID, true);
249 249
 				// Set also the topic taxonomy
250
-				if ( $field === Wordlift_Schema_Service::FIELD_TOPIC ) {
251
-					Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for( $post->ID, $entity );
250
+				if ($field === Wordlift_Schema_Service::FIELD_TOPIC) {
251
+					Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for($post->ID, $entity);
252 252
 				}
253 253
 			}
254 254
 		}
255 255
 	}
256 256
 
257 257
 	// Push the post to Redlink.
258
-	do_action( 'wl_legacy_linked_data__push', $post->ID );
258
+	do_action('wl_legacy_linked_data__push', $post->ID);
259 259
 
260
-	add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' );
260
+	add_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities');
261 261
 }
262 262
 
263
-add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' );
263
+add_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities');
264 264
 
265 265
 /**
266 266
  * Save the specified data as an entity in WordPress. This method only create new entities. When an existing entity is
@@ -278,14 +278,14 @@  discard block
 block discarded – undo
278 278
  *
279 279
  * @return null|WP_Post A post instance or null in case of failure.
280 280
  */
281
-function wl_save_entity( $entity_data ) {
281
+function wl_save_entity($entity_data) {
282 282
 
283 283
 	// Required for REST API calls
284
-	if ( ! function_exists( 'wp_crop_image' ) ) {
285
-		require_once( ABSPATH . 'wp-admin/includes/image.php' );
284
+	if ( ! function_exists('wp_crop_image')) {
285
+		require_once(ABSPATH.'wp-admin/includes/image.php');
286 286
 	}
287 287
 
288
-	$log = Wordlift_Log_Service::get_logger( 'wl_save_entity' );
288
+	$log = Wordlift_Log_Service::get_logger('wl_save_entity');
289 289
 
290 290
 	$uri = $entity_data['uri'];
291 291
 	/*
@@ -295,28 +295,28 @@  discard block
 block discarded – undo
295 295
 	 *
296 296
 	 * @see https://github.com/insideout10/wordlift-plugin/issues/841
297 297
 	 */
298
-	$label            = preg_replace( '/\xEF\xBB\xBF/', '', sanitize_text_field( $entity_data['label'] ) );
298
+	$label            = preg_replace('/\xEF\xBB\xBF/', '', sanitize_text_field($entity_data['label']));
299 299
 	$type_uri         = $entity_data['main_type'];
300
-	$entity_types     = isset( $entity_data['type'] ) ? $entity_data['type'] : array();
300
+	$entity_types     = isset($entity_data['type']) ? $entity_data['type'] : array();
301 301
 	$description      = $entity_data['description'];
302
-	$images           = isset( $entity_data['image'] ) ? (array) $entity_data['image'] : array();
303
-	$same_as          = isset( $entity_data['sameas'] ) ? (array) $entity_data['sameas'] : array();
304
-	$related_post_id  = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null;
305
-	$other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array();
302
+	$images           = isset($entity_data['image']) ? (array) $entity_data['image'] : array();
303
+	$same_as          = isset($entity_data['sameas']) ? (array) $entity_data['sameas'] : array();
304
+	$related_post_id  = isset($entity_data['related_post_id']) ? $entity_data['related_post_id'] : null;
305
+	$other_properties = isset($entity_data['properties']) ? $entity_data['properties'] : array();
306 306
 
307 307
 	// Get the synonyms.
308
-	$synonyms = isset( $entity_data['synonym'] ) ? $entity_data['synonym'] : array();
308
+	$synonyms = isset($entity_data['synonym']) ? $entity_data['synonym'] : array();
309 309
 
310 310
 	// Check whether an entity already exists with the provided URI.
311
-	if ( null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ) ) {
312
-		$log->debug( "Post already exists for URI $uri." );
311
+	if (null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri($uri)) {
312
+		$log->debug("Post already exists for URI $uri.");
313 313
 
314 314
 		return $post;
315 315
 	}
316 316
 
317 317
 	// Prepare properties of the new entity.
318 318
 	$params = array(
319
-		'post_status'  => ( is_numeric( $related_post_id ) ? get_post_status( $related_post_id ) : 'draft' ),
319
+		'post_status'  => (is_numeric($related_post_id) ? get_post_status($related_post_id) : 'draft'),
320 320
 		'post_type'    => Wordlift_Entity_Service::TYPE_NAME,
321 321
 		'post_title'   => $label,
322 322
 		'post_content' => $description,
@@ -326,7 +326,7 @@  discard block
 block discarded – undo
326 326
 		// new entities.
327 327
 		//
328 328
 		// See https://github.com/insideout10/wordlift-plugin/issues/282
329
-		'post_name'    => sanitize_title( $label ),
329
+		'post_name'    => sanitize_title($label),
330 330
 	);
331 331
 
332 332
 	// If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over"
@@ -335,18 +335,18 @@  discard block
 block discarded – undo
335 335
 	// This does NOT affect saving an entity from the entity admin page since this function is called when an entity
336 336
 	// is created when saving a post.
337 337
 	global $wpseo_metabox, $seo_ultimate;
338
-	if ( isset( $wpseo_metabox ) ) {
339
-		remove_action( 'wp_insert_post', array(
338
+	if (isset($wpseo_metabox)) {
339
+		remove_action('wp_insert_post', array(
340 340
 			$wpseo_metabox,
341 341
 			'save_postdata',
342
-		) );
342
+		));
343 343
 	}
344 344
 
345
-	if ( isset( $seo_ultimate ) ) {
346
-		remove_action( 'save_post', array(
345
+	if (isset($seo_ultimate)) {
346
+		remove_action('save_post', array(
347 347
 			$seo_ultimate,
348 348
 			'save_postmeta_box',
349
-		) );
349
+		));
350 350
 	}
351 351
 
352 352
 	// The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to
@@ -356,90 +356,90 @@  discard block
 block discarded – undo
356 356
 	// see https://github.com/insideout10/wordlift-plugin/issues/156
357 357
 	// see https://github.com/insideout10/wordlift-plugin/issues/148
358 358
 	global $wp_filter;
359
-	$save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks;
360
-	is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters();
359
+	$save_post_filters = is_array($wp_filter['save_post']) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks;
360
+	is_array($wp_filter['save_post']) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters();
361 361
 
362 362
 
363
-	$log->trace( 'Going to insert new post...' );
363
+	$log->trace('Going to insert new post...');
364 364
 
365 365
 	// create or update the post.
366
-	$post_id = wp_insert_post( $params, true );
366
+	$post_id = wp_insert_post($params, true);
367 367
 
368 368
 	// Setting the alternative labels for this entity.
369 369
 	Wordlift_Entity_Service::get_instance()
370
-	                       ->set_alternative_labels( $post_id, $synonyms );
370
+	                       ->set_alternative_labels($post_id, $synonyms);
371 371
 
372 372
 	// Restore all the existing filters.
373
-	is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters;
373
+	is_array($wp_filter['save_post']) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters;
374 374
 
375 375
 	// If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156)
376
-	if ( isset( $wpseo_metabox ) ) {
377
-		add_action( 'wp_insert_post', array(
376
+	if (isset($wpseo_metabox)) {
377
+		add_action('wp_insert_post', array(
378 378
 			$wpseo_metabox,
379 379
 			'save_postdata',
380
-		) );
380
+		));
381 381
 	}
382 382
 
383 383
 	// If SEO Ultimate is installed, add back the hook we removed a few lines above.
384
-	if ( isset( $seo_ultimate ) ) {
385
-		add_action( 'save_post', array(
384
+	if (isset($seo_ultimate)) {
385
+		add_action('save_post', array(
386 386
 			$seo_ultimate,
387 387
 			'save_postmeta_box',
388
-		), 10, 2 );
388
+		), 10, 2);
389 389
 	}
390 390
 
391 391
 	// TODO: handle errors.
392
-	if ( is_wp_error( $post_id ) ) {
393
-		$log->error( 'An error occurred: ' . $post_id->get_error_message() );
392
+	if (is_wp_error($post_id)) {
393
+		$log->error('An error occurred: '.$post_id->get_error_message());
394 394
 
395 395
 		// inform an error occurred.
396 396
 		return null;
397 397
 	}
398 398
 
399
-	wl_set_entity_main_type( $post_id, $type_uri );
399
+	wl_set_entity_main_type($post_id, $type_uri);
400 400
 
401 401
 	// Save the entity types.
402
-	wl_set_entity_rdf_types( $post_id, $entity_types );
402
+	wl_set_entity_rdf_types($post_id, $entity_types);
403 403
 
404 404
 	// Get a dataset URI for the entity.
405
-	$wl_uri = Wordlift_Entity_Service::get_instance()->get_uri( $post_id );
405
+	$wl_uri = Wordlift_Entity_Service::get_instance()->get_uri($post_id);
406 406
 
407 407
 	// Add the uri to the sameAs data if it's not a local URI.
408
-	if ( $wl_uri !== $uri ) {
409
-		array_push( $same_as, $uri );
408
+	if ($wl_uri !== $uri) {
409
+		array_push($same_as, $uri);
410 410
 	}
411 411
 
412 412
 	// Save the sameAs data for the entity.
413
-	wl_schema_set_value( $post_id, 'sameAs', $same_as );
413
+	wl_schema_set_value($post_id, 'sameAs', $same_as);
414 414
 
415 415
 	// Save the other properties (latitude, langitude, startDate, endDate, etc.)
416
-	foreach ( $other_properties as $property_name => $property_value ) {
417
-		wl_schema_set_value( $post_id, $property_name, $property_value );
416
+	foreach ($other_properties as $property_name => $property_value) {
417
+		wl_schema_set_value($post_id, $property_name, $property_value);
418 418
 	}
419 419
 
420 420
 	// Call hooks.
421
-	do_action( 'wl_save_entity', $post_id );
421
+	do_action('wl_save_entity', $post_id);
422 422
 
423
-	foreach ( $images as $image_remote_url ) {
423
+	foreach ($images as $image_remote_url) {
424 424
 
425 425
 		// Check if image is already present in local DB
426
-		if ( strpos( $image_remote_url, site_url() ) !== false ) {
426
+		if (strpos($image_remote_url, site_url()) !== false) {
427 427
 			// Do nothing.
428 428
 			continue;
429 429
 		}
430 430
 
431 431
 		// Check if there is an existing attachment for this post ID and source URL.
432
-		$existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url );
432
+		$existing_image = wl_get_attachment_for_source_url($post_id, $image_remote_url);
433 433
 
434 434
 		// Skip if an existing image is found.
435
-		if ( null !== $existing_image ) {
435
+		if (null !== $existing_image) {
436 436
 			continue;
437 437
 		}
438 438
 
439 439
 		// Save the image and get the local path.
440
-		$image = Wordlift_Remote_Image_Service::save_from_url( $image_remote_url );
440
+		$image = Wordlift_Remote_Image_Service::save_from_url($image_remote_url);
441 441
 
442
-		if ( false === $image || is_wp_error( $image ) ) {
442
+		if (false === $image || is_wp_error($image)) {
443 443
 			continue;
444 444
 		}
445 445
 
@@ -459,24 +459,24 @@  discard block
 block discarded – undo
459 459
 		);
460 460
 
461 461
 		// Create the attachment in WordPress and generate the related metadata.
462
-		$attachment_id = wp_insert_attachment( $attachment, $filename, $post_id );
462
+		$attachment_id = wp_insert_attachment($attachment, $filename, $post_id);
463 463
 
464 464
 		// Set the source URL for the image.
465
-		wl_set_source_url( $attachment_id, $image_remote_url );
465
+		wl_set_source_url($attachment_id, $image_remote_url);
466 466
 
467
-		$attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
468
-		wp_update_attachment_metadata( $attachment_id, $attachment_data );
467
+		$attachment_data = wp_generate_attachment_metadata($attachment_id, $filename);
468
+		wp_update_attachment_metadata($attachment_id, $attachment_data);
469 469
 
470 470
 		// Set it as the featured image.
471
-		set_post_thumbnail( $post_id, $attachment_id );
471
+		set_post_thumbnail($post_id, $attachment_id);
472 472
 	}
473 473
 
474 474
 	// The entity is pushed to Redlink on save by the function hooked to save_post.
475 475
 	// save the entity in the triple store.
476
-	do_action( 'wl_legacy_linked_data__push', $post_id );
476
+	do_action('wl_legacy_linked_data__push', $post_id);
477 477
 
478 478
 	// finally return the entity post.
479
-	return get_post( $post_id );
479
+	return get_post($post_id);
480 480
 }
481 481
 
482 482
 /**
@@ -488,10 +488,10 @@  discard block
 block discarded – undo
488 488
  * @since 3.0.0
489 489
  *
490 490
  */
491
-function wl_linked_data_content_get_embedded_entities( $content ) {
491
+function wl_linked_data_content_get_embedded_entities($content) {
492 492
 
493 493
 	// Remove quote escapes.
494
-	$content = str_replace( '\\"', '"', $content );
494
+	$content = str_replace('\\"', '"', $content);
495 495
 
496 496
 	// Match all itemid attributes.
497 497
 	$pattern = '/<\w+[^>]*\sitemid="([^"]+)"[^>]*>/im';
@@ -502,24 +502,24 @@  discard block
 block discarded – undo
502 502
 	$matches = array();
503 503
 
504 504
 	// In case of errors, return an empty array.
505
-	if ( false === preg_match_all( $pattern, $content, $matches ) ) {
506
-		wl_write_log( "Found no entities embedded in content" );
505
+	if (false === preg_match_all($pattern, $content, $matches)) {
506
+		wl_write_log("Found no entities embedded in content");
507 507
 
508 508
 		return array();
509 509
 	}
510 510
 
511 511
 //    wl_write_log("wl_update_related_entities [ content :: $content ][ data :: " . var_export($data, true). " ][ matches :: " . var_export($matches, true) . " ]");
512 512
 
513
-	$uris  = $matches[1];
513
+	$uris = $matches[1];
514 514
 	// Collect the entities.
515 515
 	$entities = array();
516
-	foreach ( $uris as $uri ) {
517
-		$uri_d = html_entity_decode( $uri );
516
+	foreach ($uris as $uri) {
517
+		$uri_d = html_entity_decode($uri);
518 518
 
519
-		$entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri_d );
519
+		$entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri($uri_d);
520 520
 
521
-		if ( null !== $entity ) {
522
-			array_push( $entities, $entity->ID );
521
+		if (null !== $entity) {
522
+			array_push($entities, $entity->ID);
523 523
 		}
524 524
 	}
525 525
 
Please login to merge, or discard this patch.