Completed
Pull Request — develop (#1679)
by Naveen
01:12
created
src/wordlift/relation/class-post-terms-relation-service.php 2 patches
Indentation   +55 added lines, -55 removed lines patch added patch discarded remove patch
@@ -16,69 +16,69 @@
 block discarded – undo
16 16
 
17 17
 class Post_Terms_Relation_Service extends Abstract_Relation_Service {
18 18
 
19
-	private static $instance = null;
19
+    private static $instance = null;
20 20
 
21
-	/**
22
-	 * The singleton instance.
23
-	 *
24
-	 * @return Relation_Service_Interface
25
-	 */
26
-	public static function get_instance() {
27
-		if ( ! isset( self::$instance ) ) {
28
-			self::$instance = new self();
29
-		}
21
+    /**
22
+     * The singleton instance.
23
+     *
24
+     * @return Relation_Service_Interface
25
+     */
26
+    public static function get_instance() {
27
+        if ( ! isset( self::$instance ) ) {
28
+            self::$instance = new self();
29
+        }
30 30
 
31
-		return self::$instance;
32
-	}
31
+        return self::$instance;
32
+    }
33 33
 
34
-	// **
35
-	// * @param $term_id int Term id.
36
-	// */
37
-	// public function get_relation_type( $term_id ) {
38
-	// $schema = $this->term_entity_type_service->get_schema(
39
-	// $term_id
40
-	// );
34
+    // **
35
+    // * @param $term_id int Term id.
36
+    // */
37
+    // public function get_relation_type( $term_id ) {
38
+    // $schema = $this->term_entity_type_service->get_schema(
39
+    // $term_id
40
+    // );
41 41
 //		// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize
42
-	// $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES );
43
-	//
44
-	// $entity_type = str_replace( 'wl-', '', $schema['css_class'] );
45
-	// foreach ( $classification_boxes as $cb ) {
46
-	// if ( in_array( $entity_type, $cb['registeredTypes'], true ) ) {
47
-	// return $cb['id'];
48
-	// }
49
-	// }
50
-	//
51
-	// return WL_WHAT_RELATION;
52
-	// }
42
+    // $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES );
43
+    //
44
+    // $entity_type = str_replace( 'wl-', '', $schema['css_class'] );
45
+    // foreach ( $classification_boxes as $cb ) {
46
+    // if ( in_array( $entity_type, $cb['registeredTypes'], true ) ) {
47
+    // return $cb['id'];
48
+    // }
49
+    // }
50
+    //
51
+    // return WL_WHAT_RELATION;
52
+    // }
53 53
 
54
-	/**
55
-	 * @param $content_id
56
-	 * @param $relations
57
-	 *
58
-	 * @return void
59
-	 */
60
-	public function add_relations( $content_id, $relations ) {
61
-		if ( $content_id->get_type() !== Object_Type_Enum::POST ) {
62
-			return;
63
-		}
54
+    /**
55
+     * @param $content_id
56
+     * @param $relations
57
+     *
58
+     * @return void
59
+     */
60
+    public function add_relations( $content_id, $relations ) {
61
+        if ( $content_id->get_type() !== Object_Type_Enum::POST ) {
62
+            return;
63
+        }
64 64
 
65
-		$post_id = $content_id->get_id();
66
-		// @@todo, add a filter here?
67
-		$taxonomies = array( 'category', 'post_tag' );
68
-		$term_ids   = wp_get_object_terms( $post_id, $taxonomies, array( 'fields' => 'ids' ) );
65
+        $post_id = $content_id->get_id();
66
+        // @@todo, add a filter here?
67
+        $taxonomies = array( 'category', 'post_tag' );
68
+        $term_ids   = wp_get_object_terms( $post_id, $taxonomies, array( 'fields' => 'ids' ) );
69 69
 
70
-		$new_relations = array_map(
71
-			function ( $term_id ) use ( $content_id ) {
72
-				$object_id = Wordpress_Content_Id::create_term( $term_id );
70
+        $new_relations = array_map(
71
+            function ( $term_id ) use ( $content_id ) {
72
+                $object_id = Wordpress_Content_Id::create_term( $term_id );
73 73
 
74
-				// @@todo Entity Service works only for posts, we default to `WHAT` as predicate
75
-				// for terms.
76
-				return new Relation( $content_id, $object_id, WL_WHAT_RELATION );
77
-			},
78
-			$term_ids
79
-		);
74
+                // @@todo Entity Service works only for posts, we default to `WHAT` as predicate
75
+                // for terms.
76
+                return new Relation( $content_id, $object_id, WL_WHAT_RELATION );
77
+            },
78
+            $term_ids
79
+        );
80 80
 
81
-		$relations->add( ...$new_relations );
82
-	}
81
+        $relations->add( ...$new_relations );
82
+    }
83 83
 
84 84
 }
Please login to merge, or discard this patch.
Spacing   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -24,7 +24,7 @@  discard block
 block discarded – undo
24 24
 	 * @return Relation_Service_Interface
25 25
 	 */
26 26
 	public static function get_instance() {
27
-		if ( ! isset( self::$instance ) ) {
27
+		if ( ! isset(self::$instance)) {
28 28
 			self::$instance = new self();
29 29
 		}
30 30
 
@@ -57,28 +57,28 @@  discard block
 block discarded – undo
57 57
 	 *
58 58
 	 * @return void
59 59
 	 */
60
-	public function add_relations( $content_id, $relations ) {
61
-		if ( $content_id->get_type() !== Object_Type_Enum::POST ) {
60
+	public function add_relations($content_id, $relations) {
61
+		if ($content_id->get_type() !== Object_Type_Enum::POST) {
62 62
 			return;
63 63
 		}
64 64
 
65 65
 		$post_id = $content_id->get_id();
66 66
 		// @@todo, add a filter here?
67
-		$taxonomies = array( 'category', 'post_tag' );
68
-		$term_ids   = wp_get_object_terms( $post_id, $taxonomies, array( 'fields' => 'ids' ) );
67
+		$taxonomies = array('category', 'post_tag');
68
+		$term_ids   = wp_get_object_terms($post_id, $taxonomies, array('fields' => 'ids'));
69 69
 
70 70
 		$new_relations = array_map(
71
-			function ( $term_id ) use ( $content_id ) {
72
-				$object_id = Wordpress_Content_Id::create_term( $term_id );
71
+			function($term_id) use ($content_id) {
72
+				$object_id = Wordpress_Content_Id::create_term($term_id);
73 73
 
74 74
 				// @@todo Entity Service works only for posts, we default to `WHAT` as predicate
75 75
 				// for terms.
76
-				return new Relation( $content_id, $object_id, WL_WHAT_RELATION );
76
+				return new Relation($content_id, $object_id, WL_WHAT_RELATION);
77 77
 			},
78 78
 			$term_ids
79 79
 		);
80 80
 
81
-		$relations->add( ...$new_relations );
81
+		$relations->add(...$new_relations);
82 82
 	}
83 83
 
84 84
 }
Please login to merge, or discard this patch.
src/wordlift/relation/class-relation-service-interface.php 2 patches
Indentation   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -16,20 +16,20 @@
 block discarded – undo
16 16
 
17 17
 interface  Relation_Service_Interface {
18 18
 
19
-	/**
20
-	 * Get the relations for the provided {@link Wordpress_Content_Id}
21
-	 *
22
-	 * @param Wordpress_Content_Id $content_id
23
-	 *
24
-	 * @return Relations_Interface
25
-	 */
26
-	public function get_relations( $content_id );
19
+    /**
20
+     * Get the relations for the provided {@link Wordpress_Content_Id}
21
+     *
22
+     * @param Wordpress_Content_Id $content_id
23
+     *
24
+     * @return Relations_Interface
25
+     */
26
+    public function get_relations( $content_id );
27 27
 
28
-	/**
29
-	 * Add the relations for the provided {@link Wordpress_Content_Id} to the provided {@link Relations_Interface}
30
-	 *
31
-	 * @param Wordpress_Content_Id $content_id
32
-	 */
33
-	public function add_relations( $content_id, $relations );
28
+    /**
29
+     * Add the relations for the provided {@link Wordpress_Content_Id} to the provided {@link Relations_Interface}
30
+     *
31
+     * @param Wordpress_Content_Id $content_id
32
+     */
33
+    public function add_relations( $content_id, $relations );
34 34
 
35 35
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -23,13 +23,13 @@
 block discarded – undo
23 23
 	 *
24 24
 	 * @return Relations_Interface
25 25
 	 */
26
-	public function get_relations( $content_id );
26
+	public function get_relations($content_id);
27 27
 
28 28
 	/**
29 29
 	 * Add the relations for the provided {@link Wordpress_Content_Id} to the provided {@link Relations_Interface}
30 30
 	 *
31 31
 	 * @param Wordpress_Content_Id $content_id
32 32
 	 */
33
-	public function add_relations( $content_id, $relations );
33
+	public function add_relations($content_id, $relations);
34 34
 
35 35
 }
Please login to merge, or discard this patch.
src/wordlift/relation/class-relation-instances-relation-service.php 2 patches
Indentation   +37 added lines, -37 removed lines patch added patch discarded remove patch
@@ -15,46 +15,46 @@
 block discarded – undo
15 15
 
16 16
 class Relation_Instances_Relation_Service extends Abstract_Relation_Service {
17 17
 
18
-	private static $instance = null;
19
-
20
-	/**
21
-	 * The singleton instance.
22
-	 *
23
-	 * @return Relation_Service_Interface
24
-	 */
25
-	public static function get_instance() {
26
-		if ( ! isset( self::$instance ) ) {
27
-			self::$instance = new self();
28
-		}
29
-
30
-		return self::$instance;
31
-	}
32
-
33
-	/**
34
-	 * @param Wordpress_Content_Id $content_id
35
-	 * @param Relations_Interface  $relations
36
-	 */
37
-	public function add_relations( $content_id, $relations ) {
38
-		global $wpdb;
39
-
40
-		$results = $wpdb->get_results(
41
-			$wpdb->prepare(
42
-				"
18
+    private static $instance = null;
19
+
20
+    /**
21
+     * The singleton instance.
22
+     *
23
+     * @return Relation_Service_Interface
24
+     */
25
+    public static function get_instance() {
26
+        if ( ! isset( self::$instance ) ) {
27
+            self::$instance = new self();
28
+        }
29
+
30
+        return self::$instance;
31
+    }
32
+
33
+    /**
34
+     * @param Wordpress_Content_Id $content_id
35
+     * @param Relations_Interface  $relations
36
+     */
37
+    public function add_relations( $content_id, $relations ) {
38
+        global $wpdb;
39
+
40
+        $results = $wpdb->get_results(
41
+            $wpdb->prepare(
42
+                "
43 43
 			SELECT subject_id, subject_type, predicate, object_id, object_type
44 44
 				FROM {$wpdb->prefix}wl_relation_instances
45 45
 				WHERE subject_id = %d AND subject_type = %s
46 46
 			",
47
-				$content_id->get_id(),
48
-				$content_id->get_type()
49
-			)
50
-		);
51
-
52
-		$new_relations = array_map(
53
-			'Wordlift\Relation\Relation::from_relation_instances',
54
-			$results
55
-		);
56
-
57
-		$relations->add( ...$new_relations );
58
-	}
47
+                $content_id->get_id(),
48
+                $content_id->get_type()
49
+            )
50
+        );
51
+
52
+        $new_relations = array_map(
53
+            'Wordlift\Relation\Relation::from_relation_instances',
54
+            $results
55
+        );
56
+
57
+        $relations->add( ...$new_relations );
58
+    }
59 59
 
60 60
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -23,7 +23,7 @@  discard block
 block discarded – undo
23 23
 	 * @return Relation_Service_Interface
24 24
 	 */
25 25
 	public static function get_instance() {
26
-		if ( ! isset( self::$instance ) ) {
26
+		if ( ! isset(self::$instance)) {
27 27
 			self::$instance = new self();
28 28
 		}
29 29
 
@@ -34,7 +34,7 @@  discard block
 block discarded – undo
34 34
 	 * @param Wordpress_Content_Id $content_id
35 35
 	 * @param Relations_Interface  $relations
36 36
 	 */
37
-	public function add_relations( $content_id, $relations ) {
37
+	public function add_relations($content_id, $relations) {
38 38
 		global $wpdb;
39 39
 
40 40
 		$results = $wpdb->get_results(
@@ -54,7 +54,7 @@  discard block
 block discarded – undo
54 54
 			$results
55 55
 		);
56 56
 
57
-		$relations->add( ...$new_relations );
57
+		$relations->add(...$new_relations);
58 58
 	}
59 59
 
60 60
 }
Please login to merge, or discard this patch.
src/wordlift/relation/class-abstract-relation-service.php 2 patches
Indentation   +15 added lines, -15 removed lines patch added patch discarded remove patch
@@ -6,24 +6,24 @@
 block discarded – undo
6 6
 
7 7
 abstract class Abstract_Relation_Service implements Relation_Service_Interface {
8 8
 
9
-	protected function __construct() {
9
+    protected function __construct() {
10 10
 
11
-	}
11
+    }
12 12
 
13
-	/**
14
-	 * A default implementation for the `get_relations` method that creates `Relations` instance
15
-	 * and populates it with `Relation`s.
16
-	 *
17
-	 * @param Wordpress_Content_Id $content_id The WordPress content id.
18
-	 *
19
-	 * @return Relations_Interface A `Relations` instance.
20
-	 */
21
-	public function get_relations( $content_id ) {
22
-		$relations = new Relations();
13
+    /**
14
+     * A default implementation for the `get_relations` method that creates `Relations` instance
15
+     * and populates it with `Relation`s.
16
+     *
17
+     * @param Wordpress_Content_Id $content_id The WordPress content id.
18
+     *
19
+     * @return Relations_Interface A `Relations` instance.
20
+     */
21
+    public function get_relations( $content_id ) {
22
+        $relations = new Relations();
23 23
 
24
-		$this->add_relations( $content_id, $relations );
24
+        $this->add_relations( $content_id, $relations );
25 25
 
26
-		return $relations;
27
-	}
26
+        return $relations;
27
+    }
28 28
 
29 29
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -18,10 +18,10 @@
 block discarded – undo
18 18
 	 *
19 19
 	 * @return Relations_Interface A `Relations` instance.
20 20
 	 */
21
-	public function get_relations( $content_id ) {
21
+	public function get_relations($content_id) {
22 22
 		$relations = new Relations();
23 23
 
24
-		$this->add_relations( $content_id, $relations );
24
+		$this->add_relations($content_id, $relations);
25 25
 
26 26
 		return $relations;
27 27
 	}
Please login to merge, or discard this patch.
src/wordlift/relation/class-post-content-relation-service.php 2 patches
Indentation   +40 added lines, -40 removed lines patch added patch discarded remove patch
@@ -18,55 +18,55 @@
 block discarded – undo
18 18
 
19 19
 class Post_Content_Relation_Service extends Abstract_Relation_Service {
20 20
 
21
-	private static $instance = null;
21
+    private static $instance = null;
22 22
 
23
-	/**
24
-	 * The singleton instance.
25
-	 *
26
-	 * @return Relation_Service_Interface
27
-	 */
28
-	public static function get_instance() {
29
-		if ( ! isset( self::$instance ) ) {
30
-			self::$instance = new self();
31
-		}
23
+    /**
24
+     * The singleton instance.
25
+     *
26
+     * @return Relation_Service_Interface
27
+     */
28
+    public static function get_instance() {
29
+        if ( ! isset( self::$instance ) ) {
30
+            self::$instance = new self();
31
+        }
32 32
 
33
-		return self::$instance;
34
-	}
33
+        return self::$instance;
34
+    }
35 35
 
36
-	/**
37
-	 * @param Wordpress_Content_Id $content_id
38
-	 * @param Relations_Interface  $relations
39
-	 */
40
-	public function add_relations( $content_id, $relations ) {
41
-		if ( $content_id->get_type() !== Object_Type_Enum::POST ) {
42
-			return;
43
-		}
36
+    /**
37
+     * @param Wordpress_Content_Id $content_id
38
+     * @param Relations_Interface  $relations
39
+     */
40
+    public function add_relations( $content_id, $relations ) {
41
+        if ( $content_id->get_type() !== Object_Type_Enum::POST ) {
42
+            return;
43
+        }
44 44
 
45
-		$post = get_post( $content_id->get_id() );
46
-		if ( ! is_a( $post, 'WP_Post' ) ) {
47
-			return;
48
-		}
45
+        $post = get_post( $content_id->get_id() );
46
+        if ( ! is_a( $post, 'WP_Post' ) ) {
47
+            return;
48
+        }
49 49
 
50
-		$content_filter_service = \Wordlift_Content_Filter_Service::get_instance();
50
+        $content_filter_service = \Wordlift_Content_Filter_Service::get_instance();
51 51
 
52
-		$post_content    = wp_unslash( $post->post_content );
53
-		$entity_uris     = $content_filter_service->get_entity_uris( $post_content );
54
-		$content_service = Wordpress_Content_Service::get_instance();
55
-		$entity_service  = Wordlift_Entity_Service::get_instance();
52
+        $post_content    = wp_unslash( $post->post_content );
53
+        $entity_uris     = $content_filter_service->get_entity_uris( $post_content );
54
+        $content_service = Wordpress_Content_Service::get_instance();
55
+        $entity_service  = Wordlift_Entity_Service::get_instance();
56 56
 
57
-		foreach ( $entity_uris as $entity_uri ) {
58
-			$content = $content_service->get_by_entity_id( $entity_uri );
59
-			if ( ! isset( $content ) ) {
60
-				continue;
61
-			}
57
+        foreach ( $entity_uris as $entity_uri ) {
58
+            $content = $content_service->get_by_entity_id( $entity_uri );
59
+            if ( ! isset( $content ) ) {
60
+                continue;
61
+            }
62 62
 
63
-			$object_id = new Wordpress_Content_Id( $content->get_id(), $content->get_object_type_enum() );
63
+            $object_id = new Wordpress_Content_Id( $content->get_id(), $content->get_object_type_enum() );
64 64
 
65
-			$predicate = $entity_service->get_classification_scope_for( $content->get_id() );
66
-			$relation  = new Relation( $content_id, $object_id, $predicate );
67
-			$relations->add( $relation );
68
-		}
65
+            $predicate = $entity_service->get_classification_scope_for( $content->get_id() );
66
+            $relation  = new Relation( $content_id, $object_id, $predicate );
67
+            $relations->add( $relation );
68
+        }
69 69
 
70
-	}
70
+    }
71 71
 
72 72
 }
Please login to merge, or discard this patch.
Spacing   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -26,7 +26,7 @@  discard block
 block discarded – undo
26 26
 	 * @return Relation_Service_Interface
27 27
 	 */
28 28
 	public static function get_instance() {
29
-		if ( ! isset( self::$instance ) ) {
29
+		if ( ! isset(self::$instance)) {
30 30
 			self::$instance = new self();
31 31
 		}
32 32
 
@@ -37,34 +37,34 @@  discard block
 block discarded – undo
37 37
 	 * @param Wordpress_Content_Id $content_id
38 38
 	 * @param Relations_Interface  $relations
39 39
 	 */
40
-	public function add_relations( $content_id, $relations ) {
41
-		if ( $content_id->get_type() !== Object_Type_Enum::POST ) {
40
+	public function add_relations($content_id, $relations) {
41
+		if ($content_id->get_type() !== Object_Type_Enum::POST) {
42 42
 			return;
43 43
 		}
44 44
 
45
-		$post = get_post( $content_id->get_id() );
46
-		if ( ! is_a( $post, 'WP_Post' ) ) {
45
+		$post = get_post($content_id->get_id());
46
+		if ( ! is_a($post, 'WP_Post')) {
47 47
 			return;
48 48
 		}
49 49
 
50 50
 		$content_filter_service = \Wordlift_Content_Filter_Service::get_instance();
51 51
 
52
-		$post_content    = wp_unslash( $post->post_content );
53
-		$entity_uris     = $content_filter_service->get_entity_uris( $post_content );
52
+		$post_content    = wp_unslash($post->post_content);
53
+		$entity_uris     = $content_filter_service->get_entity_uris($post_content);
54 54
 		$content_service = Wordpress_Content_Service::get_instance();
55 55
 		$entity_service  = Wordlift_Entity_Service::get_instance();
56 56
 
57
-		foreach ( $entity_uris as $entity_uri ) {
58
-			$content = $content_service->get_by_entity_id( $entity_uri );
59
-			if ( ! isset( $content ) ) {
57
+		foreach ($entity_uris as $entity_uri) {
58
+			$content = $content_service->get_by_entity_id($entity_uri);
59
+			if ( ! isset($content)) {
60 60
 				continue;
61 61
 			}
62 62
 
63
-			$object_id = new Wordpress_Content_Id( $content->get_id(), $content->get_object_type_enum() );
63
+			$object_id = new Wordpress_Content_Id($content->get_id(), $content->get_object_type_enum());
64 64
 
65
-			$predicate = $entity_service->get_classification_scope_for( $content->get_id() );
66
-			$relation  = new Relation( $content_id, $object_id, $predicate );
67
-			$relations->add( $relation );
65
+			$predicate = $entity_service->get_classification_scope_for($content->get_id());
66
+			$relation  = new Relation($content_id, $object_id, $predicate);
67
+			$relations->add($relation);
68 68
 		}
69 69
 
70 70
 	}
Please login to merge, or discard this patch.
src/wordlift/relation/class-relation-service.php 2 patches
Indentation   +75 added lines, -75 removed lines patch added patch discarded remove patch
@@ -9,80 +9,80 @@
 block discarded – undo
9 9
 
10 10
 class Relation_Service extends Abstract_Relation_Service {
11 11
 
12
-	/**
13
-	 * @var Relation_Service_Interface[]
14
-	 */
15
-	private $delegates = array();
16
-
17
-	protected function __construct() {
18
-
19
-	}
20
-
21
-	private static $instance = null;
22
-
23
-	/**
24
-	 * The singleton instance.
25
-	 *
26
-	 * @return Relation_Service_Interface
27
-	 */
28
-	public static function get_instance() {
29
-		if ( ! isset( self::$instance ) ) {
30
-			self::$instance = new self();
31
-
32
-			self::$instance->register_delegate( Relation_Instances_Relation_Service::get_instance() );
33
-			self::$instance->register_delegate( Post_Terms_Relation_Service::get_instance() );
34
-			self::$instance->register_delegate( Post_Content_Relation_Service::get_instance() );
35
-		}
36
-
37
-		return self::$instance;
38
-	}
39
-
40
-	public function register_delegate( $delegate ) {
41
-		Assertions::is_a( $delegate, 'Wordlift\Relation\Relation_Service_Interface', 'A `delegate` must implement the `Wordlift\Relation\Relation_Service_Interface` interface.' );
42
-
43
-		$this->delegates[] = $delegate;
44
-	}
45
-
46
-	public function add_relations( $content_id, $relations ) {
47
-		Assertions::is_set( $relations, '`$relations` should be set to a `Relations` instance.' );
48
-
49
-		foreach ( $this->delegates as $delegate ) {
50
-			$delegate->add_relations( $content_id, $relations );
51
-		}
52
-	}
53
-
54
-	/**
55
-	 * This helper method is used to create relations from entity_uris.
56
-	 * Its only purpose as of now is to process the entity_uris emitted
57
-	 * from disambiguation widget.
58
-	 *
59
-	 * @param $subject Wordpress_Content_Id
60
-	 * @param $uris
61
-	 *
62
-	 * @return array<Relation>
63
-	 */
64
-	public static function get_relations_from_uris( $subject, $uris ) {
65
-
66
-		$entity_service = \Wordlift_Entity_Service::get_instance();
67
-
68
-		return array_filter(
69
-			array_map(
70
-				function ( $uri ) use ( $entity_service, $subject ) {
71
-					/**
72
-					 * @var $content Content|null
73
-					 */
74
-					$content = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as( $uri );
75
-					$bag     = $content->get_bag();
76
-					if ( $bag instanceof \WP_Post || $bag instanceof \WP_Term ) {
77
-						$predicate = $bag instanceof \WP_Term ? WL_WHAT_RELATION : $entity_service->get_classification_scope_for( $content->get_id() );
78
-						return new Relation( $subject, new Wordpress_Content_Id( $content->get_id(), $content->get_object_type_enum() ), $predicate );
79
-					}
80
-					return false;
81
-				},
82
-				$uris
83
-			)
84
-		);
85
-
86
-	}
12
+    /**
13
+     * @var Relation_Service_Interface[]
14
+     */
15
+    private $delegates = array();
16
+
17
+    protected function __construct() {
18
+
19
+    }
20
+
21
+    private static $instance = null;
22
+
23
+    /**
24
+     * The singleton instance.
25
+     *
26
+     * @return Relation_Service_Interface
27
+     */
28
+    public static function get_instance() {
29
+        if ( ! isset( self::$instance ) ) {
30
+            self::$instance = new self();
31
+
32
+            self::$instance->register_delegate( Relation_Instances_Relation_Service::get_instance() );
33
+            self::$instance->register_delegate( Post_Terms_Relation_Service::get_instance() );
34
+            self::$instance->register_delegate( Post_Content_Relation_Service::get_instance() );
35
+        }
36
+
37
+        return self::$instance;
38
+    }
39
+
40
+    public function register_delegate( $delegate ) {
41
+        Assertions::is_a( $delegate, 'Wordlift\Relation\Relation_Service_Interface', 'A `delegate` must implement the `Wordlift\Relation\Relation_Service_Interface` interface.' );
42
+
43
+        $this->delegates[] = $delegate;
44
+    }
45
+
46
+    public function add_relations( $content_id, $relations ) {
47
+        Assertions::is_set( $relations, '`$relations` should be set to a `Relations` instance.' );
48
+
49
+        foreach ( $this->delegates as $delegate ) {
50
+            $delegate->add_relations( $content_id, $relations );
51
+        }
52
+    }
53
+
54
+    /**
55
+     * This helper method is used to create relations from entity_uris.
56
+     * Its only purpose as of now is to process the entity_uris emitted
57
+     * from disambiguation widget.
58
+     *
59
+     * @param $subject Wordpress_Content_Id
60
+     * @param $uris
61
+     *
62
+     * @return array<Relation>
63
+     */
64
+    public static function get_relations_from_uris( $subject, $uris ) {
65
+
66
+        $entity_service = \Wordlift_Entity_Service::get_instance();
67
+
68
+        return array_filter(
69
+            array_map(
70
+                function ( $uri ) use ( $entity_service, $subject ) {
71
+                    /**
72
+                     * @var $content Content|null
73
+                     */
74
+                    $content = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as( $uri );
75
+                    $bag     = $content->get_bag();
76
+                    if ( $bag instanceof \WP_Post || $bag instanceof \WP_Term ) {
77
+                        $predicate = $bag instanceof \WP_Term ? WL_WHAT_RELATION : $entity_service->get_classification_scope_for( $content->get_id() );
78
+                        return new Relation( $subject, new Wordpress_Content_Id( $content->get_id(), $content->get_object_type_enum() ), $predicate );
79
+                    }
80
+                    return false;
81
+                },
82
+                $uris
83
+            )
84
+        );
85
+
86
+    }
87 87
 
88 88
 }
Please login to merge, or discard this patch.
Spacing   +16 added lines, -16 removed lines patch added patch discarded remove patch
@@ -26,28 +26,28 @@  discard block
 block discarded – undo
26 26
 	 * @return Relation_Service_Interface
27 27
 	 */
28 28
 	public static function get_instance() {
29
-		if ( ! isset( self::$instance ) ) {
29
+		if ( ! isset(self::$instance)) {
30 30
 			self::$instance = new self();
31 31
 
32
-			self::$instance->register_delegate( Relation_Instances_Relation_Service::get_instance() );
33
-			self::$instance->register_delegate( Post_Terms_Relation_Service::get_instance() );
34
-			self::$instance->register_delegate( Post_Content_Relation_Service::get_instance() );
32
+			self::$instance->register_delegate(Relation_Instances_Relation_Service::get_instance());
33
+			self::$instance->register_delegate(Post_Terms_Relation_Service::get_instance());
34
+			self::$instance->register_delegate(Post_Content_Relation_Service::get_instance());
35 35
 		}
36 36
 
37 37
 		return self::$instance;
38 38
 	}
39 39
 
40
-	public function register_delegate( $delegate ) {
41
-		Assertions::is_a( $delegate, 'Wordlift\Relation\Relation_Service_Interface', 'A `delegate` must implement the `Wordlift\Relation\Relation_Service_Interface` interface.' );
40
+	public function register_delegate($delegate) {
41
+		Assertions::is_a($delegate, 'Wordlift\Relation\Relation_Service_Interface', 'A `delegate` must implement the `Wordlift\Relation\Relation_Service_Interface` interface.');
42 42
 
43 43
 		$this->delegates[] = $delegate;
44 44
 	}
45 45
 
46
-	public function add_relations( $content_id, $relations ) {
47
-		Assertions::is_set( $relations, '`$relations` should be set to a `Relations` instance.' );
46
+	public function add_relations($content_id, $relations) {
47
+		Assertions::is_set($relations, '`$relations` should be set to a `Relations` instance.');
48 48
 
49
-		foreach ( $this->delegates as $delegate ) {
50
-			$delegate->add_relations( $content_id, $relations );
49
+		foreach ($this->delegates as $delegate) {
50
+			$delegate->add_relations($content_id, $relations);
51 51
 		}
52 52
 	}
53 53
 
@@ -61,21 +61,21 @@  discard block
 block discarded – undo
61 61
 	 *
62 62
 	 * @return array<Relation>
63 63
 	 */
64
-	public static function get_relations_from_uris( $subject, $uris ) {
64
+	public static function get_relations_from_uris($subject, $uris) {
65 65
 
66 66
 		$entity_service = \Wordlift_Entity_Service::get_instance();
67 67
 
68 68
 		return array_filter(
69 69
 			array_map(
70
-				function ( $uri ) use ( $entity_service, $subject ) {
70
+				function($uri) use ($entity_service, $subject) {
71 71
 					/**
72 72
 					 * @var $content Content|null
73 73
 					 */
74
-					$content = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as( $uri );
74
+					$content = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as($uri);
75 75
 					$bag     = $content->get_bag();
76
-					if ( $bag instanceof \WP_Post || $bag instanceof \WP_Term ) {
77
-						$predicate = $bag instanceof \WP_Term ? WL_WHAT_RELATION : $entity_service->get_classification_scope_for( $content->get_id() );
78
-						return new Relation( $subject, new Wordpress_Content_Id( $content->get_id(), $content->get_object_type_enum() ), $predicate );
76
+					if ($bag instanceof \WP_Post || $bag instanceof \WP_Term) {
77
+						$predicate = $bag instanceof \WP_Term ? WL_WHAT_RELATION : $entity_service->get_classification_scope_for($content->get_id());
78
+						return new Relation($subject, new Wordpress_Content_Id($content->get_id(), $content->get_object_type_enum()), $predicate);
79 79
 					}
80 80
 					return false;
81 81
 				},
Please login to merge, or discard this patch.
src/wordlift/relation/class-relation.php 2 patches
Indentation   +138 added lines, -138 removed lines patch added patch discarded remove patch
@@ -16,142 +16,142 @@
 block discarded – undo
16 16
 use Wordlift\Content\Wordpress\Wordpress_Content_Id;
17 17
 
18 18
 class Relation implements Hashable_Interface, JsonSerializable {
19
-	/**
20
-	 * @var Wordpress_Content_Id
21
-	 */
22
-	private $subject;
23
-
24
-	/**
25
-	 * @var string
26
-	 */
27
-	private $predicate;
28
-
29
-	/**
30
-	 * @param Wordpress_Content_Id $subject
31
-	 * @param Wordpress_Content_Id $object
32
-	 * @param string               $predicate
33
-	 */
34
-	public function __construct( $subject, $object, $predicate ) {
35
-		$this->subject   = $subject;
36
-		$this->predicate = $predicate;
37
-		$this->object    = $object;
38
-	}
39
-
40
-	/**
41
-	 * @var Wordpress_Content_Id
42
-	 */
43
-	private $object;
44
-
45
-	public static function from_json( $item ) {
46
-		$subject   = Wordpress_Content_Id::from_json( $item['subject'] );
47
-		$object    = Wordpress_Content_Id::from_json( $item['object'] );
48
-		$predicate = $item['predicate'];
49
-
50
-		return new self( $subject, $object, $predicate );
51
-	}
52
-
53
-	/**
54
-	 * @return Wordpress_Content_Id
55
-	 */
56
-	public function get_subject() {
57
-		return $this->subject;
58
-	}
59
-
60
-	/**
61
-	 * @return string
62
-	 */
63
-	public function get_predicate() {
64
-		return $this->predicate;
65
-	}
66
-
67
-	/**
68
-	 * @return Wordpress_Content_Id
69
-	 */
70
-	public function get_object() {
71
-		return $this->object;
72
-	}
73
-
74
-	public function hash() {
75
-		// Define the hash algorithm for your object
76
-		// Here's an example using the md5 hash function
77
-		return md5(
78
-		// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
79
-			serialize(
80
-				array(
81
-					$this->get_subject(),
82
-					$this->get_predicate(),
83
-					$this->get_object(),
84
-				)
85
-			)
86
-		);
87
-	}
88
-
89
-	public function equals( Hashable_Interface $obj ) {
90
-		return $this->hash() === $obj->hash();
91
-	}
92
-
93
-	/**
94
-	 * @return string
95
-	 * @deprecated used for legacy compatibilty
96
-	 */
97
-	public function get_relation_type() {
98
-		return $this->predicate;
99
-	}
100
-
101
-	/**
102
-	 * @return int
103
-	 * @deprecated used for legacy compatibility
104
-	 */
105
-	public function get_object_id() {
106
-		return $this->object->get_id();
107
-	}
108
-
109
-	/**
110
-	 * Subject type.
111
-	 *
112
-	 * @deprecated used for legacy compatibility
113
-	 */
114
-	public function get_subject_type() {
115
-		return $this->subject->get_type();
116
-	}
117
-
118
-	/**
119
-	 * Object type.
120
-	 *
121
-	 * @deprecated used for legacy compatibility
122
-	 */
123
-	public function get_object_type() {
124
-		return $this->object->get_type();
125
-	}
126
-
127
-	public static function from_relation_instances( $instance ) {
128
-		Assertions::is_set( $instance->subject_id );
129
-		Assertions::is_set( $instance->subject_type );
130
-		Assertions::is_set( $instance->predicate );
131
-		Assertions::is_set( $instance->object_id );
132
-		Assertions::is_set( $instance->object_type );
133
-
134
-		return new self(
135
-		// Subject.
136
-			new Wordpress_Content_Id(
137
-				$instance->subject_id,
138
-				$instance->subject_type
139
-			),
140
-			// Object.
141
-			new Wordpress_Content_Id(
142
-				$instance->object_id,
143
-				$instance->object_type
144
-			),
145
-			// Predicate.
146
-			$instance->predicate
147
-		);
148
-	}
149
-
150
-	public function jsonSerialize() {
151
-		return array(
152
-			'subject'   => $this->get_subject(),
153
-			'object'    => $this->get_object(),
154
-			'predicate' => $this->get_predicate(),
155
-		);
156
-	}
19
+    /**
20
+     * @var Wordpress_Content_Id
21
+     */
22
+    private $subject;
23
+
24
+    /**
25
+     * @var string
26
+     */
27
+    private $predicate;
28
+
29
+    /**
30
+     * @param Wordpress_Content_Id $subject
31
+     * @param Wordpress_Content_Id $object
32
+     * @param string               $predicate
33
+     */
34
+    public function __construct( $subject, $object, $predicate ) {
35
+        $this->subject   = $subject;
36
+        $this->predicate = $predicate;
37
+        $this->object    = $object;
38
+    }
39
+
40
+    /**
41
+     * @var Wordpress_Content_Id
42
+     */
43
+    private $object;
44
+
45
+    public static function from_json( $item ) {
46
+        $subject   = Wordpress_Content_Id::from_json( $item['subject'] );
47
+        $object    = Wordpress_Content_Id::from_json( $item['object'] );
48
+        $predicate = $item['predicate'];
49
+
50
+        return new self( $subject, $object, $predicate );
51
+    }
52
+
53
+    /**
54
+     * @return Wordpress_Content_Id
55
+     */
56
+    public function get_subject() {
57
+        return $this->subject;
58
+    }
59
+
60
+    /**
61
+     * @return string
62
+     */
63
+    public function get_predicate() {
64
+        return $this->predicate;
65
+    }
66
+
67
+    /**
68
+     * @return Wordpress_Content_Id
69
+     */
70
+    public function get_object() {
71
+        return $this->object;
72
+    }
73
+
74
+    public function hash() {
75
+        // Define the hash algorithm for your object
76
+        // Here's an example using the md5 hash function
77
+        return md5(
78
+        // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
79
+            serialize(
80
+                array(
81
+                    $this->get_subject(),
82
+                    $this->get_predicate(),
83
+                    $this->get_object(),
84
+                )
85
+            )
86
+        );
87
+    }
88
+
89
+    public function equals( Hashable_Interface $obj ) {
90
+        return $this->hash() === $obj->hash();
91
+    }
92
+
93
+    /**
94
+     * @return string
95
+     * @deprecated used for legacy compatibilty
96
+     */
97
+    public function get_relation_type() {
98
+        return $this->predicate;
99
+    }
100
+
101
+    /**
102
+     * @return int
103
+     * @deprecated used for legacy compatibility
104
+     */
105
+    public function get_object_id() {
106
+        return $this->object->get_id();
107
+    }
108
+
109
+    /**
110
+     * Subject type.
111
+     *
112
+     * @deprecated used for legacy compatibility
113
+     */
114
+    public function get_subject_type() {
115
+        return $this->subject->get_type();
116
+    }
117
+
118
+    /**
119
+     * Object type.
120
+     *
121
+     * @deprecated used for legacy compatibility
122
+     */
123
+    public function get_object_type() {
124
+        return $this->object->get_type();
125
+    }
126
+
127
+    public static function from_relation_instances( $instance ) {
128
+        Assertions::is_set( $instance->subject_id );
129
+        Assertions::is_set( $instance->subject_type );
130
+        Assertions::is_set( $instance->predicate );
131
+        Assertions::is_set( $instance->object_id );
132
+        Assertions::is_set( $instance->object_type );
133
+
134
+        return new self(
135
+        // Subject.
136
+            new Wordpress_Content_Id(
137
+                $instance->subject_id,
138
+                $instance->subject_type
139
+            ),
140
+            // Object.
141
+            new Wordpress_Content_Id(
142
+                $instance->object_id,
143
+                $instance->object_type
144
+            ),
145
+            // Predicate.
146
+            $instance->predicate
147
+        );
148
+    }
149
+
150
+    public function jsonSerialize() {
151
+        return array(
152
+            'subject'   => $this->get_subject(),
153
+            'object'    => $this->get_object(),
154
+            'predicate' => $this->get_predicate(),
155
+        );
156
+    }
157 157
 }
Please login to merge, or discard this patch.
Spacing   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -31,7 +31,7 @@  discard block
 block discarded – undo
31 31
 	 * @param Wordpress_Content_Id $object
32 32
 	 * @param string               $predicate
33 33
 	 */
34
-	public function __construct( $subject, $object, $predicate ) {
34
+	public function __construct($subject, $object, $predicate) {
35 35
 		$this->subject   = $subject;
36 36
 		$this->predicate = $predicate;
37 37
 		$this->object    = $object;
@@ -42,12 +42,12 @@  discard block
 block discarded – undo
42 42
 	 */
43 43
 	private $object;
44 44
 
45
-	public static function from_json( $item ) {
46
-		$subject   = Wordpress_Content_Id::from_json( $item['subject'] );
47
-		$object    = Wordpress_Content_Id::from_json( $item['object'] );
45
+	public static function from_json($item) {
46
+		$subject   = Wordpress_Content_Id::from_json($item['subject']);
47
+		$object    = Wordpress_Content_Id::from_json($item['object']);
48 48
 		$predicate = $item['predicate'];
49 49
 
50
-		return new self( $subject, $object, $predicate );
50
+		return new self($subject, $object, $predicate);
51 51
 	}
52 52
 
53 53
 	/**
@@ -86,7 +86,7 @@  discard block
 block discarded – undo
86 86
 		);
87 87
 	}
88 88
 
89
-	public function equals( Hashable_Interface $obj ) {
89
+	public function equals(Hashable_Interface $obj) {
90 90
 		return $this->hash() === $obj->hash();
91 91
 	}
92 92
 
@@ -124,12 +124,12 @@  discard block
 block discarded – undo
124 124
 		return $this->object->get_type();
125 125
 	}
126 126
 
127
-	public static function from_relation_instances( $instance ) {
128
-		Assertions::is_set( $instance->subject_id );
129
-		Assertions::is_set( $instance->subject_type );
130
-		Assertions::is_set( $instance->predicate );
131
-		Assertions::is_set( $instance->object_id );
132
-		Assertions::is_set( $instance->object_type );
127
+	public static function from_relation_instances($instance) {
128
+		Assertions::is_set($instance->subject_id);
129
+		Assertions::is_set($instance->subject_type);
130
+		Assertions::is_set($instance->predicate);
131
+		Assertions::is_set($instance->object_id);
132
+		Assertions::is_set($instance->object_type);
133 133
 
134 134
 		return new self(
135 135
 		// Subject.
Please login to merge, or discard this patch.
src/install/class-wordlift-install-3-32-0.php 1 patch
Indentation   +41 added lines, -41 removed lines patch added patch discarded remove patch
@@ -6,49 +6,49 @@
 block discarded – undo
6 6
  */
7 7
 class Wordlift_Install_3_32_0 extends Wordlift_Install {
8 8
 
9
-	/**
10
-	 * {@inheritdoc}
11
-	 */
12
-	protected static $version = '3.32.0';
13
-
14
-	public static function is_column_exists( $column_name ) {
15
-		global $wpdb;
16
-
17
-		return $wpdb->get_results(
18
-			$wpdb->prepare(
19
-				"SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name ='{$wpdb->prefix}wl_relation_instances' AND column_name = %s",
20
-				$column_name
21
-			)
22
-		);
23
-
24
-	}
25
-
26
-	public function install() {
27
-		global $wpdb;
28
-		// Allocate only 8 bytes, represent 2 ^ 8 values in signed form ( -128 to 127 )
29
-		// we default to 0 here, because we are going to represent Object_Type_Enum in
30
-		// this field
31
-		// const POST = 0;
32
-		// const TERM = 1;
33
-		// const HOMEPAGE = 2;
34
-		// const USER = 3;
35
-		// we add 0 as default since we want to add compat between old and new values.
36
-
37
-		if ( ! self::is_column_exists( 'object_type' ) ) {
38
-			// Add object_type column
39
-			$wpdb->query(
40
-				"ALTER TABLE {$wpdb->prefix}wl_relation_instances
9
+    /**
10
+     * {@inheritdoc}
11
+     */
12
+    protected static $version = '3.32.0';
13
+
14
+    public static function is_column_exists( $column_name ) {
15
+        global $wpdb;
16
+
17
+        return $wpdb->get_results(
18
+            $wpdb->prepare(
19
+                "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name ='{$wpdb->prefix}wl_relation_instances' AND column_name = %s",
20
+                $column_name
21
+            )
22
+        );
23
+
24
+    }
25
+
26
+    public function install() {
27
+        global $wpdb;
28
+        // Allocate only 8 bytes, represent 2 ^ 8 values in signed form ( -128 to 127 )
29
+        // we default to 0 here, because we are going to represent Object_Type_Enum in
30
+        // this field
31
+        // const POST = 0;
32
+        // const TERM = 1;
33
+        // const HOMEPAGE = 2;
34
+        // const USER = 3;
35
+        // we add 0 as default since we want to add compat between old and new values.
36
+
37
+        if ( ! self::is_column_exists( 'object_type' ) ) {
38
+            // Add object_type column
39
+            $wpdb->query(
40
+                "ALTER TABLE {$wpdb->prefix}wl_relation_instances
41 41
 					ADD object_type TINYINT DEFAULT 0;"
42
-			);
43
-		}
42
+            );
43
+        }
44 44
 
45
-		if ( ! self::is_column_exists( 'subject_type' ) ) {
46
-			// Add subject_type column.
47
-			$wpdb->query(
48
-				"ALTER TABLE {$wpdb->prefix}wl_relation_instances
45
+        if ( ! self::is_column_exists( 'subject_type' ) ) {
46
+            // Add subject_type column.
47
+            $wpdb->query(
48
+                "ALTER TABLE {$wpdb->prefix}wl_relation_instances
49 49
 					ADD subject_type TINYINT DEFAULT 0;"
50
-			);
51
-		}
52
-	}
50
+            );
51
+        }
52
+    }
53 53
 
54 54
 }
Please login to merge, or discard this patch.
src/modules/linked_data/wordlift-linked-data.php 2 patches
Indentation   +462 added lines, -462 removed lines patch added patch discarded remove patch
@@ -23,61 +23,61 @@  discard block
 block discarded – undo
23 23
  */
24 24
 function wl_linked_data_save_post( $post_id ) {
25 25
 
26
-	$log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post' );
27
-
28
-	$log->trace( "Saving post $post_id to Linked Data..." );
29
-
30
-	// If it's not numeric exit from here.
31
-	if ( ! is_numeric( $post_id ) || is_numeric( wp_is_post_revision( $post_id ) ) ) {
32
-		$log->debug( "Skipping $post_id, because id is not numeric or is a post revision." );
33
-
34
-		return;
35
-	}
36
-
37
-	// Get the post type and check whether it supports the editor.
38
-	//
39
-	// @see https://github.com/insideout10/wordlift-plugin/issues/659.
40
-	$post_type = get_post_type( $post_id );
41
-	/**
42
-	 * Use `wl_post_type_supports_editor` which calls a filter to allow 3rd parties to change the setting.
43
-	 *
44
-	 * @since 3.19.4
45
-	 *
46
-	 * @see https://github.com/insideout10/wordlift-plugin/issues/847.
47
-	 */
48
-	$is_editor_supported = wl_post_type_supports_editor( $post_type );
49
-
50
-	$is_no_editor_analysis_enabled = Wordlift\No_Editor_Analysis\No_Editor_Analysis_Feature::can_no_editor_analysis_be_used( $post_id );
51
-	// Bail out if it's not an entity.
52
-	if ( ! $is_editor_supported
53
-		 && ! $is_no_editor_analysis_enabled ) {
54
-		$log->debug( "Skipping $post_id, because $post_type doesn't support the editor (content)." );
55
-
56
-		return;
57
-	}
58
-
59
-	/**
60
-	 * Only process valid post types
61
-	 *
62
-	 * @since 3.25.6
63
-	 */
64
-	$supported_types = Wordlift_Entity_Service::valid_entity_post_types();
65
-
66
-	// Bail out if it's not a valid entity.
67
-	if ( ! in_array( $post_type, $supported_types, true ) && ! $is_no_editor_analysis_enabled ) {
68
-		$log->debug( "Skipping $post_id, because $post_type is not a valid entity." );
69
-
70
-		return;
71
-	}
72
-
73
-	// Unhook this function so it doesn't loop infinitely.
74
-	remove_action( 'save_post', 'wl_linked_data_save_post' );
75
-
76
-	// raise the *wl_linked_data_save_post* event.
77
-	do_action( 'wl_linked_data_save_post', $post_id );
78
-
79
-	// Re-hook this function.
80
-	add_action( 'save_post', 'wl_linked_data_save_post' );
26
+    $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post' );
27
+
28
+    $log->trace( "Saving post $post_id to Linked Data..." );
29
+
30
+    // If it's not numeric exit from here.
31
+    if ( ! is_numeric( $post_id ) || is_numeric( wp_is_post_revision( $post_id ) ) ) {
32
+        $log->debug( "Skipping $post_id, because id is not numeric or is a post revision." );
33
+
34
+        return;
35
+    }
36
+
37
+    // Get the post type and check whether it supports the editor.
38
+    //
39
+    // @see https://github.com/insideout10/wordlift-plugin/issues/659.
40
+    $post_type = get_post_type( $post_id );
41
+    /**
42
+     * Use `wl_post_type_supports_editor` which calls a filter to allow 3rd parties to change the setting.
43
+     *
44
+     * @since 3.19.4
45
+     *
46
+     * @see https://github.com/insideout10/wordlift-plugin/issues/847.
47
+     */
48
+    $is_editor_supported = wl_post_type_supports_editor( $post_type );
49
+
50
+    $is_no_editor_analysis_enabled = Wordlift\No_Editor_Analysis\No_Editor_Analysis_Feature::can_no_editor_analysis_be_used( $post_id );
51
+    // Bail out if it's not an entity.
52
+    if ( ! $is_editor_supported
53
+         && ! $is_no_editor_analysis_enabled ) {
54
+        $log->debug( "Skipping $post_id, because $post_type doesn't support the editor (content)." );
55
+
56
+        return;
57
+    }
58
+
59
+    /**
60
+     * Only process valid post types
61
+     *
62
+     * @since 3.25.6
63
+     */
64
+    $supported_types = Wordlift_Entity_Service::valid_entity_post_types();
65
+
66
+    // Bail out if it's not a valid entity.
67
+    if ( ! in_array( $post_type, $supported_types, true ) && ! $is_no_editor_analysis_enabled ) {
68
+        $log->debug( "Skipping $post_id, because $post_type is not a valid entity." );
69
+
70
+        return;
71
+    }
72
+
73
+    // Unhook this function so it doesn't loop infinitely.
74
+    remove_action( 'save_post', 'wl_linked_data_save_post' );
75
+
76
+    // raise the *wl_linked_data_save_post* event.
77
+    do_action( 'wl_linked_data_save_post', $post_id );
78
+
79
+    // Re-hook this function.
80
+    add_action( 'save_post', 'wl_linked_data_save_post' );
81 81
 }
82 82
 
83 83
 add_action( 'save_post', 'wl_linked_data_save_post' );
@@ -93,204 +93,204 @@  discard block
 block discarded – undo
93 93
  */
94 94
 function wl_linked_data_save_post_and_related_entities( $post_id ) {
95 95
 
96
-	$log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post_and_related_entities' );
97
-
98
-	$log->trace( "Saving $post_id to Linked Data along with related entities..." );
99
-
100
-	// Ignore auto-saves
101
-	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
102
-		$log->trace( 'Doing autosave, skipping...' );
103
-
104
-		return;
105
-	}
106
-
107
-	// get the current post.
108
-	$post = get_post( $post_id );
109
-
110
-	remove_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' );
111
-
112
-	// wl_write_log( "[ post id :: $post_id ][ autosave :: false ][ post type :: $post->post_type ]" );
113
-
114
-	// Get the entity service instance.
115
-	$entity_service  = Wordlift_Entity_Service::get_instance();
116
-	$uri_service     = Wordlift_Entity_Uri_Service::get_instance();
117
-	$content_service = Wordpress_Content_Service::get_instance();
118
-
119
-	// Store mapping between tmp new entities uris and real new entities uri
120
-	$entities_uri_mapping = array();
121
-
122
-	// Save all the selected internal entity uris to this variable.
123
-	$internal_entity_uris = array();
124
-
125
-	// Save the entities coming with POST data.
126
-	if ( isset( $_POST['wl_entities'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
127
-		$data              = filter_var_array( $_POST, array( 'wl_entities' => array( 'flags' => FILTER_REQUIRE_ARRAY ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
128
-		$entities_via_post = $data['wl_entities'];
129
-		wl_write_log( "[ post id :: $post_id ][ POST(wl_entities) :: " );
130
-		wl_write_log( wp_json_encode( $entities_via_post ) );
131
-		wl_write_log( ']' );
132
-
133
-		foreach ( $entities_via_post as $entity_uri => $entity ) {
134
-
135
-			if ( preg_match( '/^local-entity-.+/', $entity_uri ) ) {
136
-				$existing_entity = get_page_by_title( $entity['label'], OBJECT, Wordlift_Entity_Service::valid_entity_post_types() );
137
-				if ( isset( $existing_entity ) ) {
138
-					$existing_entity_type = Wordlift_Entity_Type_Service::get_instance()->get( $existing_entity->ID );
139
-					// Type doesn't match, continue to create a new entity.
140
-					if ( ! isset( $existing_entity_type ) || $existing_entity_type['css_class'] !== $entity['main_type'] ) {
141
-						$existing_entity = null;
142
-					}
143
-				}
144
-			} else {
145
-				// Look if current entity uri matches an internal existing entity, meaning:
146
-				// 1. when $entity_uri is an internal uri
147
-				// 2. when $entity_uri is an external uri used as sameAs of an internal entity
148
-				$existing_entity = $entity_service->get_entity_post_by_uri( $entity_uri );
149
-			}
150
-
151
-			// Don't save the entities which are not found, but also local.
152
-			if ( ! isset( $existing_entity ) && $uri_service->is_internal( $entity_uri ) ) {
153
-				$internal_entity_uris[] = $entity_uri;
154
-				continue;
155
-			}
156
-
157
-			if ( ! isset( $existing_entity ) ) {
158
-				// Update entity data with related post
159
-				$entity['related_post_id'] = $post_id;
160
-				// New entity, save it.
161
-				$existing_entity = wl_save_entity( $entity );
162
-			} else {
163
-				// Existing entity, update post status.
164
-				if ( $existing_entity instanceof WP_Post && 'publish' !== $existing_entity->post_status ) {
165
-					// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
166
-					$post_status = apply_filters( 'wl_feature__enable__entity-auto-publish', true )
167
-						? $post->post_status : 'draft';
168
-					wl_update_post_status( $existing_entity->ID, $post_status );
169
-				}
170
-			}
171
-
172
-			$uri = $content_service->get_entity_id( Wordpress_Content_Id::create_post( $existing_entity->ID ) );
173
-
174
-			$internal_entity_uris[] = $uri;
175
-			wl_write_log( "Map $entity_uri on $uri" );
176
-			$entities_uri_mapping[ $entity_uri ] = $uri;
177
-
178
-		}
179
-	}
180
-
181
-	// Replace tmp uris in content post if needed
182
-	$updated_post_content = $post->post_content;
183
-
184
-	// Update the post content if we found mappings of new entities.
185
-	if ( ! empty( $entities_uri_mapping ) ) {
186
-		// Save each entity and store the post id.
187
-		foreach ( $entities_uri_mapping as $tmp_uri => $uri ) {
188
-			if ( 1 !== preg_match( '@^(https?://|local-entity-)@', $tmp_uri ) ) {
189
-				continue;
190
-			}
191
-
192
-			$updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content );
193
-		}
194
-
195
-		// Update the post content.
196
-		/**
197
-		 * Note: wp_update_post do stripslashes before saving the
198
-		 * content, so add the slashes to prevent back slash getting
199
-		 * removed.
200
-		 */
201
-		wp_update_post(
202
-			array(
203
-				'ID'           => $post->ID,
204
-				'post_content' => addslashes( $updated_post_content ),
205
-			)
206
-		);
207
-	}
208
-
209
-	// Reset previously saved instances.
210
-	wl_core_delete_relation_instances( $post_id );
211
-
212
-	$content_id = Wordpress_Content_Id::create_post( $post->ID );
213
-	$relations  = Relation_Service::get_instance()->get_relations( $content_id );
214
-
215
-	if ( No_Editor_Analysis_Feature::can_no_editor_analysis_be_used( $post_id ) ) {
216
-		$relations->add( ...Relation_Service::get_relations_from_uris( $content_id, $internal_entity_uris ) );
217
-	}
218
-	/**
219
-	 * Filter the relations, we dont want to create a relation
220
-	 * to uncategorized term, we are already filtering this on jsonld,
221
-	 *
222
-	 * @todo: do i need to move this to post-term-relation-service ?
223
-	 */
224
-	$filtered_relations = array_filter(
225
-		$relations->toArray(),
226
-		function ( $item ) {
227
-			/**
228
-			 * @var $item Relation
229
-			 */
230
-			$object = $item->get_object();
231
-
232
-			return ! ( $object->get_type() === Object_Type_Enum::TERM
233
-			&& $object->get_id() === 1 );
234
-		}
235
-	);
236
-
237
-	// Save relation instances
238
-	/** @var Relation $relation */
239
-	foreach ( $filtered_relations  as $relation ) {
240
-		$subject = $relation->get_subject();
241
-		$object  = $relation->get_object();
242
-
243
-		wl_core_add_relation_instance(
244
-		// subject id.
245
-			$subject->get_id(),
246
-			// what, where, when, who
247
-			$relation->get_predicate(),
248
-			// object id.
249
-			$object->get_id(),
250
-			// Subject type.
251
-			$subject->get_type(),
252
-			// Object type.
253
-			$object->get_type()
254
-		);
255
-
256
-	}
257
-
258
-	if ( isset( $_POST['wl_entities'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
259
-		// Save post metadata if available
260
-		$metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ? filter_input_array( INPUT_POST, array( 'wl_metadata' => FILTER_DEFAULT ) ) : array(); // phpcs:ignore WordPress.Security.NonceVerification.Missing
261
-
262
-		$fields = array(
263
-			Wordlift_Schema_Service::FIELD_LOCATION_CREATED,
264
-			Wordlift_Schema_Service::FIELD_TOPIC,
265
-		);
266
-
267
-		// Unlink topic taxonomy terms
268
-		Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for( $post->ID );
269
-
270
-		foreach ( $fields as $field ) {
271
-
272
-			// Delete current values
273
-			delete_post_meta( $post->ID, $field );
274
-			// Retrieve the entity uri
275
-			$uri = ( isset( $metadata_via_post[ $field ] ) ) ?
276
-				stripslashes( $metadata_via_post[ $field ] ) : '';
277
-
278
-			if ( empty( $uri ) ) {
279
-				continue;
280
-			}
281
-			$entity = $entity_service->get_entity_post_by_uri( $uri );
282
-
283
-			if ( $entity ) {
284
-				add_post_meta( $post->ID, $field, $entity->ID, true );
285
-				// Set also the topic taxonomy
286
-				if ( Wordlift_Schema_Service::FIELD_TOPIC === $field ) {
287
-					Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for( $post->ID, $entity );
288
-				}
289
-			}
290
-		}
291
-	}
292
-
293
-	add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' );
96
+    $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post_and_related_entities' );
97
+
98
+    $log->trace( "Saving $post_id to Linked Data along with related entities..." );
99
+
100
+    // Ignore auto-saves
101
+    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
102
+        $log->trace( 'Doing autosave, skipping...' );
103
+
104
+        return;
105
+    }
106
+
107
+    // get the current post.
108
+    $post = get_post( $post_id );
109
+
110
+    remove_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' );
111
+
112
+    // wl_write_log( "[ post id :: $post_id ][ autosave :: false ][ post type :: $post->post_type ]" );
113
+
114
+    // Get the entity service instance.
115
+    $entity_service  = Wordlift_Entity_Service::get_instance();
116
+    $uri_service     = Wordlift_Entity_Uri_Service::get_instance();
117
+    $content_service = Wordpress_Content_Service::get_instance();
118
+
119
+    // Store mapping between tmp new entities uris and real new entities uri
120
+    $entities_uri_mapping = array();
121
+
122
+    // Save all the selected internal entity uris to this variable.
123
+    $internal_entity_uris = array();
124
+
125
+    // Save the entities coming with POST data.
126
+    if ( isset( $_POST['wl_entities'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
127
+        $data              = filter_var_array( $_POST, array( 'wl_entities' => array( 'flags' => FILTER_REQUIRE_ARRAY ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
128
+        $entities_via_post = $data['wl_entities'];
129
+        wl_write_log( "[ post id :: $post_id ][ POST(wl_entities) :: " );
130
+        wl_write_log( wp_json_encode( $entities_via_post ) );
131
+        wl_write_log( ']' );
132
+
133
+        foreach ( $entities_via_post as $entity_uri => $entity ) {
134
+
135
+            if ( preg_match( '/^local-entity-.+/', $entity_uri ) ) {
136
+                $existing_entity = get_page_by_title( $entity['label'], OBJECT, Wordlift_Entity_Service::valid_entity_post_types() );
137
+                if ( isset( $existing_entity ) ) {
138
+                    $existing_entity_type = Wordlift_Entity_Type_Service::get_instance()->get( $existing_entity->ID );
139
+                    // Type doesn't match, continue to create a new entity.
140
+                    if ( ! isset( $existing_entity_type ) || $existing_entity_type['css_class'] !== $entity['main_type'] ) {
141
+                        $existing_entity = null;
142
+                    }
143
+                }
144
+            } else {
145
+                // Look if current entity uri matches an internal existing entity, meaning:
146
+                // 1. when $entity_uri is an internal uri
147
+                // 2. when $entity_uri is an external uri used as sameAs of an internal entity
148
+                $existing_entity = $entity_service->get_entity_post_by_uri( $entity_uri );
149
+            }
150
+
151
+            // Don't save the entities which are not found, but also local.
152
+            if ( ! isset( $existing_entity ) && $uri_service->is_internal( $entity_uri ) ) {
153
+                $internal_entity_uris[] = $entity_uri;
154
+                continue;
155
+            }
156
+
157
+            if ( ! isset( $existing_entity ) ) {
158
+                // Update entity data with related post
159
+                $entity['related_post_id'] = $post_id;
160
+                // New entity, save it.
161
+                $existing_entity = wl_save_entity( $entity );
162
+            } else {
163
+                // Existing entity, update post status.
164
+                if ( $existing_entity instanceof WP_Post && 'publish' !== $existing_entity->post_status ) {
165
+                    // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
166
+                    $post_status = apply_filters( 'wl_feature__enable__entity-auto-publish', true )
167
+                        ? $post->post_status : 'draft';
168
+                    wl_update_post_status( $existing_entity->ID, $post_status );
169
+                }
170
+            }
171
+
172
+            $uri = $content_service->get_entity_id( Wordpress_Content_Id::create_post( $existing_entity->ID ) );
173
+
174
+            $internal_entity_uris[] = $uri;
175
+            wl_write_log( "Map $entity_uri on $uri" );
176
+            $entities_uri_mapping[ $entity_uri ] = $uri;
177
+
178
+        }
179
+    }
180
+
181
+    // Replace tmp uris in content post if needed
182
+    $updated_post_content = $post->post_content;
183
+
184
+    // Update the post content if we found mappings of new entities.
185
+    if ( ! empty( $entities_uri_mapping ) ) {
186
+        // Save each entity and store the post id.
187
+        foreach ( $entities_uri_mapping as $tmp_uri => $uri ) {
188
+            if ( 1 !== preg_match( '@^(https?://|local-entity-)@', $tmp_uri ) ) {
189
+                continue;
190
+            }
191
+
192
+            $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content );
193
+        }
194
+
195
+        // Update the post content.
196
+        /**
197
+         * Note: wp_update_post do stripslashes before saving the
198
+         * content, so add the slashes to prevent back slash getting
199
+         * removed.
200
+         */
201
+        wp_update_post(
202
+            array(
203
+                'ID'           => $post->ID,
204
+                'post_content' => addslashes( $updated_post_content ),
205
+            )
206
+        );
207
+    }
208
+
209
+    // Reset previously saved instances.
210
+    wl_core_delete_relation_instances( $post_id );
211
+
212
+    $content_id = Wordpress_Content_Id::create_post( $post->ID );
213
+    $relations  = Relation_Service::get_instance()->get_relations( $content_id );
214
+
215
+    if ( No_Editor_Analysis_Feature::can_no_editor_analysis_be_used( $post_id ) ) {
216
+        $relations->add( ...Relation_Service::get_relations_from_uris( $content_id, $internal_entity_uris ) );
217
+    }
218
+    /**
219
+     * Filter the relations, we dont want to create a relation
220
+     * to uncategorized term, we are already filtering this on jsonld,
221
+     *
222
+     * @todo: do i need to move this to post-term-relation-service ?
223
+     */
224
+    $filtered_relations = array_filter(
225
+        $relations->toArray(),
226
+        function ( $item ) {
227
+            /**
228
+             * @var $item Relation
229
+             */
230
+            $object = $item->get_object();
231
+
232
+            return ! ( $object->get_type() === Object_Type_Enum::TERM
233
+            && $object->get_id() === 1 );
234
+        }
235
+    );
236
+
237
+    // Save relation instances
238
+    /** @var Relation $relation */
239
+    foreach ( $filtered_relations  as $relation ) {
240
+        $subject = $relation->get_subject();
241
+        $object  = $relation->get_object();
242
+
243
+        wl_core_add_relation_instance(
244
+        // subject id.
245
+            $subject->get_id(),
246
+            // what, where, when, who
247
+            $relation->get_predicate(),
248
+            // object id.
249
+            $object->get_id(),
250
+            // Subject type.
251
+            $subject->get_type(),
252
+            // Object type.
253
+            $object->get_type()
254
+        );
255
+
256
+    }
257
+
258
+    if ( isset( $_POST['wl_entities'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
259
+        // Save post metadata if available
260
+        $metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ? filter_input_array( INPUT_POST, array( 'wl_metadata' => FILTER_DEFAULT ) ) : array(); // phpcs:ignore WordPress.Security.NonceVerification.Missing
261
+
262
+        $fields = array(
263
+            Wordlift_Schema_Service::FIELD_LOCATION_CREATED,
264
+            Wordlift_Schema_Service::FIELD_TOPIC,
265
+        );
266
+
267
+        // Unlink topic taxonomy terms
268
+        Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for( $post->ID );
269
+
270
+        foreach ( $fields as $field ) {
271
+
272
+            // Delete current values
273
+            delete_post_meta( $post->ID, $field );
274
+            // Retrieve the entity uri
275
+            $uri = ( isset( $metadata_via_post[ $field ] ) ) ?
276
+                stripslashes( $metadata_via_post[ $field ] ) : '';
277
+
278
+            if ( empty( $uri ) ) {
279
+                continue;
280
+            }
281
+            $entity = $entity_service->get_entity_post_by_uri( $uri );
282
+
283
+            if ( $entity ) {
284
+                add_post_meta( $post->ID, $field, $entity->ID, true );
285
+                // Set also the topic taxonomy
286
+                if ( Wordlift_Schema_Service::FIELD_TOPIC === $field ) {
287
+                    Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for( $post->ID, $entity );
288
+                }
289
+            }
290
+        }
291
+    }
292
+
293
+    add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' );
294 294
 }
295 295
 
296 296
 add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' );
@@ -313,221 +313,221 @@  discard block
 block discarded – undo
313 313
  */
314 314
 function wl_save_entity( $entity_data ) {
315 315
 
316
-	// Required for REST API calls
317
-	if ( ! function_exists( 'wp_crop_image' ) ) {
318
-		require_once ABSPATH . 'wp-admin/includes/image.php';
319
-	}
316
+    // Required for REST API calls
317
+    if ( ! function_exists( 'wp_crop_image' ) ) {
318
+        require_once ABSPATH . 'wp-admin/includes/image.php';
319
+    }
320 320
 
321
-	$log = Wordlift_Log_Service::get_logger( 'wl_save_entity' );
321
+    $log = Wordlift_Log_Service::get_logger( 'wl_save_entity' );
322 322
 
323
-	/*
323
+    /*
324 324
 	 * Data is coming from a $_POST, sanitize it.
325 325
 	 *
326 326
 	 * @since 3.19.4
327 327
 	 *
328 328
 	 * @see https://github.com/insideout10/wordlift-plugin/issues/841
329 329
 	 */
330
-	$label            = preg_replace( '/\xEF\xBB\xBF/', '', sanitize_text_field( $entity_data['label'] ) );
331
-	$type_uri         = $entity_data['main_type'];
332
-	$entity_types     = isset( $entity_data['type'] ) ? $entity_data['type'] : array();
333
-	$description      = $entity_data['description'];
334
-	$images           = isset( $entity_data['image'] ) ? (array) $entity_data['image'] : array();
335
-	$same_as          = isset( $entity_data['sameas'] ) ? (array) $entity_data['sameas'] : array();
336
-	$related_post_id  = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null;
337
-	$other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array();
338
-	// Get the synonyms.
339
-	$synonyms = isset( $entity_data['synonym'] ) ? $entity_data['synonym'] : array();
340
-
341
-	// Check whether an entity already exists with the provided URI.
342
-	$uri = $entity_data['uri'];
343
-	if ( isset( $uri ) ) {
344
-		$post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri );
345
-		if ( isset( $post ) ) {
346
-			$log->debug( "Post already exists for URI $uri." );
347
-
348
-			return $post;
349
-		}
350
-	}
351
-
352
-	// Prepare properties of the new entity.
353
-	// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
354
-	$post_status = apply_filters( 'wl_feature__enable__entity-auto-publish', true ) && is_numeric( $related_post_id )
355
-		? get_post_status( $related_post_id ) : 'draft';
356
-
357
-	$params = array(
358
-		// @@todo: we don't want an entity to be automatically published.
359
-		'post_status'  => $post_status,
360
-		'post_type'    => Wordlift_Entity_Service::TYPE_NAME,
361
-		'post_title'   => $label,
362
-		'post_content' => $description,
363
-		'post_excerpt' => '',
364
-		// Ensure we're using a valid slug. We're not overwriting an existing
365
-		// entity with a post_name already set, since this call is made only for
366
-		// new entities.
367
-		//
368
-		// See https://github.com/insideout10/wordlift-plugin/issues/282
369
-		'post_name'    => sanitize_title( $label ),
370
-	);
371
-
372
-	// If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over"
373
-	// the local SEO form values to the created entity (https://github.com/insideout10/wordlift-plugin/issues/156)
374
-	// Same thing applies to SEO Ultimate (https://github.com/insideout10/wordlift-plugin/issues/148)
375
-	// This does NOT affect saving an entity from the entity admin page since this function is called when an entity
376
-	// is created when saving a post.
377
-	global $wpseo_metabox, $seo_ultimate;
378
-	if ( isset( $wpseo_metabox ) ) {
379
-		remove_action(
380
-			'wp_insert_post',
381
-			array(
382
-				$wpseo_metabox,
383
-				'save_postdata',
384
-			)
385
-		);
386
-	}
387
-
388
-	if ( isset( $seo_ultimate ) ) {
389
-		remove_action(
390
-			'save_post',
391
-			array(
392
-				$seo_ultimate,
393
-				'save_postmeta_box',
394
-			)
395
-		);
396
-	}
397
-
398
-	// The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to
399
-	// save_post in order to save additional inputs from the edit page. In order to avoid issues, we pop all the hooks
400
-	// to the save_post and restore them after we saved the entity.
401
-	// see https://github.com/insideout10/wordlift-plugin/issues/203
402
-	// see https://github.com/insideout10/wordlift-plugin/issues/156
403
-	// see https://github.com/insideout10/wordlift-plugin/issues/148
404
-	global $wp_filter;
405
-	$save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks;
406
-	is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
407
-
408
-	$log->trace( 'Going to insert new post...' );
409
-
410
-	// create or update the post.
411
-	$post_id = wp_insert_post( $params, true );
412
-
413
-	// Setting the alternative labels for this entity.
414
-	Wordlift_Entity_Service::get_instance()
415
-						   ->set_alternative_labels( $post_id, $synonyms );
416
-
417
-	// Restore all the existing filters.
418
-	is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
419
-
420
-	// If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156)
421
-	if ( isset( $wpseo_metabox ) ) {
422
-		add_action(
423
-			'wp_insert_post',
424
-			array(
425
-				$wpseo_metabox,
426
-				'save_postdata',
427
-			)
428
-		);
429
-	}
430
-
431
-	// If SEO Ultimate is installed, add back the hook we removed a few lines above.
432
-	if ( isset( $seo_ultimate ) ) {
433
-		add_action(
434
-			'save_post',
435
-			array(
436
-				$seo_ultimate,
437
-				'save_postmeta_box',
438
-			),
439
-			10,
440
-			2
441
-		);
442
-	}
443
-
444
-	// TODO: handle errors.
445
-	if ( is_wp_error( $post_id ) ) {
446
-		$log->error( 'An error occurred: ' . $post_id->get_error_message() );
447
-
448
-		// inform an error occurred.
449
-		return null;
450
-	}
451
-
452
-	wl_set_entity_main_type( $post_id, $type_uri );
453
-
454
-	// Save the entity types.
455
-	wl_set_entity_rdf_types( $post_id, $entity_types );
456
-
457
-	// Get a dataset URI for the entity.
458
-	$wl_uri = Wordlift_Entity_Service::get_instance()->get_uri( $post_id );
459
-
460
-	// Add the uri to the sameAs data if it's not a local URI.
461
-	if ( isset( $uri ) && preg_match( '@https?://.*@', $uri ) &&
462
-		 $wl_uri !== $uri &&
463
-		 // Only remote entities
464
-		 0 !== strpos( $uri, Wordlift_Configuration_Service::get_instance()->get_dataset_uri() )
465
-	) {
466
-		array_push( $same_as, $uri );
467
-	}
468
-
469
-	// Save the sameAs data for the entity.
470
-	wl_schema_set_value( $post_id, 'sameAs', $same_as );
471
-
472
-	// Save the other properties (latitude, langitude, startDate, endDate, etc.)
473
-	foreach ( $other_properties as $property_name => $property_value ) {
474
-		wl_schema_set_value( $post_id, $property_name, $property_value );
475
-	}
476
-
477
-	// Call hooks.
478
-	do_action( 'wl_save_entity', $post_id );
479
-
480
-	foreach ( $images as $image_remote_url ) {
481
-
482
-		// Check if image is already present in local DB
483
-		if ( strpos( $image_remote_url, site_url() ) !== false ) {
484
-			// Do nothing.
485
-			continue;
486
-		}
487
-
488
-		// Check if there is an existing attachment for this post ID and source URL.
489
-		$existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url );
490
-
491
-		// Skip if an existing image is found.
492
-		if ( null !== $existing_image ) {
493
-			continue;
494
-		}
495
-
496
-		// Save the image and get the local path.
497
-		$image = Wordlift_Remote_Image_Service::save_from_url( $image_remote_url );
498
-
499
-		if ( false === $image || is_wp_error( $image ) ) {
500
-			continue;
501
-		}
502
-
503
-		// Get the local URL.
504
-		$filename     = $image['path'];
505
-		$url          = $image['url'];
506
-		$content_type = $image['content_type'];
507
-
508
-		$attachment = array(
509
-			'guid'           => $url,
510
-			// post_title, post_content (the value for this key should be the empty string), post_status and post_mime_type
511
-			'post_title'     => $label,
512
-			// Set the title to the post title.
513
-			'post_content'   => '',
514
-			'post_status'    => 'inherit',
515
-			'post_mime_type' => $content_type,
516
-		);
517
-
518
-		// Create the attachment in WordPress and generate the related metadata.
519
-		$attachment_id = wp_insert_attachment( $attachment, $filename, $post_id );
520
-
521
-		// Set the source URL for the image.
522
-		wl_set_source_url( $attachment_id, $image_remote_url );
523
-
524
-		$attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
525
-		wp_update_attachment_metadata( $attachment_id, $attachment_data );
526
-
527
-		// Set it as the featured image.
528
-		set_post_thumbnail( $post_id, $attachment_id );
529
-	}
530
-
531
-	// finally return the entity post.
532
-	return get_post( $post_id );
330
+    $label            = preg_replace( '/\xEF\xBB\xBF/', '', sanitize_text_field( $entity_data['label'] ) );
331
+    $type_uri         = $entity_data['main_type'];
332
+    $entity_types     = isset( $entity_data['type'] ) ? $entity_data['type'] : array();
333
+    $description      = $entity_data['description'];
334
+    $images           = isset( $entity_data['image'] ) ? (array) $entity_data['image'] : array();
335
+    $same_as          = isset( $entity_data['sameas'] ) ? (array) $entity_data['sameas'] : array();
336
+    $related_post_id  = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null;
337
+    $other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array();
338
+    // Get the synonyms.
339
+    $synonyms = isset( $entity_data['synonym'] ) ? $entity_data['synonym'] : array();
340
+
341
+    // Check whether an entity already exists with the provided URI.
342
+    $uri = $entity_data['uri'];
343
+    if ( isset( $uri ) ) {
344
+        $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri );
345
+        if ( isset( $post ) ) {
346
+            $log->debug( "Post already exists for URI $uri." );
347
+
348
+            return $post;
349
+        }
350
+    }
351
+
352
+    // Prepare properties of the new entity.
353
+    // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
354
+    $post_status = apply_filters( 'wl_feature__enable__entity-auto-publish', true ) && is_numeric( $related_post_id )
355
+        ? get_post_status( $related_post_id ) : 'draft';
356
+
357
+    $params = array(
358
+        // @@todo: we don't want an entity to be automatically published.
359
+        'post_status'  => $post_status,
360
+        'post_type'    => Wordlift_Entity_Service::TYPE_NAME,
361
+        'post_title'   => $label,
362
+        'post_content' => $description,
363
+        'post_excerpt' => '',
364
+        // Ensure we're using a valid slug. We're not overwriting an existing
365
+        // entity with a post_name already set, since this call is made only for
366
+        // new entities.
367
+        //
368
+        // See https://github.com/insideout10/wordlift-plugin/issues/282
369
+        'post_name'    => sanitize_title( $label ),
370
+    );
371
+
372
+    // If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over"
373
+    // the local SEO form values to the created entity (https://github.com/insideout10/wordlift-plugin/issues/156)
374
+    // Same thing applies to SEO Ultimate (https://github.com/insideout10/wordlift-plugin/issues/148)
375
+    // This does NOT affect saving an entity from the entity admin page since this function is called when an entity
376
+    // is created when saving a post.
377
+    global $wpseo_metabox, $seo_ultimate;
378
+    if ( isset( $wpseo_metabox ) ) {
379
+        remove_action(
380
+            'wp_insert_post',
381
+            array(
382
+                $wpseo_metabox,
383
+                'save_postdata',
384
+            )
385
+        );
386
+    }
387
+
388
+    if ( isset( $seo_ultimate ) ) {
389
+        remove_action(
390
+            'save_post',
391
+            array(
392
+                $seo_ultimate,
393
+                'save_postmeta_box',
394
+            )
395
+        );
396
+    }
397
+
398
+    // The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to
399
+    // save_post in order to save additional inputs from the edit page. In order to avoid issues, we pop all the hooks
400
+    // to the save_post and restore them after we saved the entity.
401
+    // see https://github.com/insideout10/wordlift-plugin/issues/203
402
+    // see https://github.com/insideout10/wordlift-plugin/issues/156
403
+    // see https://github.com/insideout10/wordlift-plugin/issues/148
404
+    global $wp_filter;
405
+    $save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks;
406
+    is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
407
+
408
+    $log->trace( 'Going to insert new post...' );
409
+
410
+    // create or update the post.
411
+    $post_id = wp_insert_post( $params, true );
412
+
413
+    // Setting the alternative labels for this entity.
414
+    Wordlift_Entity_Service::get_instance()
415
+                            ->set_alternative_labels( $post_id, $synonyms );
416
+
417
+    // Restore all the existing filters.
418
+    is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
419
+
420
+    // If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156)
421
+    if ( isset( $wpseo_metabox ) ) {
422
+        add_action(
423
+            'wp_insert_post',
424
+            array(
425
+                $wpseo_metabox,
426
+                'save_postdata',
427
+            )
428
+        );
429
+    }
430
+
431
+    // If SEO Ultimate is installed, add back the hook we removed a few lines above.
432
+    if ( isset( $seo_ultimate ) ) {
433
+        add_action(
434
+            'save_post',
435
+            array(
436
+                $seo_ultimate,
437
+                'save_postmeta_box',
438
+            ),
439
+            10,
440
+            2
441
+        );
442
+    }
443
+
444
+    // TODO: handle errors.
445
+    if ( is_wp_error( $post_id ) ) {
446
+        $log->error( 'An error occurred: ' . $post_id->get_error_message() );
447
+
448
+        // inform an error occurred.
449
+        return null;
450
+    }
451
+
452
+    wl_set_entity_main_type( $post_id, $type_uri );
453
+
454
+    // Save the entity types.
455
+    wl_set_entity_rdf_types( $post_id, $entity_types );
456
+
457
+    // Get a dataset URI for the entity.
458
+    $wl_uri = Wordlift_Entity_Service::get_instance()->get_uri( $post_id );
459
+
460
+    // Add the uri to the sameAs data if it's not a local URI.
461
+    if ( isset( $uri ) && preg_match( '@https?://.*@', $uri ) &&
462
+         $wl_uri !== $uri &&
463
+         // Only remote entities
464
+         0 !== strpos( $uri, Wordlift_Configuration_Service::get_instance()->get_dataset_uri() )
465
+    ) {
466
+        array_push( $same_as, $uri );
467
+    }
468
+
469
+    // Save the sameAs data for the entity.
470
+    wl_schema_set_value( $post_id, 'sameAs', $same_as );
471
+
472
+    // Save the other properties (latitude, langitude, startDate, endDate, etc.)
473
+    foreach ( $other_properties as $property_name => $property_value ) {
474
+        wl_schema_set_value( $post_id, $property_name, $property_value );
475
+    }
476
+
477
+    // Call hooks.
478
+    do_action( 'wl_save_entity', $post_id );
479
+
480
+    foreach ( $images as $image_remote_url ) {
481
+
482
+        // Check if image is already present in local DB
483
+        if ( strpos( $image_remote_url, site_url() ) !== false ) {
484
+            // Do nothing.
485
+            continue;
486
+        }
487
+
488
+        // Check if there is an existing attachment for this post ID and source URL.
489
+        $existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url );
490
+
491
+        // Skip if an existing image is found.
492
+        if ( null !== $existing_image ) {
493
+            continue;
494
+        }
495
+
496
+        // Save the image and get the local path.
497
+        $image = Wordlift_Remote_Image_Service::save_from_url( $image_remote_url );
498
+
499
+        if ( false === $image || is_wp_error( $image ) ) {
500
+            continue;
501
+        }
502
+
503
+        // Get the local URL.
504
+        $filename     = $image['path'];
505
+        $url          = $image['url'];
506
+        $content_type = $image['content_type'];
507
+
508
+        $attachment = array(
509
+            'guid'           => $url,
510
+            // post_title, post_content (the value for this key should be the empty string), post_status and post_mime_type
511
+            'post_title'     => $label,
512
+            // Set the title to the post title.
513
+            'post_content'   => '',
514
+            'post_status'    => 'inherit',
515
+            'post_mime_type' => $content_type,
516
+        );
517
+
518
+        // Create the attachment in WordPress and generate the related metadata.
519
+        $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id );
520
+
521
+        // Set the source URL for the image.
522
+        wl_set_source_url( $attachment_id, $image_remote_url );
523
+
524
+        $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
525
+        wp_update_attachment_metadata( $attachment_id, $attachment_data );
526
+
527
+        // Set it as the featured image.
528
+        set_post_thumbnail( $post_id, $attachment_id );
529
+    }
530
+
531
+    // finally return the entity post.
532
+    return get_post( $post_id );
533 533
 }
Please login to merge, or discard this patch.
Spacing   +125 added lines, -125 removed lines patch added patch discarded remove patch
@@ -21,15 +21,15 @@  discard block
 block discarded – undo
21 21
  *
22 22
  * @since 3.0.0
23 23
  */
24
-function wl_linked_data_save_post( $post_id ) {
24
+function wl_linked_data_save_post($post_id) {
25 25
 
26
-	$log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post' );
26
+	$log = Wordlift_Log_Service::get_logger('wl_linked_data_save_post');
27 27
 
28
-	$log->trace( "Saving post $post_id to Linked Data..." );
28
+	$log->trace("Saving post $post_id to Linked Data...");
29 29
 
30 30
 	// If it's not numeric exit from here.
31
-	if ( ! is_numeric( $post_id ) || is_numeric( wp_is_post_revision( $post_id ) ) ) {
32
-		$log->debug( "Skipping $post_id, because id is not numeric or is a post revision." );
31
+	if ( ! is_numeric($post_id) || is_numeric(wp_is_post_revision($post_id))) {
32
+		$log->debug("Skipping $post_id, because id is not numeric or is a post revision.");
33 33
 
34 34
 		return;
35 35
 	}
@@ -37,7 +37,7 @@  discard block
 block discarded – undo
37 37
 	// Get the post type and check whether it supports the editor.
38 38
 	//
39 39
 	// @see https://github.com/insideout10/wordlift-plugin/issues/659.
40
-	$post_type = get_post_type( $post_id );
40
+	$post_type = get_post_type($post_id);
41 41
 	/**
42 42
 	 * Use `wl_post_type_supports_editor` which calls a filter to allow 3rd parties to change the setting.
43 43
 	 *
@@ -45,13 +45,13 @@  discard block
 block discarded – undo
45 45
 	 *
46 46
 	 * @see https://github.com/insideout10/wordlift-plugin/issues/847.
47 47
 	 */
48
-	$is_editor_supported = wl_post_type_supports_editor( $post_type );
48
+	$is_editor_supported = wl_post_type_supports_editor($post_type);
49 49
 
50
-	$is_no_editor_analysis_enabled = Wordlift\No_Editor_Analysis\No_Editor_Analysis_Feature::can_no_editor_analysis_be_used( $post_id );
50
+	$is_no_editor_analysis_enabled = Wordlift\No_Editor_Analysis\No_Editor_Analysis_Feature::can_no_editor_analysis_be_used($post_id);
51 51
 	// Bail out if it's not an entity.
52 52
 	if ( ! $is_editor_supported
53
-		 && ! $is_no_editor_analysis_enabled ) {
54
-		$log->debug( "Skipping $post_id, because $post_type doesn't support the editor (content)." );
53
+		 && ! $is_no_editor_analysis_enabled) {
54
+		$log->debug("Skipping $post_id, because $post_type doesn't support the editor (content).");
55 55
 
56 56
 		return;
57 57
 	}
@@ -64,23 +64,23 @@  discard block
 block discarded – undo
64 64
 	$supported_types = Wordlift_Entity_Service::valid_entity_post_types();
65 65
 
66 66
 	// Bail out if it's not a valid entity.
67
-	if ( ! in_array( $post_type, $supported_types, true ) && ! $is_no_editor_analysis_enabled ) {
68
-		$log->debug( "Skipping $post_id, because $post_type is not a valid entity." );
67
+	if ( ! in_array($post_type, $supported_types, true) && ! $is_no_editor_analysis_enabled) {
68
+		$log->debug("Skipping $post_id, because $post_type is not a valid entity.");
69 69
 
70 70
 		return;
71 71
 	}
72 72
 
73 73
 	// Unhook this function so it doesn't loop infinitely.
74
-	remove_action( 'save_post', 'wl_linked_data_save_post' );
74
+	remove_action('save_post', 'wl_linked_data_save_post');
75 75
 
76 76
 	// raise the *wl_linked_data_save_post* event.
77
-	do_action( 'wl_linked_data_save_post', $post_id );
77
+	do_action('wl_linked_data_save_post', $post_id);
78 78
 
79 79
 	// Re-hook this function.
80
-	add_action( 'save_post', 'wl_linked_data_save_post' );
80
+	add_action('save_post', 'wl_linked_data_save_post');
81 81
 }
82 82
 
83
-add_action( 'save_post', 'wl_linked_data_save_post' );
83
+add_action('save_post', 'wl_linked_data_save_post');
84 84
 
85 85
 /**
86 86
  * Save the post to the triple store. Also saves the entities locally and on the triple store.
@@ -91,23 +91,23 @@  discard block
 block discarded – undo
91 91
  *
92 92
  * @since 3.0.0
93 93
  */
94
-function wl_linked_data_save_post_and_related_entities( $post_id ) {
94
+function wl_linked_data_save_post_and_related_entities($post_id) {
95 95
 
96
-	$log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post_and_related_entities' );
96
+	$log = Wordlift_Log_Service::get_logger('wl_linked_data_save_post_and_related_entities');
97 97
 
98
-	$log->trace( "Saving $post_id to Linked Data along with related entities..." );
98
+	$log->trace("Saving $post_id to Linked Data along with related entities...");
99 99
 
100 100
 	// Ignore auto-saves
101
-	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
102
-		$log->trace( 'Doing autosave, skipping...' );
101
+	if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
102
+		$log->trace('Doing autosave, skipping...');
103 103
 
104 104
 		return;
105 105
 	}
106 106
 
107 107
 	// get the current post.
108
-	$post = get_post( $post_id );
108
+	$post = get_post($post_id);
109 109
 
110
-	remove_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' );
110
+	remove_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities');
111 111
 
112 112
 	// wl_write_log( "[ post id :: $post_id ][ autosave :: false ][ post type :: $post->post_type ]" );
113 113
 
@@ -123,21 +123,21 @@  discard block
 block discarded – undo
123 123
 	$internal_entity_uris = array();
124 124
 
125 125
 	// Save the entities coming with POST data.
126
-	if ( isset( $_POST['wl_entities'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
127
-		$data              = filter_var_array( $_POST, array( 'wl_entities' => array( 'flags' => FILTER_REQUIRE_ARRAY ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
126
+	if (isset($_POST['wl_entities'])) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
127
+		$data              = filter_var_array($_POST, array('wl_entities' => array('flags' => FILTER_REQUIRE_ARRAY))); // phpcs:ignore WordPress.Security.NonceVerification.Missing
128 128
 		$entities_via_post = $data['wl_entities'];
129
-		wl_write_log( "[ post id :: $post_id ][ POST(wl_entities) :: " );
130
-		wl_write_log( wp_json_encode( $entities_via_post ) );
131
-		wl_write_log( ']' );
129
+		wl_write_log("[ post id :: $post_id ][ POST(wl_entities) :: ");
130
+		wl_write_log(wp_json_encode($entities_via_post));
131
+		wl_write_log(']');
132 132
 
133
-		foreach ( $entities_via_post as $entity_uri => $entity ) {
133
+		foreach ($entities_via_post as $entity_uri => $entity) {
134 134
 
135
-			if ( preg_match( '/^local-entity-.+/', $entity_uri ) ) {
136
-				$existing_entity = get_page_by_title( $entity['label'], OBJECT, Wordlift_Entity_Service::valid_entity_post_types() );
137
-				if ( isset( $existing_entity ) ) {
138
-					$existing_entity_type = Wordlift_Entity_Type_Service::get_instance()->get( $existing_entity->ID );
135
+			if (preg_match('/^local-entity-.+/', $entity_uri)) {
136
+				$existing_entity = get_page_by_title($entity['label'], OBJECT, Wordlift_Entity_Service::valid_entity_post_types());
137
+				if (isset($existing_entity)) {
138
+					$existing_entity_type = Wordlift_Entity_Type_Service::get_instance()->get($existing_entity->ID);
139 139
 					// Type doesn't match, continue to create a new entity.
140
-					if ( ! isset( $existing_entity_type ) || $existing_entity_type['css_class'] !== $entity['main_type'] ) {
140
+					if ( ! isset($existing_entity_type) || $existing_entity_type['css_class'] !== $entity['main_type']) {
141 141
 						$existing_entity = null;
142 142
 					}
143 143
 				}
@@ -145,35 +145,35 @@  discard block
 block discarded – undo
145 145
 				// Look if current entity uri matches an internal existing entity, meaning:
146 146
 				// 1. when $entity_uri is an internal uri
147 147
 				// 2. when $entity_uri is an external uri used as sameAs of an internal entity
148
-				$existing_entity = $entity_service->get_entity_post_by_uri( $entity_uri );
148
+				$existing_entity = $entity_service->get_entity_post_by_uri($entity_uri);
149 149
 			}
150 150
 
151 151
 			// Don't save the entities which are not found, but also local.
152
-			if ( ! isset( $existing_entity ) && $uri_service->is_internal( $entity_uri ) ) {
152
+			if ( ! isset($existing_entity) && $uri_service->is_internal($entity_uri)) {
153 153
 				$internal_entity_uris[] = $entity_uri;
154 154
 				continue;
155 155
 			}
156 156
 
157
-			if ( ! isset( $existing_entity ) ) {
157
+			if ( ! isset($existing_entity)) {
158 158
 				// Update entity data with related post
159 159
 				$entity['related_post_id'] = $post_id;
160 160
 				// New entity, save it.
161
-				$existing_entity = wl_save_entity( $entity );
161
+				$existing_entity = wl_save_entity($entity);
162 162
 			} else {
163 163
 				// Existing entity, update post status.
164
-				if ( $existing_entity instanceof WP_Post && 'publish' !== $existing_entity->post_status ) {
164
+				if ($existing_entity instanceof WP_Post && 'publish' !== $existing_entity->post_status) {
165 165
 					// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
166
-					$post_status = apply_filters( 'wl_feature__enable__entity-auto-publish', true )
166
+					$post_status = apply_filters('wl_feature__enable__entity-auto-publish', true)
167 167
 						? $post->post_status : 'draft';
168
-					wl_update_post_status( $existing_entity->ID, $post_status );
168
+					wl_update_post_status($existing_entity->ID, $post_status);
169 169
 				}
170 170
 			}
171 171
 
172
-			$uri = $content_service->get_entity_id( Wordpress_Content_Id::create_post( $existing_entity->ID ) );
172
+			$uri = $content_service->get_entity_id(Wordpress_Content_Id::create_post($existing_entity->ID));
173 173
 
174 174
 			$internal_entity_uris[] = $uri;
175
-			wl_write_log( "Map $entity_uri on $uri" );
176
-			$entities_uri_mapping[ $entity_uri ] = $uri;
175
+			wl_write_log("Map $entity_uri on $uri");
176
+			$entities_uri_mapping[$entity_uri] = $uri;
177 177
 
178 178
 		}
179 179
 	}
@@ -182,14 +182,14 @@  discard block
 block discarded – undo
182 182
 	$updated_post_content = $post->post_content;
183 183
 
184 184
 	// Update the post content if we found mappings of new entities.
185
-	if ( ! empty( $entities_uri_mapping ) ) {
185
+	if ( ! empty($entities_uri_mapping)) {
186 186
 		// Save each entity and store the post id.
187
-		foreach ( $entities_uri_mapping as $tmp_uri => $uri ) {
188
-			if ( 1 !== preg_match( '@^(https?://|local-entity-)@', $tmp_uri ) ) {
187
+		foreach ($entities_uri_mapping as $tmp_uri => $uri) {
188
+			if (1 !== preg_match('@^(https?://|local-entity-)@', $tmp_uri)) {
189 189
 				continue;
190 190
 			}
191 191
 
192
-			$updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content );
192
+			$updated_post_content = str_replace($tmp_uri, $uri, $updated_post_content);
193 193
 		}
194 194
 
195 195
 		// Update the post content.
@@ -201,19 +201,19 @@  discard block
 block discarded – undo
201 201
 		wp_update_post(
202 202
 			array(
203 203
 				'ID'           => $post->ID,
204
-				'post_content' => addslashes( $updated_post_content ),
204
+				'post_content' => addslashes($updated_post_content),
205 205
 			)
206 206
 		);
207 207
 	}
208 208
 
209 209
 	// Reset previously saved instances.
210
-	wl_core_delete_relation_instances( $post_id );
210
+	wl_core_delete_relation_instances($post_id);
211 211
 
212
-	$content_id = Wordpress_Content_Id::create_post( $post->ID );
213
-	$relations  = Relation_Service::get_instance()->get_relations( $content_id );
212
+	$content_id = Wordpress_Content_Id::create_post($post->ID);
213
+	$relations  = Relation_Service::get_instance()->get_relations($content_id);
214 214
 
215
-	if ( No_Editor_Analysis_Feature::can_no_editor_analysis_be_used( $post_id ) ) {
216
-		$relations->add( ...Relation_Service::get_relations_from_uris( $content_id, $internal_entity_uris ) );
215
+	if (No_Editor_Analysis_Feature::can_no_editor_analysis_be_used($post_id)) {
216
+		$relations->add(...Relation_Service::get_relations_from_uris($content_id, $internal_entity_uris));
217 217
 	}
218 218
 	/**
219 219
 	 * Filter the relations, we dont want to create a relation
@@ -223,20 +223,20 @@  discard block
 block discarded – undo
223 223
 	 */
224 224
 	$filtered_relations = array_filter(
225 225
 		$relations->toArray(),
226
-		function ( $item ) {
226
+		function($item) {
227 227
 			/**
228 228
 			 * @var $item Relation
229 229
 			 */
230 230
 			$object = $item->get_object();
231 231
 
232
-			return ! ( $object->get_type() === Object_Type_Enum::TERM
233
-			&& $object->get_id() === 1 );
232
+			return ! ($object->get_type() === Object_Type_Enum::TERM
233
+			&& $object->get_id() === 1);
234 234
 		}
235 235
 	);
236 236
 
237 237
 	// Save relation instances
238 238
 	/** @var Relation $relation */
239
-	foreach ( $filtered_relations  as $relation ) {
239
+	foreach ($filtered_relations  as $relation) {
240 240
 		$subject = $relation->get_subject();
241 241
 		$object  = $relation->get_object();
242 242
 
@@ -255,9 +255,9 @@  discard block
 block discarded – undo
255 255
 
256 256
 	}
257 257
 
258
-	if ( isset( $_POST['wl_entities'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
258
+	if (isset($_POST['wl_entities'])) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
259 259
 		// Save post metadata if available
260
-		$metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ? filter_input_array( INPUT_POST, array( 'wl_metadata' => FILTER_DEFAULT ) ) : array(); // phpcs:ignore WordPress.Security.NonceVerification.Missing
260
+		$metadata_via_post = (isset($_POST['wl_metadata'])) ? filter_input_array(INPUT_POST, array('wl_metadata' => FILTER_DEFAULT)) : array(); // phpcs:ignore WordPress.Security.NonceVerification.Missing
261 261
 
262 262
 		$fields = array(
263 263
 			Wordlift_Schema_Service::FIELD_LOCATION_CREATED,
@@ -265,35 +265,35 @@  discard block
 block discarded – undo
265 265
 		);
266 266
 
267 267
 		// Unlink topic taxonomy terms
268
-		Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for( $post->ID );
268
+		Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for($post->ID);
269 269
 
270
-		foreach ( $fields as $field ) {
270
+		foreach ($fields as $field) {
271 271
 
272 272
 			// Delete current values
273
-			delete_post_meta( $post->ID, $field );
273
+			delete_post_meta($post->ID, $field);
274 274
 			// Retrieve the entity uri
275
-			$uri = ( isset( $metadata_via_post[ $field ] ) ) ?
276
-				stripslashes( $metadata_via_post[ $field ] ) : '';
275
+			$uri = (isset($metadata_via_post[$field])) ?
276
+				stripslashes($metadata_via_post[$field]) : '';
277 277
 
278
-			if ( empty( $uri ) ) {
278
+			if (empty($uri)) {
279 279
 				continue;
280 280
 			}
281
-			$entity = $entity_service->get_entity_post_by_uri( $uri );
281
+			$entity = $entity_service->get_entity_post_by_uri($uri);
282 282
 
283
-			if ( $entity ) {
284
-				add_post_meta( $post->ID, $field, $entity->ID, true );
283
+			if ($entity) {
284
+				add_post_meta($post->ID, $field, $entity->ID, true);
285 285
 				// Set also the topic taxonomy
286
-				if ( Wordlift_Schema_Service::FIELD_TOPIC === $field ) {
287
-					Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for( $post->ID, $entity );
286
+				if (Wordlift_Schema_Service::FIELD_TOPIC === $field) {
287
+					Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for($post->ID, $entity);
288 288
 				}
289 289
 			}
290 290
 		}
291 291
 	}
292 292
 
293
-	add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' );
293
+	add_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities');
294 294
 }
295 295
 
296
-add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' );
296
+add_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities');
297 297
 
298 298
 /**
299 299
  * Save the specified data as an entity in WordPress. This method only create new entities. When an existing entity is
@@ -311,14 +311,14 @@  discard block
 block discarded – undo
311 311
  *
312 312
  * @return null|WP_Post A post instance or null in case of failure.
313 313
  */
314
-function wl_save_entity( $entity_data ) {
314
+function wl_save_entity($entity_data) {
315 315
 
316 316
 	// Required for REST API calls
317
-	if ( ! function_exists( 'wp_crop_image' ) ) {
318
-		require_once ABSPATH . 'wp-admin/includes/image.php';
317
+	if ( ! function_exists('wp_crop_image')) {
318
+		require_once ABSPATH.'wp-admin/includes/image.php';
319 319
 	}
320 320
 
321
-	$log = Wordlift_Log_Service::get_logger( 'wl_save_entity' );
321
+	$log = Wordlift_Log_Service::get_logger('wl_save_entity');
322 322
 
323 323
 	/*
324 324
 	 * Data is coming from a $_POST, sanitize it.
@@ -327,23 +327,23 @@  discard block
 block discarded – undo
327 327
 	 *
328 328
 	 * @see https://github.com/insideout10/wordlift-plugin/issues/841
329 329
 	 */
330
-	$label            = preg_replace( '/\xEF\xBB\xBF/', '', sanitize_text_field( $entity_data['label'] ) );
330
+	$label            = preg_replace('/\xEF\xBB\xBF/', '', sanitize_text_field($entity_data['label']));
331 331
 	$type_uri         = $entity_data['main_type'];
332
-	$entity_types     = isset( $entity_data['type'] ) ? $entity_data['type'] : array();
332
+	$entity_types     = isset($entity_data['type']) ? $entity_data['type'] : array();
333 333
 	$description      = $entity_data['description'];
334
-	$images           = isset( $entity_data['image'] ) ? (array) $entity_data['image'] : array();
335
-	$same_as          = isset( $entity_data['sameas'] ) ? (array) $entity_data['sameas'] : array();
336
-	$related_post_id  = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null;
337
-	$other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array();
334
+	$images           = isset($entity_data['image']) ? (array) $entity_data['image'] : array();
335
+	$same_as          = isset($entity_data['sameas']) ? (array) $entity_data['sameas'] : array();
336
+	$related_post_id  = isset($entity_data['related_post_id']) ? $entity_data['related_post_id'] : null;
337
+	$other_properties = isset($entity_data['properties']) ? $entity_data['properties'] : array();
338 338
 	// Get the synonyms.
339
-	$synonyms = isset( $entity_data['synonym'] ) ? $entity_data['synonym'] : array();
339
+	$synonyms = isset($entity_data['synonym']) ? $entity_data['synonym'] : array();
340 340
 
341 341
 	// Check whether an entity already exists with the provided URI.
342 342
 	$uri = $entity_data['uri'];
343
-	if ( isset( $uri ) ) {
344
-		$post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri );
345
-		if ( isset( $post ) ) {
346
-			$log->debug( "Post already exists for URI $uri." );
343
+	if (isset($uri)) {
344
+		$post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri($uri);
345
+		if (isset($post)) {
346
+			$log->debug("Post already exists for URI $uri.");
347 347
 
348 348
 			return $post;
349 349
 		}
@@ -351,8 +351,8 @@  discard block
 block discarded – undo
351 351
 
352 352
 	// Prepare properties of the new entity.
353 353
 	// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
354
-	$post_status = apply_filters( 'wl_feature__enable__entity-auto-publish', true ) && is_numeric( $related_post_id )
355
-		? get_post_status( $related_post_id ) : 'draft';
354
+	$post_status = apply_filters('wl_feature__enable__entity-auto-publish', true) && is_numeric($related_post_id)
355
+		? get_post_status($related_post_id) : 'draft';
356 356
 
357 357
 	$params = array(
358 358
 		// @@todo: we don't want an entity to be automatically published.
@@ -366,7 +366,7 @@  discard block
 block discarded – undo
366 366
 		// new entities.
367 367
 		//
368 368
 		// See https://github.com/insideout10/wordlift-plugin/issues/282
369
-		'post_name'    => sanitize_title( $label ),
369
+		'post_name'    => sanitize_title($label),
370 370
 	);
371 371
 
372 372
 	// If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over"
@@ -375,7 +375,7 @@  discard block
 block discarded – undo
375 375
 	// This does NOT affect saving an entity from the entity admin page since this function is called when an entity
376 376
 	// is created when saving a post.
377 377
 	global $wpseo_metabox, $seo_ultimate;
378
-	if ( isset( $wpseo_metabox ) ) {
378
+	if (isset($wpseo_metabox)) {
379 379
 		remove_action(
380 380
 			'wp_insert_post',
381 381
 			array(
@@ -385,7 +385,7 @@  discard block
 block discarded – undo
385 385
 		);
386 386
 	}
387 387
 
388
-	if ( isset( $seo_ultimate ) ) {
388
+	if (isset($seo_ultimate)) {
389 389
 		remove_action(
390 390
 			'save_post',
391 391
 			array(
@@ -402,23 +402,23 @@  discard block
 block discarded – undo
402 402
 	// see https://github.com/insideout10/wordlift-plugin/issues/156
403 403
 	// see https://github.com/insideout10/wordlift-plugin/issues/148
404 404
 	global $wp_filter;
405
-	$save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks;
406
-	is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
405
+	$save_post_filters = is_array($wp_filter['save_post']) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks;
406
+	is_array($wp_filter['save_post']) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
407 407
 
408
-	$log->trace( 'Going to insert new post...' );
408
+	$log->trace('Going to insert new post...');
409 409
 
410 410
 	// create or update the post.
411
-	$post_id = wp_insert_post( $params, true );
411
+	$post_id = wp_insert_post($params, true);
412 412
 
413 413
 	// Setting the alternative labels for this entity.
414 414
 	Wordlift_Entity_Service::get_instance()
415
-						   ->set_alternative_labels( $post_id, $synonyms );
415
+						   ->set_alternative_labels($post_id, $synonyms);
416 416
 
417 417
 	// Restore all the existing filters.
418
-	is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
418
+	is_array($wp_filter['save_post']) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
419 419
 
420 420
 	// If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156)
421
-	if ( isset( $wpseo_metabox ) ) {
421
+	if (isset($wpseo_metabox)) {
422 422
 		add_action(
423 423
 			'wp_insert_post',
424 424
 			array(
@@ -429,7 +429,7 @@  discard block
 block discarded – undo
429 429
 	}
430 430
 
431 431
 	// If SEO Ultimate is installed, add back the hook we removed a few lines above.
432
-	if ( isset( $seo_ultimate ) ) {
432
+	if (isset($seo_ultimate)) {
433 433
 		add_action(
434 434
 			'save_post',
435 435
 			array(
@@ -442,61 +442,61 @@  discard block
 block discarded – undo
442 442
 	}
443 443
 
444 444
 	// TODO: handle errors.
445
-	if ( is_wp_error( $post_id ) ) {
446
-		$log->error( 'An error occurred: ' . $post_id->get_error_message() );
445
+	if (is_wp_error($post_id)) {
446
+		$log->error('An error occurred: '.$post_id->get_error_message());
447 447
 
448 448
 		// inform an error occurred.
449 449
 		return null;
450 450
 	}
451 451
 
452
-	wl_set_entity_main_type( $post_id, $type_uri );
452
+	wl_set_entity_main_type($post_id, $type_uri);
453 453
 
454 454
 	// Save the entity types.
455
-	wl_set_entity_rdf_types( $post_id, $entity_types );
455
+	wl_set_entity_rdf_types($post_id, $entity_types);
456 456
 
457 457
 	// Get a dataset URI for the entity.
458
-	$wl_uri = Wordlift_Entity_Service::get_instance()->get_uri( $post_id );
458
+	$wl_uri = Wordlift_Entity_Service::get_instance()->get_uri($post_id);
459 459
 
460 460
 	// Add the uri to the sameAs data if it's not a local URI.
461
-	if ( isset( $uri ) && preg_match( '@https?://.*@', $uri ) &&
461
+	if (isset($uri) && preg_match('@https?://.*@', $uri) &&
462 462
 		 $wl_uri !== $uri &&
463 463
 		 // Only remote entities
464
-		 0 !== strpos( $uri, Wordlift_Configuration_Service::get_instance()->get_dataset_uri() )
464
+		 0 !== strpos($uri, Wordlift_Configuration_Service::get_instance()->get_dataset_uri())
465 465
 	) {
466
-		array_push( $same_as, $uri );
466
+		array_push($same_as, $uri);
467 467
 	}
468 468
 
469 469
 	// Save the sameAs data for the entity.
470
-	wl_schema_set_value( $post_id, 'sameAs', $same_as );
470
+	wl_schema_set_value($post_id, 'sameAs', $same_as);
471 471
 
472 472
 	// Save the other properties (latitude, langitude, startDate, endDate, etc.)
473
-	foreach ( $other_properties as $property_name => $property_value ) {
474
-		wl_schema_set_value( $post_id, $property_name, $property_value );
473
+	foreach ($other_properties as $property_name => $property_value) {
474
+		wl_schema_set_value($post_id, $property_name, $property_value);
475 475
 	}
476 476
 
477 477
 	// Call hooks.
478
-	do_action( 'wl_save_entity', $post_id );
478
+	do_action('wl_save_entity', $post_id);
479 479
 
480
-	foreach ( $images as $image_remote_url ) {
480
+	foreach ($images as $image_remote_url) {
481 481
 
482 482
 		// Check if image is already present in local DB
483
-		if ( strpos( $image_remote_url, site_url() ) !== false ) {
483
+		if (strpos($image_remote_url, site_url()) !== false) {
484 484
 			// Do nothing.
485 485
 			continue;
486 486
 		}
487 487
 
488 488
 		// Check if there is an existing attachment for this post ID and source URL.
489
-		$existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url );
489
+		$existing_image = wl_get_attachment_for_source_url($post_id, $image_remote_url);
490 490
 
491 491
 		// Skip if an existing image is found.
492
-		if ( null !== $existing_image ) {
492
+		if (null !== $existing_image) {
493 493
 			continue;
494 494
 		}
495 495
 
496 496
 		// Save the image and get the local path.
497
-		$image = Wordlift_Remote_Image_Service::save_from_url( $image_remote_url );
497
+		$image = Wordlift_Remote_Image_Service::save_from_url($image_remote_url);
498 498
 
499
-		if ( false === $image || is_wp_error( $image ) ) {
499
+		if (false === $image || is_wp_error($image)) {
500 500
 			continue;
501 501
 		}
502 502
 
@@ -516,18 +516,18 @@  discard block
 block discarded – undo
516 516
 		);
517 517
 
518 518
 		// Create the attachment in WordPress and generate the related metadata.
519
-		$attachment_id = wp_insert_attachment( $attachment, $filename, $post_id );
519
+		$attachment_id = wp_insert_attachment($attachment, $filename, $post_id);
520 520
 
521 521
 		// Set the source URL for the image.
522
-		wl_set_source_url( $attachment_id, $image_remote_url );
522
+		wl_set_source_url($attachment_id, $image_remote_url);
523 523
 
524
-		$attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
525
-		wp_update_attachment_metadata( $attachment_id, $attachment_data );
524
+		$attachment_data = wp_generate_attachment_metadata($attachment_id, $filename);
525
+		wp_update_attachment_metadata($attachment_id, $attachment_data);
526 526
 
527 527
 		// Set it as the featured image.
528
-		set_post_thumbnail( $post_id, $attachment_id );
528
+		set_post_thumbnail($post_id, $attachment_id);
529 529
 	}
530 530
 
531 531
 	// finally return the entity post.
532
-	return get_post( $post_id );
532
+	return get_post($post_id);
533 533
 }
Please login to merge, or discard this patch.