Completed
Push — develop ( 390b6b...e43d0b )
by David
02:34 queued 12s
created
src/wordlift/cache/class-ttl-cache.php 2 patches
Indentation   +140 added lines, -140 removed lines patch added patch discarded remove patch
@@ -17,150 +17,150 @@
 block discarded – undo
17 17
 // @@todo: add a hook to clear the cached files now and then.
18 18
 class Ttl_Cache {
19 19
 
20
-	/**
21
-	 * The cache name.
22
-	 *
23
-	 * @var string $name The cache name.
24
-	 * @access private
25
-	 * @since 3.21.2
26
-	 */
27
-	private $name;
28
-
29
-	/**
30
-	 * The TTL of cached responses in seconds.
31
-	 *
32
-	 * @var int $ttl The TTL in seconds.
33
-	 * @access private
34
-	 * @since 3.21.2
35
-	 */
36
-	private $ttl;
37
-
38
-	/**
39
-	 * The cache dir where the cached data is written.
40
-	 *
41
-	 * @since 3.21.2
42
-	 * @access private
43
-	 * @var string $cache_dir The cache dir where the cached responses are written.
44
-	 */
45
-	private $cache_dir;
46
-
47
-	/**
48
-	 * A {@link Wordlift_Log_Service} instance.
49
-	 *
50
-	 * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
51
-	 * @access private
52
-	 * @since 3.21.2
53
-	 */
54
-	private $log;
55
-
56
-	/**
57
-	 * @var array
58
-	 */
59
-	private static $caches = array();
60
-
61
-	/**
62
-	 * Create a {@link Ttl_Cache} with the specified TTL, default 900 secs.
63
-	 *
64
-	 * @param string $name The cache name.
65
-	 * @param int    $ttl The cache TTL, default 900 secs.
66
-	 *
67
-	 * @since 3.21.2
68
-	 */
69
-	public function __construct( $name, $ttl = 900 ) {
70
-
71
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
72
-
73
-		$this->name = $name;
74
-		$this->ttl  = $ttl;
75
-
76
-		// Get the temp dir and add the directory separator if missing.
77
-		$temp_dir = get_temp_dir();
78
-		if ( DIRECTORY_SEPARATOR !== substr( $temp_dir, - strlen( DIRECTORY_SEPARATOR ) ) ) {
79
-			$temp_dir .= DIRECTORY_SEPARATOR;
80
-		}
81
-		$this->cache_dir = $temp_dir . 'wl.cache' . DIRECTORY_SEPARATOR . md5( $name );
82
-
83
-		$this->log->trace( "Creating the cache folder {$this->cache_dir}..." );
84
-		wp_mkdir_p( $this->cache_dir );
85
-
86
-		self::$caches[ $name ] = $this;
87
-
88
-	}
89
-
90
-	/**
91
-	 * Get the cached data for the specified key.
92
-	 *
93
-	 * @param mixed $key A serializable key.
94
-	 *
95
-	 * @return mixed|null
96
-	 * @since 3.21.2
97
-	 */
98
-	public function get( $key ) {
99
-
100
-		$filename = $this->get_filename( $key );
101
-
102
-		// If the cache file exists and it's not too old, then return it.
103
-		if ( file_exists( $filename ) && $this->ttl >= time() - filemtime( $filename ) ) {
104
-			$this->log->trace( "Cache HIT.\n" );
105
-
106
-			return json_decode( file_get_contents( $filename ), true );
107
-		}
108
-
109
-		$this->log->trace( "Cache MISS, filename $filename.\n" );
110
-
111
-		return null;
112
-	}
113
-
114
-	public function put( $key, $data ) {
115
-
116
-		$filename = $this->get_filename( $key );
117
-
118
-		// Cache.
119
-		@unlink( $filename );
120
-		@file_put_contents( $filename, wp_json_encode( $data ) );
121
-
122
-	}
123
-
124
-	public function flush() {
125
-
126
-		$files = glob( $this->cache_dir . DIRECTORY_SEPARATOR . '*' );
127
-		foreach ( $files as $file ) { // iterate files
128
-			if ( is_file( $file ) ) {
129
-				@unlink( $file );
130
-			}
131
-		}
132
-
133
-	}
134
-
135
-	public static function flush_all() {
136
-
137
-		/** @var Ttl_Cache $cache */
138
-		foreach ( self::$caches as $cache ) {
139
-			$cache->flush();
140
-		}
141
-
142
-	}
20
+    /**
21
+     * The cache name.
22
+     *
23
+     * @var string $name The cache name.
24
+     * @access private
25
+     * @since 3.21.2
26
+     */
27
+    private $name;
28
+
29
+    /**
30
+     * The TTL of cached responses in seconds.
31
+     *
32
+     * @var int $ttl The TTL in seconds.
33
+     * @access private
34
+     * @since 3.21.2
35
+     */
36
+    private $ttl;
37
+
38
+    /**
39
+     * The cache dir where the cached data is written.
40
+     *
41
+     * @since 3.21.2
42
+     * @access private
43
+     * @var string $cache_dir The cache dir where the cached responses are written.
44
+     */
45
+    private $cache_dir;
46
+
47
+    /**
48
+     * A {@link Wordlift_Log_Service} instance.
49
+     *
50
+     * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
51
+     * @access private
52
+     * @since 3.21.2
53
+     */
54
+    private $log;
55
+
56
+    /**
57
+     * @var array
58
+     */
59
+    private static $caches = array();
60
+
61
+    /**
62
+     * Create a {@link Ttl_Cache} with the specified TTL, default 900 secs.
63
+     *
64
+     * @param string $name The cache name.
65
+     * @param int    $ttl The cache TTL, default 900 secs.
66
+     *
67
+     * @since 3.21.2
68
+     */
69
+    public function __construct( $name, $ttl = 900 ) {
70
+
71
+        $this->log = Wordlift_Log_Service::get_logger( get_class() );
72
+
73
+        $this->name = $name;
74
+        $this->ttl  = $ttl;
75
+
76
+        // Get the temp dir and add the directory separator if missing.
77
+        $temp_dir = get_temp_dir();
78
+        if ( DIRECTORY_SEPARATOR !== substr( $temp_dir, - strlen( DIRECTORY_SEPARATOR ) ) ) {
79
+            $temp_dir .= DIRECTORY_SEPARATOR;
80
+        }
81
+        $this->cache_dir = $temp_dir . 'wl.cache' . DIRECTORY_SEPARATOR . md5( $name );
82
+
83
+        $this->log->trace( "Creating the cache folder {$this->cache_dir}..." );
84
+        wp_mkdir_p( $this->cache_dir );
85
+
86
+        self::$caches[ $name ] = $this;
87
+
88
+    }
89
+
90
+    /**
91
+     * Get the cached data for the specified key.
92
+     *
93
+     * @param mixed $key A serializable key.
94
+     *
95
+     * @return mixed|null
96
+     * @since 3.21.2
97
+     */
98
+    public function get( $key ) {
99
+
100
+        $filename = $this->get_filename( $key );
101
+
102
+        // If the cache file exists and it's not too old, then return it.
103
+        if ( file_exists( $filename ) && $this->ttl >= time() - filemtime( $filename ) ) {
104
+            $this->log->trace( "Cache HIT.\n" );
105
+
106
+            return json_decode( file_get_contents( $filename ), true );
107
+        }
108
+
109
+        $this->log->trace( "Cache MISS, filename $filename.\n" );
110
+
111
+        return null;
112
+    }
113
+
114
+    public function put( $key, $data ) {
115
+
116
+        $filename = $this->get_filename( $key );
117
+
118
+        // Cache.
119
+        @unlink( $filename );
120
+        @file_put_contents( $filename, wp_json_encode( $data ) );
121
+
122
+    }
123
+
124
+    public function flush() {
125
+
126
+        $files = glob( $this->cache_dir . DIRECTORY_SEPARATOR . '*' );
127
+        foreach ( $files as $file ) { // iterate files
128
+            if ( is_file( $file ) ) {
129
+                @unlink( $file );
130
+            }
131
+        }
132
+
133
+    }
134
+
135
+    public static function flush_all() {
136
+
137
+        /** @var Ttl_Cache $cache */
138
+        foreach ( self::$caches as $cache ) {
139
+            $cache->flush();
140
+        }
141
+
142
+    }
143 143
 
144
-	/**
145
-	 * Get the full path for the given `$hash`. The file is not checked for its existence.
146
-	 *
147
-	 * @param string $hash A file hash.
148
-	 *
149
-	 * @return string The full path to the file.
150
-	 * @since 3.21.2
151
-	 */
152
-	private function get_path( $hash ) {
144
+    /**
145
+     * Get the full path for the given `$hash`. The file is not checked for its existence.
146
+     *
147
+     * @param string $hash A file hash.
148
+     *
149
+     * @return string The full path to the file.
150
+     * @since 3.21.2
151
+     */
152
+    private function get_path( $hash ) {
153 153
 
154
-		return $this->cache_dir . DIRECTORY_SEPARATOR . $hash;
155
-	}
154
+        return $this->cache_dir . DIRECTORY_SEPARATOR . $hash;
155
+    }
156 156
 
157
-	private function get_filename( $key ) {
157
+    private function get_filename( $key ) {
158 158
 
159
-		// Create a hash and a path to the cache file.
160
-		$hash     = md5( json_encode( $key ) );
161
-		$filename = $this->get_path( $hash );
159
+        // Create a hash and a path to the cache file.
160
+        $hash     = md5( json_encode( $key ) );
161
+        $filename = $this->get_path( $hash );
162 162
 
163
-		return $filename;
164
-	}
163
+        return $filename;
164
+    }
165 165
 
166 166
 }
Please login to merge, or discard this patch.
Spacing   +27 added lines, -27 removed lines patch added patch discarded remove patch
@@ -66,24 +66,24 @@  discard block
 block discarded – undo
66 66
 	 *
67 67
 	 * @since 3.21.2
68 68
 	 */
69
-	public function __construct( $name, $ttl = 900 ) {
69
+	public function __construct($name, $ttl = 900) {
70 70
 
71
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
71
+		$this->log = Wordlift_Log_Service::get_logger(get_class());
72 72
 
73 73
 		$this->name = $name;
74 74
 		$this->ttl  = $ttl;
75 75
 
76 76
 		// Get the temp dir and add the directory separator if missing.
77 77
 		$temp_dir = get_temp_dir();
78
-		if ( DIRECTORY_SEPARATOR !== substr( $temp_dir, - strlen( DIRECTORY_SEPARATOR ) ) ) {
78
+		if (DIRECTORY_SEPARATOR !== substr($temp_dir, - strlen(DIRECTORY_SEPARATOR))) {
79 79
 			$temp_dir .= DIRECTORY_SEPARATOR;
80 80
 		}
81
-		$this->cache_dir = $temp_dir . 'wl.cache' . DIRECTORY_SEPARATOR . md5( $name );
81
+		$this->cache_dir = $temp_dir.'wl.cache'.DIRECTORY_SEPARATOR.md5($name);
82 82
 
83
-		$this->log->trace( "Creating the cache folder {$this->cache_dir}..." );
84
-		wp_mkdir_p( $this->cache_dir );
83
+		$this->log->trace("Creating the cache folder {$this->cache_dir}...");
84
+		wp_mkdir_p($this->cache_dir);
85 85
 
86
-		self::$caches[ $name ] = $this;
86
+		self::$caches[$name] = $this;
87 87
 
88 88
 	}
89 89
 
@@ -95,38 +95,38 @@  discard block
 block discarded – undo
95 95
 	 * @return mixed|null
96 96
 	 * @since 3.21.2
97 97
 	 */
98
-	public function get( $key ) {
98
+	public function get($key) {
99 99
 
100
-		$filename = $this->get_filename( $key );
100
+		$filename = $this->get_filename($key);
101 101
 
102 102
 		// If the cache file exists and it's not too old, then return it.
103
-		if ( file_exists( $filename ) && $this->ttl >= time() - filemtime( $filename ) ) {
104
-			$this->log->trace( "Cache HIT.\n" );
103
+		if (file_exists($filename) && $this->ttl >= time() - filemtime($filename)) {
104
+			$this->log->trace("Cache HIT.\n");
105 105
 
106
-			return json_decode( file_get_contents( $filename ), true );
106
+			return json_decode(file_get_contents($filename), true);
107 107
 		}
108 108
 
109
-		$this->log->trace( "Cache MISS, filename $filename.\n" );
109
+		$this->log->trace("Cache MISS, filename $filename.\n");
110 110
 
111 111
 		return null;
112 112
 	}
113 113
 
114
-	public function put( $key, $data ) {
114
+	public function put($key, $data) {
115 115
 
116
-		$filename = $this->get_filename( $key );
116
+		$filename = $this->get_filename($key);
117 117
 
118 118
 		// Cache.
119
-		@unlink( $filename );
120
-		@file_put_contents( $filename, wp_json_encode( $data ) );
119
+		@unlink($filename);
120
+		@file_put_contents($filename, wp_json_encode($data));
121 121
 
122 122
 	}
123 123
 
124 124
 	public function flush() {
125 125
 
126
-		$files = glob( $this->cache_dir . DIRECTORY_SEPARATOR . '*' );
127
-		foreach ( $files as $file ) { // iterate files
128
-			if ( is_file( $file ) ) {
129
-				@unlink( $file );
126
+		$files = glob($this->cache_dir.DIRECTORY_SEPARATOR.'*');
127
+		foreach ($files as $file) { // iterate files
128
+			if (is_file($file)) {
129
+				@unlink($file);
130 130
 			}
131 131
 		}
132 132
 
@@ -135,7 +135,7 @@  discard block
 block discarded – undo
135 135
 	public static function flush_all() {
136 136
 
137 137
 		/** @var Ttl_Cache $cache */
138
-		foreach ( self::$caches as $cache ) {
138
+		foreach (self::$caches as $cache) {
139 139
 			$cache->flush();
140 140
 		}
141 141
 
@@ -149,16 +149,16 @@  discard block
 block discarded – undo
149 149
 	 * @return string The full path to the file.
150 150
 	 * @since 3.21.2
151 151
 	 */
152
-	private function get_path( $hash ) {
152
+	private function get_path($hash) {
153 153
 
154
-		return $this->cache_dir . DIRECTORY_SEPARATOR . $hash;
154
+		return $this->cache_dir.DIRECTORY_SEPARATOR.$hash;
155 155
 	}
156 156
 
157
-	private function get_filename( $key ) {
157
+	private function get_filename($key) {
158 158
 
159 159
 		// Create a hash and a path to the cache file.
160
-		$hash     = md5( json_encode( $key ) );
161
-		$filename = $this->get_path( $hash );
160
+		$hash     = md5(json_encode($key));
161
+		$filename = $this->get_path($hash);
162 162
 
163 163
 		return $filename;
164 164
 	}
Please login to merge, or discard this patch.
src/shortcodes/wordlift_shortcode_faceted_search.php 2 patches
Indentation   +355 added lines, -355 removed lines patch added patch discarded remove patch
@@ -18,113 +18,113 @@  discard block
 block discarded – undo
18 18
  */
19 19
 function wl_shortcode_faceted_search_data_ajax( $request_body = null ) {
20 20
 
21
-	// Post ID must be defined.
22
-	if ( ! isset( $_GET['post_id'] ) ) { // WPCS: input var ok; CSRF ok.
23
-		wp_die( 'No post_id given' );
24
-
25
-		return;
26
-	}
27
-
28
-	// Extract filtering conditions.
29
-	$filtering_entity_uris = json_decode( $request_body );
30
-
31
-	$current_post_id = $_GET['post_id']; // WPCS: input var ok; CSRF ok.
32
-	$current_post    = get_post( $current_post_id );
33
-
34
-	// Post ID has to match an existing item.
35
-	if ( null === $current_post ) {
36
-		wp_die( 'No valid post_id given' );
37
-
38
-		return;
39
-	}
40
-
41
-	// If the current post is an entity,
42
-	// the current post is used as main entity.
43
-	// Otherwise, current post related entities are used.
44
-	$entity_service = Wordlift_Entity_Service::get_instance();
45
-	$entity_ids     = $entity_service->is_entity( $current_post->ID ) ?
46
-		array( $current_post->ID ) :
47
-		wl_core_get_related_entity_ids( $current_post->ID );
48
-
49
-	// If there are no entities we cannot render the widget.
50
-	if ( 0 === count( $entity_ids ) ) {
51
-		wp_die( 'No entities available' );
52
-
53
-		return;
54
-	}
55
-
56
-	// Retrieve requested type
57
-	$required_type = ( isset( $_GET['type'] ) ) ? $_GET['type'] : null; // WPCS: input var ok; CSRF ok.
58
-
59
-	$limit = ( isset( $_GET['limit'] ) ) ? (int) $_GET['limit'] : 20;  // WPCS: input var ok; CSRF ok.
60
-
61
-	$referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects(
62
-		$entity_ids,
63
-		'*',
64
-		null,
65
-		'publish',
66
-		array( $current_post_id ),
67
-		$limit
68
-	);
69
-
70
-	$referencing_post_ids = array_map( function ( $p ) {
71
-		return $p->ID;
72
-	}, $referencing_posts );
73
-	$results              = array();
74
-
75
-	if ( 'posts' === $required_type ) {
76
-
77
-		// Required filtered posts.
78
-		wl_write_log( "Going to find related posts for the current post [ post ID :: $current_post_id ]" );
79
-
80
-		$filtered_posts = ( empty( $filtering_entity_uris ) ) ?
81
-			$referencing_posts :
82
-			Wordlift_Relation_Service::get_instance()->get_article_subjects(
83
-				wl_get_entity_post_ids_by_uris( $filtering_entity_uris ),
84
-				'*',
85
-				null,
86
-				null,
87
-				array(),
88
-				null,
89
-				$referencing_post_ids
90
-			);
91
-
92
-		if ( $filtered_posts ) {
93
-			foreach ( $filtered_posts as $post_obj ) {
94
-
95
-				/**
96
-				 * Use the thumbnail.
97
-				 *
98
-				 * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue.
99
-				 * @see https://github.com/insideout10/wordlift-plugin/issues/837
100
-				 *
101
-				 * @since 3.19.3 We're using the medium size image.
102
-				 */
103
-				$thumbnail           = get_the_post_thumbnail_url( $post_obj, 'medium' );
104
-				$post_obj->thumbnail = ( $thumbnail ) ?
105
-					$thumbnail : WL_DEFAULT_THUMBNAIL_PATH;
106
-				$post_obj->permalink = get_post_permalink( $post_obj->ID );
107
-
108
-				$results[] = $post_obj;
109
-			}
110
-		}
111
-	} else {
112
-
113
-		global $wpdb;
114
-
115
-		wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ]" );
116
-
117
-		// Retrieve Wordlift relation instances table name.
118
-		$table_name = wl_core_get_relation_instances_table_name();
119
-
120
-		/*
21
+    // Post ID must be defined.
22
+    if ( ! isset( $_GET['post_id'] ) ) { // WPCS: input var ok; CSRF ok.
23
+        wp_die( 'No post_id given' );
24
+
25
+        return;
26
+    }
27
+
28
+    // Extract filtering conditions.
29
+    $filtering_entity_uris = json_decode( $request_body );
30
+
31
+    $current_post_id = $_GET['post_id']; // WPCS: input var ok; CSRF ok.
32
+    $current_post    = get_post( $current_post_id );
33
+
34
+    // Post ID has to match an existing item.
35
+    if ( null === $current_post ) {
36
+        wp_die( 'No valid post_id given' );
37
+
38
+        return;
39
+    }
40
+
41
+    // If the current post is an entity,
42
+    // the current post is used as main entity.
43
+    // Otherwise, current post related entities are used.
44
+    $entity_service = Wordlift_Entity_Service::get_instance();
45
+    $entity_ids     = $entity_service->is_entity( $current_post->ID ) ?
46
+        array( $current_post->ID ) :
47
+        wl_core_get_related_entity_ids( $current_post->ID );
48
+
49
+    // If there are no entities we cannot render the widget.
50
+    if ( 0 === count( $entity_ids ) ) {
51
+        wp_die( 'No entities available' );
52
+
53
+        return;
54
+    }
55
+
56
+    // Retrieve requested type
57
+    $required_type = ( isset( $_GET['type'] ) ) ? $_GET['type'] : null; // WPCS: input var ok; CSRF ok.
58
+
59
+    $limit = ( isset( $_GET['limit'] ) ) ? (int) $_GET['limit'] : 20;  // WPCS: input var ok; CSRF ok.
60
+
61
+    $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects(
62
+        $entity_ids,
63
+        '*',
64
+        null,
65
+        'publish',
66
+        array( $current_post_id ),
67
+        $limit
68
+    );
69
+
70
+    $referencing_post_ids = array_map( function ( $p ) {
71
+        return $p->ID;
72
+    }, $referencing_posts );
73
+    $results              = array();
74
+
75
+    if ( 'posts' === $required_type ) {
76
+
77
+        // Required filtered posts.
78
+        wl_write_log( "Going to find related posts for the current post [ post ID :: $current_post_id ]" );
79
+
80
+        $filtered_posts = ( empty( $filtering_entity_uris ) ) ?
81
+            $referencing_posts :
82
+            Wordlift_Relation_Service::get_instance()->get_article_subjects(
83
+                wl_get_entity_post_ids_by_uris( $filtering_entity_uris ),
84
+                '*',
85
+                null,
86
+                null,
87
+                array(),
88
+                null,
89
+                $referencing_post_ids
90
+            );
91
+
92
+        if ( $filtered_posts ) {
93
+            foreach ( $filtered_posts as $post_obj ) {
94
+
95
+                /**
96
+                 * Use the thumbnail.
97
+                 *
98
+                 * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue.
99
+                 * @see https://github.com/insideout10/wordlift-plugin/issues/837
100
+                 *
101
+                 * @since 3.19.3 We're using the medium size image.
102
+                 */
103
+                $thumbnail           = get_the_post_thumbnail_url( $post_obj, 'medium' );
104
+                $post_obj->thumbnail = ( $thumbnail ) ?
105
+                    $thumbnail : WL_DEFAULT_THUMBNAIL_PATH;
106
+                $post_obj->permalink = get_post_permalink( $post_obj->ID );
107
+
108
+                $results[] = $post_obj;
109
+            }
110
+        }
111
+    } else {
112
+
113
+        global $wpdb;
114
+
115
+        wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ]" );
116
+
117
+        // Retrieve Wordlift relation instances table name.
118
+        $table_name = wl_core_get_relation_instances_table_name();
119
+
120
+        /*
121 121
 		 * Make sure we have some referenced post, otherwise the IN parts of
122 122
 		 * the SQL will produce an SQL error.
123 123
 		 */
124
-		if ( ! empty( $referencing_post_ids ) ) {
125
-			$subject_ids = implode( ',', $referencing_post_ids );
124
+        if ( ! empty( $referencing_post_ids ) ) {
125
+            $subject_ids = implode( ',', $referencing_post_ids );
126 126
 
127
-			$query = "
127
+            $query = "
128 128
 				SELECT
129 129
 					object_id AS ID,
130 130
 					count( object_id ) AS counter
@@ -136,30 +136,30 @@  discard block
 block discarded – undo
136 136
 				LIMIT $limit;
137 137
 			";
138 138
 
139
-			wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" );
139
+            wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" );
140 140
 
141
-			$entities = $wpdb->get_results( $query, OBJECT ); // No cache ok.
141
+            $entities = $wpdb->get_results( $query, OBJECT ); // No cache ok.
142 142
 
143
-			wl_write_log( 'Entities found ' . count( $entities ) );
143
+            wl_write_log( 'Entities found ' . count( $entities ) );
144 144
 
145
-			foreach ( $entities as $obj ) {
145
+            foreach ( $entities as $obj ) {
146 146
 
147
-				$entity = get_post( $obj->ID );
147
+                $entity = get_post( $obj->ID );
148 148
 
149
-				// Ensure only valid and published entities are returned.
150
-				if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) {
149
+                // Ensure only valid and published entities are returned.
150
+                if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) {
151 151
 
152
-					$serialized_entity              = wl_serialize_entity( $entity );
153
-					$serialized_entity['counter']   = $obj->counter;
154
-					$serialized_entity['createdAt'] = $entity->post_date;
152
+                    $serialized_entity              = wl_serialize_entity( $entity );
153
+                    $serialized_entity['counter']   = $obj->counter;
154
+                    $serialized_entity['createdAt'] = $entity->post_date;
155 155
 
156
-					$results[] = $serialized_entity;
157
-				}
158
-			}
159
-		}
160
-	}
156
+                    $results[] = $serialized_entity;
157
+                }
158
+            }
159
+        }
160
+    }
161 161
 
162
-	return $results;
162
+    return $results;
163 163
 
164 164
 }
165 165
 
@@ -171,148 +171,148 @@  discard block
 block discarded – undo
171 171
  */
172 172
 function wl_shortcode_faceted_search_data_wp_json( $http_raw_data = null ) {
173 173
 
174
-	// Post ID must be defined.
175
-	if ( ! isset( $_GET['post_id'] ) ) { // WPCS: input var ok; CSRF ok.
176
-		wp_die( 'No post_id given' );
177
-
178
-		return;
179
-	}
180
-
181
-	// Extract filtering conditions.
182
-	$filtering_entity_uris = ( null == $http_raw_data ) ? file_get_contents( 'php://input' ) : $http_raw_data;
183
-	$filtering_entity_uris = json_decode( $filtering_entity_uris );
184
-
185
-	$current_post_id = $_GET['post_id']; // WPCS: input var ok; CSRF ok.
186
-	$current_post    = get_post( $current_post_id );
187
-
188
-	// Post ID has to match an existing item.
189
-	if ( null === $current_post ) {
190
-		wp_die( 'No valid post_id given' );
191
-
192
-		return;
193
-	}
194
-
195
-	// If the current post is an entity,
196
-	// the current post is used as main entity.
197
-	// Otherwise, current post related entities are used.
198
-	$entity_service = Wordlift_Entity_Service::get_instance();
199
-	$entity_ids     = $entity_service->is_entity( $current_post->ID ) ?
200
-		array( $current_post->ID ) :
201
-		wl_core_get_related_entity_ids( $current_post->ID );
202
-
203
-	// If there are no entities we cannot render the widget.
204
-	if ( 0 === count( $entity_ids ) ) {
205
-		wp_die( 'No entities available' );
206
-
207
-		return;
208
-	}
209
-
210
-	// Retrieve requested type
211
-	$required_type = ( isset( $_GET['type'] ) ) ? $_GET['type'] : null; // WPCS: input var ok; CSRF ok.
212
-
213
-	$limit = ( isset( $_GET['limit'] ) ) ? (int) $_GET['limit'] : 20;  // WPCS: input var ok; CSRF ok.
214
-
215
-	$referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects(
216
-		$entity_ids,
217
-		'*',
218
-		null,
219
-		'publish',
220
-		array( $current_post_id ),
221
-		$limit
222
-	);
223
-
224
-	$referencing_post_ids = array_map( function ( $p ) {
225
-		return $p->ID;
226
-	}, $referencing_posts );
227
-	$results              = array();
228
-
229
-	if ( 'posts' === $required_type ) {
230
-
231
-		// Required filtered posts.
232
-		wl_write_log( "Going to find related posts for the current post [ post ID :: $current_post_id ]" );
233
-
234
-		$filtered_posts = ( empty( $filtering_entity_uris ) ) ?
235
-			$referencing_posts :
236
-			Wordlift_Relation_Service::get_instance()->get_article_subjects(
237
-				wl_get_entity_post_ids_by_uris( $filtering_entity_uris ),
238
-				'*',
239
-				null,
240
-				null,
241
-				array(),
242
-				null,
243
-				$referencing_post_ids
244
-			);
245
-
246
-		if ( $filtered_posts ) {
247
-			foreach ( $filtered_posts as $i => $post_obj ) {
248
-
249
-				/**
250
-				 * Use the thumbnail.
251
-				 *
252
-				 * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue.
253
-				 * @see https://github.com/insideout10/wordlift-plugin/issues/837
254
-				 *
255
-				 * @since 3.19.3 We're using the medium size image.
256
-				 */
257
-				$thumbnail           = get_the_post_thumbnail_url( $post_obj, 'medium' );
258
-				$post_obj->thumbnail = ( $thumbnail ) ?
259
-					$thumbnail : WL_DEFAULT_THUMBNAIL_PATH;
260
-				$post_obj->permalink = get_post_permalink( $post_obj->ID );
261
-
262
-				$results[ $i ] = $post_obj;
263
-
264
-				// Get Entity URLs needed for client side filtering in amp
265
-				foreach ( Wordlift_Relation_Service::get_instance()->get_objects( $post_obj->ID, 'ids' ) as $entity_id ) {
266
-					$results[ $i ]->entities[] = Wordlift_Entity_Service::get_instance()->get_uri( $entity_id );
267
-				}
268
-			}
269
-		}
270
-
271
-		return array(
272
-			array( 'values' => $results ),
273
-		);
274
-
275
-	} else {
276
-
277
-		global $wpdb;
278
-
279
-		wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ]" );
280
-
281
-		// Retrieve Wordlift relation instances table name.
282
-		$table_name = wl_core_get_relation_instances_table_name();
283
-
284
-		// Response interface with l10n strings, grouped entities and empty data array
285
-		$serialized_entity_groups = array(
286
-			array(
287
-				'l10n'     => $_GET['l10n'] ? $_GET['l10n']['what'] : 'what',
288
-				'entities' => array( 'thing', 'creative-work', 'recipe' ),
289
-				'data'     => array(),
290
-			),
291
-			array(
292
-				'l10n'     => $_GET['l10n'] ? $_GET['l10n']['who'] : 'who',
293
-				'entities' => array( 'person', 'organization', 'local-business' ),
294
-				'data'     => array(),
295
-			),
296
-			array(
297
-				'l10n'     => $_GET['l10n'] ? $_GET['l10n']['where'] : 'where',
298
-				'entities' => array( 'place' ),
299
-				'data'     => array(),
300
-			),
301
-			array(
302
-				'l10n'     => $_GET['l10n'] ? $_GET['l10n']['when'] : 'when',
303
-				'entities' => array( 'event' ),
304
-				'data'     => array(),
305
-			),
306
-		);
307
-
308
-		/*
174
+    // Post ID must be defined.
175
+    if ( ! isset( $_GET['post_id'] ) ) { // WPCS: input var ok; CSRF ok.
176
+        wp_die( 'No post_id given' );
177
+
178
+        return;
179
+    }
180
+
181
+    // Extract filtering conditions.
182
+    $filtering_entity_uris = ( null == $http_raw_data ) ? file_get_contents( 'php://input' ) : $http_raw_data;
183
+    $filtering_entity_uris = json_decode( $filtering_entity_uris );
184
+
185
+    $current_post_id = $_GET['post_id']; // WPCS: input var ok; CSRF ok.
186
+    $current_post    = get_post( $current_post_id );
187
+
188
+    // Post ID has to match an existing item.
189
+    if ( null === $current_post ) {
190
+        wp_die( 'No valid post_id given' );
191
+
192
+        return;
193
+    }
194
+
195
+    // If the current post is an entity,
196
+    // the current post is used as main entity.
197
+    // Otherwise, current post related entities are used.
198
+    $entity_service = Wordlift_Entity_Service::get_instance();
199
+    $entity_ids     = $entity_service->is_entity( $current_post->ID ) ?
200
+        array( $current_post->ID ) :
201
+        wl_core_get_related_entity_ids( $current_post->ID );
202
+
203
+    // If there are no entities we cannot render the widget.
204
+    if ( 0 === count( $entity_ids ) ) {
205
+        wp_die( 'No entities available' );
206
+
207
+        return;
208
+    }
209
+
210
+    // Retrieve requested type
211
+    $required_type = ( isset( $_GET['type'] ) ) ? $_GET['type'] : null; // WPCS: input var ok; CSRF ok.
212
+
213
+    $limit = ( isset( $_GET['limit'] ) ) ? (int) $_GET['limit'] : 20;  // WPCS: input var ok; CSRF ok.
214
+
215
+    $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects(
216
+        $entity_ids,
217
+        '*',
218
+        null,
219
+        'publish',
220
+        array( $current_post_id ),
221
+        $limit
222
+    );
223
+
224
+    $referencing_post_ids = array_map( function ( $p ) {
225
+        return $p->ID;
226
+    }, $referencing_posts );
227
+    $results              = array();
228
+
229
+    if ( 'posts' === $required_type ) {
230
+
231
+        // Required filtered posts.
232
+        wl_write_log( "Going to find related posts for the current post [ post ID :: $current_post_id ]" );
233
+
234
+        $filtered_posts = ( empty( $filtering_entity_uris ) ) ?
235
+            $referencing_posts :
236
+            Wordlift_Relation_Service::get_instance()->get_article_subjects(
237
+                wl_get_entity_post_ids_by_uris( $filtering_entity_uris ),
238
+                '*',
239
+                null,
240
+                null,
241
+                array(),
242
+                null,
243
+                $referencing_post_ids
244
+            );
245
+
246
+        if ( $filtered_posts ) {
247
+            foreach ( $filtered_posts as $i => $post_obj ) {
248
+
249
+                /**
250
+                 * Use the thumbnail.
251
+                 *
252
+                 * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue.
253
+                 * @see https://github.com/insideout10/wordlift-plugin/issues/837
254
+                 *
255
+                 * @since 3.19.3 We're using the medium size image.
256
+                 */
257
+                $thumbnail           = get_the_post_thumbnail_url( $post_obj, 'medium' );
258
+                $post_obj->thumbnail = ( $thumbnail ) ?
259
+                    $thumbnail : WL_DEFAULT_THUMBNAIL_PATH;
260
+                $post_obj->permalink = get_post_permalink( $post_obj->ID );
261
+
262
+                $results[ $i ] = $post_obj;
263
+
264
+                // Get Entity URLs needed for client side filtering in amp
265
+                foreach ( Wordlift_Relation_Service::get_instance()->get_objects( $post_obj->ID, 'ids' ) as $entity_id ) {
266
+                    $results[ $i ]->entities[] = Wordlift_Entity_Service::get_instance()->get_uri( $entity_id );
267
+                }
268
+            }
269
+        }
270
+
271
+        return array(
272
+            array( 'values' => $results ),
273
+        );
274
+
275
+    } else {
276
+
277
+        global $wpdb;
278
+
279
+        wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ]" );
280
+
281
+        // Retrieve Wordlift relation instances table name.
282
+        $table_name = wl_core_get_relation_instances_table_name();
283
+
284
+        // Response interface with l10n strings, grouped entities and empty data array
285
+        $serialized_entity_groups = array(
286
+            array(
287
+                'l10n'     => $_GET['l10n'] ? $_GET['l10n']['what'] : 'what',
288
+                'entities' => array( 'thing', 'creative-work', 'recipe' ),
289
+                'data'     => array(),
290
+            ),
291
+            array(
292
+                'l10n'     => $_GET['l10n'] ? $_GET['l10n']['who'] : 'who',
293
+                'entities' => array( 'person', 'organization', 'local-business' ),
294
+                'data'     => array(),
295
+            ),
296
+            array(
297
+                'l10n'     => $_GET['l10n'] ? $_GET['l10n']['where'] : 'where',
298
+                'entities' => array( 'place' ),
299
+                'data'     => array(),
300
+            ),
301
+            array(
302
+                'l10n'     => $_GET['l10n'] ? $_GET['l10n']['when'] : 'when',
303
+                'entities' => array( 'event' ),
304
+                'data'     => array(),
305
+            ),
306
+        );
307
+
308
+        /*
309 309
 		 * Make sure we have some referenced post, otherwise the IN parts of
310 310
 		 * the SQL will produce an SQL error.
311 311
 		 */
312
-		if ( ! empty( $referencing_post_ids ) ) {
313
-			$subject_ids = implode( ',', $referencing_post_ids );
312
+        if ( ! empty( $referencing_post_ids ) ) {
313
+            $subject_ids = implode( ',', $referencing_post_ids );
314 314
 
315
-			$query = "
315
+            $query = "
316 316
 				SELECT
317 317
 					object_id AS ID,
318 318
 					count( object_id ) AS counter
@@ -324,45 +324,45 @@  discard block
 block discarded – undo
324 324
 				LIMIT $limit;
325 325
 			";
326 326
 
327
-			wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" );
327
+            wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" );
328 328
 
329
-			$entities = $wpdb->get_results( $query, OBJECT ); // No cache ok.
329
+            $entities = $wpdb->get_results( $query, OBJECT ); // No cache ok.
330 330
 
331
-			wl_write_log( 'Entities found ' . count( $entities ) );
331
+            wl_write_log( 'Entities found ' . count( $entities ) );
332 332
 
333
-			foreach ( $entities as $obj ) {
333
+            foreach ( $entities as $obj ) {
334 334
 
335
-				$entity = get_post( $obj->ID );
335
+                $entity = get_post( $obj->ID );
336 336
 
337
-				// Ensure only valid and published entities are returned.
338
-				if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) {
337
+                // Ensure only valid and published entities are returned.
338
+                if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) {
339 339
 
340
-					$serialized_entity              = wl_serialize_entity( $entity );
341
-					$serialized_entity['counter']   = $obj->counter;
342
-					$serialized_entity['createdAt'] = $entity->post_date;
340
+                    $serialized_entity              = wl_serialize_entity( $entity );
341
+                    $serialized_entity['counter']   = $obj->counter;
342
+                    $serialized_entity['createdAt'] = $entity->post_date;
343 343
 
344
-					// Populate $serialized_entity_groups maintaining array sequence
345
-					foreach ( $serialized_entity_groups as $seg_key => $seg_value ) {
346
-						if ( in_array( $serialized_entity['mainType'], $seg_value['entities'] ) ) {
347
-							$serialized_entity_groups[ $seg_key ]['data'][] = $serialized_entity;
348
-						}
349
-					}
350
-				}
351
-			}
344
+                    // Populate $serialized_entity_groups maintaining array sequence
345
+                    foreach ( $serialized_entity_groups as $seg_key => $seg_value ) {
346
+                        if ( in_array( $serialized_entity['mainType'], $seg_value['entities'] ) ) {
347
+                            $serialized_entity_groups[ $seg_key ]['data'][] = $serialized_entity;
348
+                        }
349
+                    }
350
+                }
351
+            }
352 352
 
353
-			// Clean-up $serialized_entity_groups by removing empty items and entities
354
-			foreach ( $serialized_entity_groups as $seg_key => $seg_value ) {
355
-				if ( empty( $seg_value['data'] ) ) {
356
-					unset( $serialized_entity_groups[ $seg_key ] );
357
-				}
358
-				unset( $serialized_entity_groups[ $seg_key ]['entities'] );
359
-			}
353
+            // Clean-up $serialized_entity_groups by removing empty items and entities
354
+            foreach ( $serialized_entity_groups as $seg_key => $seg_value ) {
355
+                if ( empty( $seg_value['data'] ) ) {
356
+                    unset( $serialized_entity_groups[ $seg_key ] );
357
+                }
358
+                unset( $serialized_entity_groups[ $seg_key ]['entities'] );
359
+            }
360 360
 
361
-			$results = $serialized_entity_groups;
362
-		}
361
+            $results = $serialized_entity_groups;
362
+        }
363 363
 
364
-		return $results;
365
-	}
364
+        return $results;
365
+    }
366 366
 
367 367
 }
368 368
 
@@ -374,34 +374,34 @@  discard block
 block discarded – undo
374 374
  */
375 375
 function wl_shortcode_faceted_search_ajax( $http_raw_data = null ) {
376 376
 
377
-	// Get the request body. It may contain posts to filter the results.
378
-	$request_body = ( empty( $http_raw_data ) ? file_get_contents( 'php://input' ) : $http_raw_data );
377
+    // Get the request body. It may contain posts to filter the results.
378
+    $request_body = ( empty( $http_raw_data ) ? file_get_contents( 'php://input' ) : $http_raw_data );
379 379
 
380
-	// Create the cache key.
381
-	$cache_key = array(
382
-		'request_body'   => $request_body,
383
-		'request_params' => $_REQUEST,
384
-	);
380
+    // Create the cache key.
381
+    $cache_key = array(
382
+        'request_body'   => $request_body,
383
+        'request_params' => $_REQUEST,
384
+    );
385 385
 
386
-	// Create the TTL cache and try to get the results.
387
-	$cache         = new Ttl_Cache( "faceted-search", 8 * 60 * 60 ); // 8 hours.
388
-	$cache_results = $cache->get( $cache_key );
386
+    // Create the TTL cache and try to get the results.
387
+    $cache         = new Ttl_Cache( "faceted-search", 8 * 60 * 60 ); // 8 hours.
388
+    $cache_results = $cache->get( $cache_key );
389 389
 
390
-	if ( isset( $cache_results ) ) {
391
-		header( 'X-WordLift-Cache: HIT' );
392
-		wl_core_send_json( $cache_results );
390
+    if ( isset( $cache_results ) ) {
391
+        header( 'X-WordLift-Cache: HIT' );
392
+        wl_core_send_json( $cache_results );
393 393
 
394
-		return;
395
-	}
394
+        return;
395
+    }
396 396
 
397
-	header( 'X-WordLift-Cache: MISS' );
397
+    header( 'X-WordLift-Cache: MISS' );
398 398
 
399
-	$results = wl_shortcode_faceted_search_data_ajax( $request_body );
399
+    $results = wl_shortcode_faceted_search_data_ajax( $request_body );
400 400
 
401
-	// Put the result before sending the json to the client, since sending the json will terminate us.
402
-	$cache->put( $cache_key, $results );
401
+    // Put the result before sending the json to the client, since sending the json will terminate us.
402
+    $cache->put( $cache_key, $results );
403 403
 
404
-	wl_core_send_json( $results );
404
+    wl_core_send_json( $results );
405 405
 
406 406
 }
407 407
 
@@ -416,14 +416,14 @@  discard block
 block discarded – undo
416 416
  */
417 417
 function wl_shortcode_faceted_search_wp_json( $http_raw_data = null ) {
418 418
 
419
-	$results = wl_shortcode_faceted_search_data_wp_json( $http_raw_data );
420
-	if ( ob_get_contents() ) {
421
-		ob_clean();
422
-	}
419
+    $results = wl_shortcode_faceted_search_data_wp_json( $http_raw_data );
420
+    if ( ob_get_contents() ) {
421
+        ob_clean();
422
+    }
423 423
 
424
-	return array(
425
-		'items' => $results,
426
-	);
424
+    return array(
425
+        'items' => $results,
426
+    );
427 427
 
428 428
 }
429 429
 
@@ -431,52 +431,52 @@  discard block
 block discarded – undo
431 431
  * Adding `rest_api_init` action for amp backend of faceted-search
432 432
  */
433 433
 add_action( 'rest_api_init', function () {
434
-	register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search', array(
435
-		'methods'  => 'GET',
436
-		'callback' => 'wl_shortcode_faceted_search_wp_json',
437
-	) );
434
+    register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search', array(
435
+        'methods'  => 'GET',
436
+        'callback' => 'wl_shortcode_faceted_search_wp_json',
437
+    ) );
438 438
 } );
439 439
 
440 440
 /**
441 441
  * register_block_type for Gutenberg blocks
442 442
  */
443 443
 add_action( 'init', function () {
444
-	// Bail out if the `register_block_type` function isn't available.
445
-	if ( ! function_exists( 'register_block_type' ) ) {
446
-		return;
447
-	}
448
-
449
-	register_block_type( 'wordlift/faceted-search', array(
450
-		'editor_script'   => 'wordlift-admin-edit-gutenberg',
451
-		'render_callback' => function ( $attributes ) {
452
-			$attr_code = '';
453
-			foreach ( $attributes as $key => $value ) {
454
-				$attr_code .= $key . '="' . $value . '" ';
455
-			}
456
-
457
-			return '[wl_faceted_search ' . $attr_code . ']';
458
-		},
459
-		'attributes'      => array(
460
-			'title'          => array(
461
-				'type'    => 'string',
462
-				'default' => __( 'Related articles', 'wordlift' ),
463
-			),
464
-			'show_facets'    => array(
465
-				'type'    => 'bool',
466
-				'default' => true,
467
-			),
468
-			'with_carousel'  => array(
469
-				'type'    => 'bool',
470
-				'default' => true,
471
-			),
472
-			'squared_thumbs' => array(
473
-				'type'    => 'bool',
474
-				'default' => false,
475
-			),
476
-			'limit'          => array(
477
-				'type'    => 'number',
478
-				'default' => 20,
479
-			),
480
-		),
481
-	) );
444
+    // Bail out if the `register_block_type` function isn't available.
445
+    if ( ! function_exists( 'register_block_type' ) ) {
446
+        return;
447
+    }
448
+
449
+    register_block_type( 'wordlift/faceted-search', array(
450
+        'editor_script'   => 'wordlift-admin-edit-gutenberg',
451
+        'render_callback' => function ( $attributes ) {
452
+            $attr_code = '';
453
+            foreach ( $attributes as $key => $value ) {
454
+                $attr_code .= $key . '="' . $value . '" ';
455
+            }
456
+
457
+            return '[wl_faceted_search ' . $attr_code . ']';
458
+        },
459
+        'attributes'      => array(
460
+            'title'          => array(
461
+                'type'    => 'string',
462
+                'default' => __( 'Related articles', 'wordlift' ),
463
+            ),
464
+            'show_facets'    => array(
465
+                'type'    => 'bool',
466
+                'default' => true,
467
+            ),
468
+            'with_carousel'  => array(
469
+                'type'    => 'bool',
470
+                'default' => true,
471
+            ),
472
+            'squared_thumbs' => array(
473
+                'type'    => 'bool',
474
+                'default' => false,
475
+            ),
476
+            'limit'          => array(
477
+                'type'    => 'number',
478
+                'default' => 20,
479
+            ),
480
+        ),
481
+    ) );
482 482
 } );
Please login to merge, or discard this patch.
Spacing   +116 added lines, -120 removed lines patch added patch discarded remove patch
@@ -16,24 +16,24 @@  discard block
 block discarded – undo
16 16
  * @return array $results
17 17
  * @since        3.20.0
18 18
  */
19
-function wl_shortcode_faceted_search_data_ajax( $request_body = null ) {
19
+function wl_shortcode_faceted_search_data_ajax($request_body = null) {
20 20
 
21 21
 	// Post ID must be defined.
22
-	if ( ! isset( $_GET['post_id'] ) ) { // WPCS: input var ok; CSRF ok.
23
-		wp_die( 'No post_id given' );
22
+	if ( ! isset($_GET['post_id'])) { // WPCS: input var ok; CSRF ok.
23
+		wp_die('No post_id given');
24 24
 
25 25
 		return;
26 26
 	}
27 27
 
28 28
 	// Extract filtering conditions.
29
-	$filtering_entity_uris = json_decode( $request_body );
29
+	$filtering_entity_uris = json_decode($request_body);
30 30
 
31 31
 	$current_post_id = $_GET['post_id']; // WPCS: input var ok; CSRF ok.
32
-	$current_post    = get_post( $current_post_id );
32
+	$current_post    = get_post($current_post_id);
33 33
 
34 34
 	// Post ID has to match an existing item.
35
-	if ( null === $current_post ) {
36
-		wp_die( 'No valid post_id given' );
35
+	if (null === $current_post) {
36
+		wp_die('No valid post_id given');
37 37
 
38 38
 		return;
39 39
 	}
@@ -42,45 +42,43 @@  discard block
 block discarded – undo
42 42
 	// the current post is used as main entity.
43 43
 	// Otherwise, current post related entities are used.
44 44
 	$entity_service = Wordlift_Entity_Service::get_instance();
45
-	$entity_ids     = $entity_service->is_entity( $current_post->ID ) ?
46
-		array( $current_post->ID ) :
47
-		wl_core_get_related_entity_ids( $current_post->ID );
45
+	$entity_ids     = $entity_service->is_entity($current_post->ID) ?
46
+		array($current_post->ID) : wl_core_get_related_entity_ids($current_post->ID);
48 47
 
49 48
 	// If there are no entities we cannot render the widget.
50
-	if ( 0 === count( $entity_ids ) ) {
51
-		wp_die( 'No entities available' );
49
+	if (0 === count($entity_ids)) {
50
+		wp_die('No entities available');
52 51
 
53 52
 		return;
54 53
 	}
55 54
 
56 55
 	// Retrieve requested type
57
-	$required_type = ( isset( $_GET['type'] ) ) ? $_GET['type'] : null; // WPCS: input var ok; CSRF ok.
56
+	$required_type = (isset($_GET['type'])) ? $_GET['type'] : null; // WPCS: input var ok; CSRF ok.
58 57
 
59
-	$limit = ( isset( $_GET['limit'] ) ) ? (int) $_GET['limit'] : 20;  // WPCS: input var ok; CSRF ok.
58
+	$limit = (isset($_GET['limit'])) ? (int) $_GET['limit'] : 20; // WPCS: input var ok; CSRF ok.
60 59
 
61 60
 	$referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects(
62 61
 		$entity_ids,
63 62
 		'*',
64 63
 		null,
65 64
 		'publish',
66
-		array( $current_post_id ),
65
+		array($current_post_id),
67 66
 		$limit
68 67
 	);
69 68
 
70
-	$referencing_post_ids = array_map( function ( $p ) {
69
+	$referencing_post_ids = array_map(function($p) {
71 70
 		return $p->ID;
72
-	}, $referencing_posts );
71
+	}, $referencing_posts);
73 72
 	$results              = array();
74 73
 
75
-	if ( 'posts' === $required_type ) {
74
+	if ('posts' === $required_type) {
76 75
 
77 76
 		// Required filtered posts.
78
-		wl_write_log( "Going to find related posts for the current post [ post ID :: $current_post_id ]" );
77
+		wl_write_log("Going to find related posts for the current post [ post ID :: $current_post_id ]");
79 78
 
80
-		$filtered_posts = ( empty( $filtering_entity_uris ) ) ?
81
-			$referencing_posts :
82
-			Wordlift_Relation_Service::get_instance()->get_article_subjects(
83
-				wl_get_entity_post_ids_by_uris( $filtering_entity_uris ),
79
+		$filtered_posts = (empty($filtering_entity_uris)) ?
80
+			$referencing_posts : Wordlift_Relation_Service::get_instance()->get_article_subjects(
81
+				wl_get_entity_post_ids_by_uris($filtering_entity_uris),
84 82
 				'*',
85 83
 				null,
86 84
 				null,
@@ -89,8 +87,8 @@  discard block
 block discarded – undo
89 87
 				$referencing_post_ids
90 88
 			);
91 89
 
92
-		if ( $filtered_posts ) {
93
-			foreach ( $filtered_posts as $post_obj ) {
90
+		if ($filtered_posts) {
91
+			foreach ($filtered_posts as $post_obj) {
94 92
 
95 93
 				/**
96 94
 				 * Use the thumbnail.
@@ -100,10 +98,10 @@  discard block
 block discarded – undo
100 98
 				 *
101 99
 				 * @since 3.19.3 We're using the medium size image.
102 100
 				 */
103
-				$thumbnail           = get_the_post_thumbnail_url( $post_obj, 'medium' );
104
-				$post_obj->thumbnail = ( $thumbnail ) ?
101
+				$thumbnail           = get_the_post_thumbnail_url($post_obj, 'medium');
102
+				$post_obj->thumbnail = ($thumbnail) ?
105 103
 					$thumbnail : WL_DEFAULT_THUMBNAIL_PATH;
106
-				$post_obj->permalink = get_post_permalink( $post_obj->ID );
104
+				$post_obj->permalink = get_post_permalink($post_obj->ID);
107 105
 
108 106
 				$results[] = $post_obj;
109 107
 			}
@@ -112,7 +110,7 @@  discard block
 block discarded – undo
112 110
 
113 111
 		global $wpdb;
114 112
 
115
-		wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ]" );
113
+		wl_write_log("Going to find related entities for the current post [ post ID :: $current_post_id ]");
116 114
 
117 115
 		// Retrieve Wordlift relation instances table name.
118 116
 		$table_name = wl_core_get_relation_instances_table_name();
@@ -121,8 +119,8 @@  discard block
 block discarded – undo
121 119
 		 * Make sure we have some referenced post, otherwise the IN parts of
122 120
 		 * the SQL will produce an SQL error.
123 121
 		 */
124
-		if ( ! empty( $referencing_post_ids ) ) {
125
-			$subject_ids = implode( ',', $referencing_post_ids );
122
+		if ( ! empty($referencing_post_ids)) {
123
+			$subject_ids = implode(',', $referencing_post_ids);
126 124
 
127 125
 			$query = "
128 126
 				SELECT
@@ -136,20 +134,20 @@  discard block
 block discarded – undo
136 134
 				LIMIT $limit;
137 135
 			";
138 136
 
139
-			wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" );
137
+			wl_write_log("Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]");
140 138
 
141
-			$entities = $wpdb->get_results( $query, OBJECT ); // No cache ok.
139
+			$entities = $wpdb->get_results($query, OBJECT); // No cache ok.
142 140
 
143
-			wl_write_log( 'Entities found ' . count( $entities ) );
141
+			wl_write_log('Entities found '.count($entities));
144 142
 
145
-			foreach ( $entities as $obj ) {
143
+			foreach ($entities as $obj) {
146 144
 
147
-				$entity = get_post( $obj->ID );
145
+				$entity = get_post($obj->ID);
148 146
 
149 147
 				// Ensure only valid and published entities are returned.
150
-				if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) {
148
+				if ((null !== $entity) && ('publish' === $entity->post_status)) {
151 149
 
152
-					$serialized_entity              = wl_serialize_entity( $entity );
150
+					$serialized_entity              = wl_serialize_entity($entity);
153 151
 					$serialized_entity['counter']   = $obj->counter;
154 152
 					$serialized_entity['createdAt'] = $entity->post_date;
155 153
 
@@ -169,25 +167,25 @@  discard block
 block discarded – undo
169 167
  * @return array $results
170 168
  * @since        3.20.0
171 169
  */
172
-function wl_shortcode_faceted_search_data_wp_json( $http_raw_data = null ) {
170
+function wl_shortcode_faceted_search_data_wp_json($http_raw_data = null) {
173 171
 
174 172
 	// Post ID must be defined.
175
-	if ( ! isset( $_GET['post_id'] ) ) { // WPCS: input var ok; CSRF ok.
176
-		wp_die( 'No post_id given' );
173
+	if ( ! isset($_GET['post_id'])) { // WPCS: input var ok; CSRF ok.
174
+		wp_die('No post_id given');
177 175
 
178 176
 		return;
179 177
 	}
180 178
 
181 179
 	// Extract filtering conditions.
182
-	$filtering_entity_uris = ( null == $http_raw_data ) ? file_get_contents( 'php://input' ) : $http_raw_data;
183
-	$filtering_entity_uris = json_decode( $filtering_entity_uris );
180
+	$filtering_entity_uris = (null == $http_raw_data) ? file_get_contents('php://input') : $http_raw_data;
181
+	$filtering_entity_uris = json_decode($filtering_entity_uris);
184 182
 
185 183
 	$current_post_id = $_GET['post_id']; // WPCS: input var ok; CSRF ok.
186
-	$current_post    = get_post( $current_post_id );
184
+	$current_post    = get_post($current_post_id);
187 185
 
188 186
 	// Post ID has to match an existing item.
189
-	if ( null === $current_post ) {
190
-		wp_die( 'No valid post_id given' );
187
+	if (null === $current_post) {
188
+		wp_die('No valid post_id given');
191 189
 
192 190
 		return;
193 191
 	}
@@ -196,45 +194,43 @@  discard block
 block discarded – undo
196 194
 	// the current post is used as main entity.
197 195
 	// Otherwise, current post related entities are used.
198 196
 	$entity_service = Wordlift_Entity_Service::get_instance();
199
-	$entity_ids     = $entity_service->is_entity( $current_post->ID ) ?
200
-		array( $current_post->ID ) :
201
-		wl_core_get_related_entity_ids( $current_post->ID );
197
+	$entity_ids     = $entity_service->is_entity($current_post->ID) ?
198
+		array($current_post->ID) : wl_core_get_related_entity_ids($current_post->ID);
202 199
 
203 200
 	// If there are no entities we cannot render the widget.
204
-	if ( 0 === count( $entity_ids ) ) {
205
-		wp_die( 'No entities available' );
201
+	if (0 === count($entity_ids)) {
202
+		wp_die('No entities available');
206 203
 
207 204
 		return;
208 205
 	}
209 206
 
210 207
 	// Retrieve requested type
211
-	$required_type = ( isset( $_GET['type'] ) ) ? $_GET['type'] : null; // WPCS: input var ok; CSRF ok.
208
+	$required_type = (isset($_GET['type'])) ? $_GET['type'] : null; // WPCS: input var ok; CSRF ok.
212 209
 
213
-	$limit = ( isset( $_GET['limit'] ) ) ? (int) $_GET['limit'] : 20;  // WPCS: input var ok; CSRF ok.
210
+	$limit = (isset($_GET['limit'])) ? (int) $_GET['limit'] : 20; // WPCS: input var ok; CSRF ok.
214 211
 
215 212
 	$referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects(
216 213
 		$entity_ids,
217 214
 		'*',
218 215
 		null,
219 216
 		'publish',
220
-		array( $current_post_id ),
217
+		array($current_post_id),
221 218
 		$limit
222 219
 	);
223 220
 
224
-	$referencing_post_ids = array_map( function ( $p ) {
221
+	$referencing_post_ids = array_map(function($p) {
225 222
 		return $p->ID;
226
-	}, $referencing_posts );
223
+	}, $referencing_posts);
227 224
 	$results              = array();
228 225
 
229
-	if ( 'posts' === $required_type ) {
226
+	if ('posts' === $required_type) {
230 227
 
231 228
 		// Required filtered posts.
232
-		wl_write_log( "Going to find related posts for the current post [ post ID :: $current_post_id ]" );
229
+		wl_write_log("Going to find related posts for the current post [ post ID :: $current_post_id ]");
233 230
 
234
-		$filtered_posts = ( empty( $filtering_entity_uris ) ) ?
235
-			$referencing_posts :
236
-			Wordlift_Relation_Service::get_instance()->get_article_subjects(
237
-				wl_get_entity_post_ids_by_uris( $filtering_entity_uris ),
231
+		$filtered_posts = (empty($filtering_entity_uris)) ?
232
+			$referencing_posts : Wordlift_Relation_Service::get_instance()->get_article_subjects(
233
+				wl_get_entity_post_ids_by_uris($filtering_entity_uris),
238 234
 				'*',
239 235
 				null,
240 236
 				null,
@@ -243,8 +239,8 @@  discard block
 block discarded – undo
243 239
 				$referencing_post_ids
244 240
 			);
245 241
 
246
-		if ( $filtered_posts ) {
247
-			foreach ( $filtered_posts as $i => $post_obj ) {
242
+		if ($filtered_posts) {
243
+			foreach ($filtered_posts as $i => $post_obj) {
248 244
 
249 245
 				/**
250 246
 				 * Use the thumbnail.
@@ -254,29 +250,29 @@  discard block
 block discarded – undo
254 250
 				 *
255 251
 				 * @since 3.19.3 We're using the medium size image.
256 252
 				 */
257
-				$thumbnail           = get_the_post_thumbnail_url( $post_obj, 'medium' );
258
-				$post_obj->thumbnail = ( $thumbnail ) ?
253
+				$thumbnail           = get_the_post_thumbnail_url($post_obj, 'medium');
254
+				$post_obj->thumbnail = ($thumbnail) ?
259 255
 					$thumbnail : WL_DEFAULT_THUMBNAIL_PATH;
260
-				$post_obj->permalink = get_post_permalink( $post_obj->ID );
256
+				$post_obj->permalink = get_post_permalink($post_obj->ID);
261 257
 
262
-				$results[ $i ] = $post_obj;
258
+				$results[$i] = $post_obj;
263 259
 
264 260
 				// Get Entity URLs needed for client side filtering in amp
265
-				foreach ( Wordlift_Relation_Service::get_instance()->get_objects( $post_obj->ID, 'ids' ) as $entity_id ) {
266
-					$results[ $i ]->entities[] = Wordlift_Entity_Service::get_instance()->get_uri( $entity_id );
261
+				foreach (Wordlift_Relation_Service::get_instance()->get_objects($post_obj->ID, 'ids') as $entity_id) {
262
+					$results[$i]->entities[] = Wordlift_Entity_Service::get_instance()->get_uri($entity_id);
267 263
 				}
268 264
 			}
269 265
 		}
270 266
 
271 267
 		return array(
272
-			array( 'values' => $results ),
268
+			array('values' => $results),
273 269
 		);
274 270
 
275 271
 	} else {
276 272
 
277 273
 		global $wpdb;
278 274
 
279
-		wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ]" );
275
+		wl_write_log("Going to find related entities for the current post [ post ID :: $current_post_id ]");
280 276
 
281 277
 		// Retrieve Wordlift relation instances table name.
282 278
 		$table_name = wl_core_get_relation_instances_table_name();
@@ -285,22 +281,22 @@  discard block
 block discarded – undo
285 281
 		$serialized_entity_groups = array(
286 282
 			array(
287 283
 				'l10n'     => $_GET['l10n'] ? $_GET['l10n']['what'] : 'what',
288
-				'entities' => array( 'thing', 'creative-work', 'recipe' ),
284
+				'entities' => array('thing', 'creative-work', 'recipe'),
289 285
 				'data'     => array(),
290 286
 			),
291 287
 			array(
292 288
 				'l10n'     => $_GET['l10n'] ? $_GET['l10n']['who'] : 'who',
293
-				'entities' => array( 'person', 'organization', 'local-business' ),
289
+				'entities' => array('person', 'organization', 'local-business'),
294 290
 				'data'     => array(),
295 291
 			),
296 292
 			array(
297 293
 				'l10n'     => $_GET['l10n'] ? $_GET['l10n']['where'] : 'where',
298
-				'entities' => array( 'place' ),
294
+				'entities' => array('place'),
299 295
 				'data'     => array(),
300 296
 			),
301 297
 			array(
302 298
 				'l10n'     => $_GET['l10n'] ? $_GET['l10n']['when'] : 'when',
303
-				'entities' => array( 'event' ),
299
+				'entities' => array('event'),
304 300
 				'data'     => array(),
305 301
 			),
306 302
 		);
@@ -309,8 +305,8 @@  discard block
 block discarded – undo
309 305
 		 * Make sure we have some referenced post, otherwise the IN parts of
310 306
 		 * the SQL will produce an SQL error.
311 307
 		 */
312
-		if ( ! empty( $referencing_post_ids ) ) {
313
-			$subject_ids = implode( ',', $referencing_post_ids );
308
+		if ( ! empty($referencing_post_ids)) {
309
+			$subject_ids = implode(',', $referencing_post_ids);
314 310
 
315 311
 			$query = "
316 312
 				SELECT
@@ -324,38 +320,38 @@  discard block
 block discarded – undo
324 320
 				LIMIT $limit;
325 321
 			";
326 322
 
327
-			wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" );
323
+			wl_write_log("Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]");
328 324
 
329
-			$entities = $wpdb->get_results( $query, OBJECT ); // No cache ok.
325
+			$entities = $wpdb->get_results($query, OBJECT); // No cache ok.
330 326
 
331
-			wl_write_log( 'Entities found ' . count( $entities ) );
327
+			wl_write_log('Entities found '.count($entities));
332 328
 
333
-			foreach ( $entities as $obj ) {
329
+			foreach ($entities as $obj) {
334 330
 
335
-				$entity = get_post( $obj->ID );
331
+				$entity = get_post($obj->ID);
336 332
 
337 333
 				// Ensure only valid and published entities are returned.
338
-				if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) {
334
+				if ((null !== $entity) && ('publish' === $entity->post_status)) {
339 335
 
340
-					$serialized_entity              = wl_serialize_entity( $entity );
336
+					$serialized_entity              = wl_serialize_entity($entity);
341 337
 					$serialized_entity['counter']   = $obj->counter;
342 338
 					$serialized_entity['createdAt'] = $entity->post_date;
343 339
 
344 340
 					// Populate $serialized_entity_groups maintaining array sequence
345
-					foreach ( $serialized_entity_groups as $seg_key => $seg_value ) {
346
-						if ( in_array( $serialized_entity['mainType'], $seg_value['entities'] ) ) {
347
-							$serialized_entity_groups[ $seg_key ]['data'][] = $serialized_entity;
341
+					foreach ($serialized_entity_groups as $seg_key => $seg_value) {
342
+						if (in_array($serialized_entity['mainType'], $seg_value['entities'])) {
343
+							$serialized_entity_groups[$seg_key]['data'][] = $serialized_entity;
348 344
 						}
349 345
 					}
350 346
 				}
351 347
 			}
352 348
 
353 349
 			// Clean-up $serialized_entity_groups by removing empty items and entities
354
-			foreach ( $serialized_entity_groups as $seg_key => $seg_value ) {
355
-				if ( empty( $seg_value['data'] ) ) {
356
-					unset( $serialized_entity_groups[ $seg_key ] );
350
+			foreach ($serialized_entity_groups as $seg_key => $seg_value) {
351
+				if (empty($seg_value['data'])) {
352
+					unset($serialized_entity_groups[$seg_key]);
357 353
 				}
358
-				unset( $serialized_entity_groups[ $seg_key ]['entities'] );
354
+				unset($serialized_entity_groups[$seg_key]['entities']);
359 355
 			}
360 356
 
361 357
 			$results = $serialized_entity_groups;
@@ -372,10 +368,10 @@  discard block
 block discarded – undo
372 368
  * @since 3.21.4 fix the cache key by also using the request body.
373 369
  * @since 3.21.2 add a caching layer.
374 370
  */
375
-function wl_shortcode_faceted_search_ajax( $http_raw_data = null ) {
371
+function wl_shortcode_faceted_search_ajax($http_raw_data = null) {
376 372
 
377 373
 	// Get the request body. It may contain posts to filter the results.
378
-	$request_body = ( empty( $http_raw_data ) ? file_get_contents( 'php://input' ) : $http_raw_data );
374
+	$request_body = (empty($http_raw_data) ? file_get_contents('php://input') : $http_raw_data);
379 375
 
380 376
 	// Create the cache key.
381 377
 	$cache_key = array(
@@ -384,40 +380,40 @@  discard block
 block discarded – undo
384 380
 	);
385 381
 
386 382
 	// Create the TTL cache and try to get the results.
387
-	$cache         = new Ttl_Cache( "faceted-search", 8 * 60 * 60 ); // 8 hours.
388
-	$cache_results = $cache->get( $cache_key );
383
+	$cache         = new Ttl_Cache("faceted-search", 8 * 60 * 60); // 8 hours.
384
+	$cache_results = $cache->get($cache_key);
389 385
 
390
-	if ( isset( $cache_results ) ) {
391
-		header( 'X-WordLift-Cache: HIT' );
392
-		wl_core_send_json( $cache_results );
386
+	if (isset($cache_results)) {
387
+		header('X-WordLift-Cache: HIT');
388
+		wl_core_send_json($cache_results);
393 389
 
394 390
 		return;
395 391
 	}
396 392
 
397
-	header( 'X-WordLift-Cache: MISS' );
393
+	header('X-WordLift-Cache: MISS');
398 394
 
399
-	$results = wl_shortcode_faceted_search_data_ajax( $request_body );
395
+	$results = wl_shortcode_faceted_search_data_ajax($request_body);
400 396
 
401 397
 	// Put the result before sending the json to the client, since sending the json will terminate us.
402
-	$cache->put( $cache_key, $results );
398
+	$cache->put($cache_key, $results);
403 399
 
404
-	wl_core_send_json( $results );
400
+	wl_core_send_json($results);
405 401
 
406 402
 }
407 403
 
408 404
 /**
409 405
  * Adding `wp_ajax` and `wp_ajax_nopriv` action for web backend of faceted-search
410 406
  */
411
-add_action( 'wp_ajax_wl_faceted_search', 'wl_shortcode_faceted_search_ajax' );
412
-add_action( 'wp_ajax_nopriv_wl_faceted_search', 'wl_shortcode_faceted_search_ajax' );
407
+add_action('wp_ajax_wl_faceted_search', 'wl_shortcode_faceted_search_ajax');
408
+add_action('wp_ajax_nopriv_wl_faceted_search', 'wl_shortcode_faceted_search_ajax');
413 409
 
414 410
 /**
415 411
  * wp-json call for the faceted search widget
416 412
  */
417
-function wl_shortcode_faceted_search_wp_json( $http_raw_data = null ) {
413
+function wl_shortcode_faceted_search_wp_json($http_raw_data = null) {
418 414
 
419
-	$results = wl_shortcode_faceted_search_data_wp_json( $http_raw_data );
420
-	if ( ob_get_contents() ) {
415
+	$results = wl_shortcode_faceted_search_data_wp_json($http_raw_data);
416
+	if (ob_get_contents()) {
421 417
 		ob_clean();
422 418
 	}
423 419
 
@@ -430,36 +426,36 @@  discard block
 block discarded – undo
430 426
 /**
431 427
  * Adding `rest_api_init` action for amp backend of faceted-search
432 428
  */
433
-add_action( 'rest_api_init', function () {
434
-	register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search', array(
429
+add_action('rest_api_init', function() {
430
+	register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search', array(
435 431
 		'methods'  => 'GET',
436 432
 		'callback' => 'wl_shortcode_faceted_search_wp_json',
437
-	) );
433
+	));
438 434
 } );
439 435
 
440 436
 /**
441 437
  * register_block_type for Gutenberg blocks
442 438
  */
443
-add_action( 'init', function () {
439
+add_action('init', function() {
444 440
 	// Bail out if the `register_block_type` function isn't available.
445
-	if ( ! function_exists( 'register_block_type' ) ) {
441
+	if ( ! function_exists('register_block_type')) {
446 442
 		return;
447 443
 	}
448 444
 
449
-	register_block_type( 'wordlift/faceted-search', array(
445
+	register_block_type('wordlift/faceted-search', array(
450 446
 		'editor_script'   => 'wordlift-admin-edit-gutenberg',
451
-		'render_callback' => function ( $attributes ) {
447
+		'render_callback' => function($attributes) {
452 448
 			$attr_code = '';
453
-			foreach ( $attributes as $key => $value ) {
454
-				$attr_code .= $key . '="' . $value . '" ';
449
+			foreach ($attributes as $key => $value) {
450
+				$attr_code .= $key.'="'.$value.'" ';
455 451
 			}
456 452
 
457
-			return '[wl_faceted_search ' . $attr_code . ']';
453
+			return '[wl_faceted_search '.$attr_code.']';
458 454
 		},
459 455
 		'attributes'      => array(
460 456
 			'title'          => array(
461 457
 				'type'    => 'string',
462
-				'default' => __( 'Related articles', 'wordlift' ),
458
+				'default' => __('Related articles', 'wordlift'),
463 459
 			),
464 460
 			'show_facets'    => array(
465 461
 				'type'    => 'bool',
@@ -478,5 +474,5 @@  discard block
 block discarded – undo
478 474
 				'default' => 20,
479 475
 			),
480 476
 		),
481
-	) );
477
+	));
482 478
 } );
Please login to merge, or discard this patch.
src/includes/mapping/class-wordlift-mapping-service.php 2 patches
Indentation   +200 added lines, -200 removed lines patch added patch discarded remove patch
@@ -14,205 +14,205 @@
 block discarded – undo
14 14
  */
15 15
 class Wordlift_Mapping_Service {
16 16
 
17
-	/**
18
-	 * The mapping's options.
19
-	 *
20
-	 * @since 3.20.0
21
-	 * @access private
22
-	 * @var array $options The mapping's options.
23
-	 */
24
-	private $options;
25
-
26
-	/**
27
-	 * The {@link Wordlift_Entity_Type_Service} instance.
28
-	 *
29
-	 * @since 3.20.0
30
-	 * @access private
31
-	 * @var \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance.
32
-	 */
33
-	private $entity_type_service;
34
-
35
-	/**
36
-	 * The singleton instance.
37
-	 *
38
-	 * @since 3.20.0
39
-	 * @access private
40
-	 * @var \Wordlift_Mapping_Service $instance The singleton instance.
41
-	 */
42
-	private static $instance;
43
-
44
-	/**
45
-	 * Create a {@link Wordlift_Mapping_Service} instance.
46
-	 *
47
-	 * @since 3.20.0
48
-	 *
49
-	 * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance.
50
-	 */
51
-	public function __construct( $entity_type_service ) {
52
-
53
-		// Set the entity type service instance.
54
-		$this->entity_type_service = $entity_type_service;
55
-
56
-		// Load the options.
57
-		$this->options = get_option( 'wl_mappings', array() );
58
-
59
-		// Hook to `wl_valid_entity_post_types` and to `wl_default_entity_types_for_post_type`.
60
-		add_filter( 'wl_valid_entity_post_types', array( $this, 'valid_entity_post_types', ), 9 );
61
-		add_filter( 'wl_default_entity_types_for_post_type', array(
62
-			$this,
63
-			'default_entity_types_for_post_type',
64
-		), 9, 2 );
65
-
66
-		// Set the singleton instance.
67
-		self::$instance = $this;
68
-
69
-	}
70
-
71
-	/**
72
-	 * Get the singleton instance.
73
-	 *
74
-	 * @since 3.20.0
75
-	 *
76
-	 * @return \Wordlift_Mapping_Service The singleton instance.
77
-	 */
78
-	public static function get_instance() {
79
-
80
-		return self::$instance;
81
-	}
82
-
83
-	/**
84
-	 * Save the options.
85
-	 *
86
-	 * @since 3.20.0
87
-	 */
88
-	private function save_options() {
89
-
90
-		update_option( 'wl_mappings', $this->options, true );
91
-
92
-	}
93
-
94
-	/**
95
-	 * Set the default entity types for a post type.
96
-	 *
97
-	 * @since 3.20.0
98
-	 *
99
-	 * @param string $post_type Post type.
100
-	 * @param array  $entity_types An array of entity types slugs.
101
-	 */
102
-	public function set_entity_types_for_post_type( $post_type, $entity_types ) {
103
-
104
-		$this->options[ $post_type ] = $entity_types;
105
-		$this->save_options();
106
-
107
-	}
108
-
109
-	/**
110
-	 * Hook to `wl_valid_entity_post_types` to declare schema.org support for the configured post types.
111
-	 *
112
-	 * @since 3.20.0
113
-	 *
114
-	 * @param array $post_types The default post types.
115
-	 *
116
-	 * @return array The supported post types.
117
-	 */
118
-	public function valid_entity_post_types( $post_types ) {
119
-
120
-		return array_merge( $post_types, array_keys( $this->options ) );
121
-	}
122
-
123
-	/**
124
-	 * Hook to `wl_default_entity_types_for_post_type` to declare the entity types for a post type.
125
-	 *
126
-	 * @since 3.20.0
127
-	 *
128
-	 * @param array  $default The default entity types.
129
-	 * @param string $post_type The post type.
130
-	 *
131
-	 * @return array The default entity types.
132
-	 */
133
-	public function default_entity_types_for_post_type( $default, $post_type ) {
134
-
135
-		return isset( $this->options[ $post_type ] ) ? $this->options[ $post_type ] : $default;
136
-	}
137
-
138
-	/**
139
-	 * Update the post type with the entity types, starting at the specified offset.
140
-	 *
141
-	 * @since 3.20.0
142
-	 *
143
-	 * @param string $post_type The post type.
144
-	 * @param array  $entity_types The entity types.
145
-	 * @param int    $offset The offset (0 by default).
146
-	 *
147
-	 * @return array {
148
-	 * The result array.
149
-	 *
150
-	 * @type int     $current The current offset.
151
-	 * @type int     $next The next offset.
152
-	 * @type int     $count The total element count.
153
-	 * }
154
-	 */
155
-	public function update( $post_type, $entity_types, $offset = 0 ) {
156
-
157
-		$entity_type_service = $this->entity_type_service;
158
-		$tax_query           = $this->get_tax_query( $entity_types );
159
-
160
-		return Wordlift_Batch_Action::process( $post_type, $offset, $tax_query, function ( $post_id ) use ( $entity_type_service, $entity_types ) {
161
-			foreach ( $entity_types as $entity_type ) {
162
-				$entity_type_service->set( $post_id, $entity_type, false );
163
-			}
164
-		} );
165
-	}
166
-
167
-	/**
168
-	 * Count the number of posts that need to be assigned with the entity types.
169
-	 *
170
-	 * @since 3.20.0
171
-	 *
172
-	 * @param string $post_type The post type.
173
-	 * @param array  $entity_types An array of entity types.
174
-	 *
175
-	 * @return int The number of posts to be assigned with entity types.
176
-	 */
177
-	public function count( $post_type, $entity_types ) {
178
-
179
-		$tax_query = $this->get_tax_query( $entity_types );
180
-
181
-		return Wordlift_Batch_Action::count( $post_type, $tax_query );
182
-	}
183
-
184
-	/**
185
-	 * Get the taxonomy query for the specified entity types.
186
-	 *
187
-	 * @since 3.20.0
188
-	 *
189
-	 * @param array $entity_types The entity types.
190
-	 *
191
-	 * @return array The tax query.
192
-	 */
193
-	private function get_tax_query( $entity_types ) {
194
-
195
-		$entity_type_service = $this->entity_type_service;
196
-		$entity_types_terms  = array_filter( array_map( function ( $item ) use ( $entity_type_service ) {
197
-			return $entity_type_service->get_term_by_uri( $item );
198
-		}, $entity_types ) );
199
-
200
-		$entity_types_terms_ids = array_map( function ( $term ) {
201
-			return $term->term_id;
202
-		}, $entity_types_terms );
203
-
204
-		$tax_query = array(
205
-			'tax_query' => array(
206
-				array(
207
-					'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
208
-					'field'    => 'term_id',
209
-					'terms'    => $entity_types_terms_ids,
210
-					'operator' => 'NOT IN',
211
-				),
212
-			),
213
-		);
214
-
215
-		return $tax_query;
216
-	}
17
+    /**
18
+     * The mapping's options.
19
+     *
20
+     * @since 3.20.0
21
+     * @access private
22
+     * @var array $options The mapping's options.
23
+     */
24
+    private $options;
25
+
26
+    /**
27
+     * The {@link Wordlift_Entity_Type_Service} instance.
28
+     *
29
+     * @since 3.20.0
30
+     * @access private
31
+     * @var \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance.
32
+     */
33
+    private $entity_type_service;
34
+
35
+    /**
36
+     * The singleton instance.
37
+     *
38
+     * @since 3.20.0
39
+     * @access private
40
+     * @var \Wordlift_Mapping_Service $instance The singleton instance.
41
+     */
42
+    private static $instance;
43
+
44
+    /**
45
+     * Create a {@link Wordlift_Mapping_Service} instance.
46
+     *
47
+     * @since 3.20.0
48
+     *
49
+     * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance.
50
+     */
51
+    public function __construct( $entity_type_service ) {
52
+
53
+        // Set the entity type service instance.
54
+        $this->entity_type_service = $entity_type_service;
55
+
56
+        // Load the options.
57
+        $this->options = get_option( 'wl_mappings', array() );
58
+
59
+        // Hook to `wl_valid_entity_post_types` and to `wl_default_entity_types_for_post_type`.
60
+        add_filter( 'wl_valid_entity_post_types', array( $this, 'valid_entity_post_types', ), 9 );
61
+        add_filter( 'wl_default_entity_types_for_post_type', array(
62
+            $this,
63
+            'default_entity_types_for_post_type',
64
+        ), 9, 2 );
65
+
66
+        // Set the singleton instance.
67
+        self::$instance = $this;
68
+
69
+    }
70
+
71
+    /**
72
+     * Get the singleton instance.
73
+     *
74
+     * @since 3.20.0
75
+     *
76
+     * @return \Wordlift_Mapping_Service The singleton instance.
77
+     */
78
+    public static function get_instance() {
79
+
80
+        return self::$instance;
81
+    }
82
+
83
+    /**
84
+     * Save the options.
85
+     *
86
+     * @since 3.20.0
87
+     */
88
+    private function save_options() {
89
+
90
+        update_option( 'wl_mappings', $this->options, true );
91
+
92
+    }
93
+
94
+    /**
95
+     * Set the default entity types for a post type.
96
+     *
97
+     * @since 3.20.0
98
+     *
99
+     * @param string $post_type Post type.
100
+     * @param array  $entity_types An array of entity types slugs.
101
+     */
102
+    public function set_entity_types_for_post_type( $post_type, $entity_types ) {
103
+
104
+        $this->options[ $post_type ] = $entity_types;
105
+        $this->save_options();
106
+
107
+    }
108
+
109
+    /**
110
+     * Hook to `wl_valid_entity_post_types` to declare schema.org support for the configured post types.
111
+     *
112
+     * @since 3.20.0
113
+     *
114
+     * @param array $post_types The default post types.
115
+     *
116
+     * @return array The supported post types.
117
+     */
118
+    public function valid_entity_post_types( $post_types ) {
119
+
120
+        return array_merge( $post_types, array_keys( $this->options ) );
121
+    }
122
+
123
+    /**
124
+     * Hook to `wl_default_entity_types_for_post_type` to declare the entity types for a post type.
125
+     *
126
+     * @since 3.20.0
127
+     *
128
+     * @param array  $default The default entity types.
129
+     * @param string $post_type The post type.
130
+     *
131
+     * @return array The default entity types.
132
+     */
133
+    public function default_entity_types_for_post_type( $default, $post_type ) {
134
+
135
+        return isset( $this->options[ $post_type ] ) ? $this->options[ $post_type ] : $default;
136
+    }
137
+
138
+    /**
139
+     * Update the post type with the entity types, starting at the specified offset.
140
+     *
141
+     * @since 3.20.0
142
+     *
143
+     * @param string $post_type The post type.
144
+     * @param array  $entity_types The entity types.
145
+     * @param int    $offset The offset (0 by default).
146
+     *
147
+     * @return array {
148
+     * The result array.
149
+     *
150
+     * @type int     $current The current offset.
151
+     * @type int     $next The next offset.
152
+     * @type int     $count The total element count.
153
+     * }
154
+     */
155
+    public function update( $post_type, $entity_types, $offset = 0 ) {
156
+
157
+        $entity_type_service = $this->entity_type_service;
158
+        $tax_query           = $this->get_tax_query( $entity_types );
159
+
160
+        return Wordlift_Batch_Action::process( $post_type, $offset, $tax_query, function ( $post_id ) use ( $entity_type_service, $entity_types ) {
161
+            foreach ( $entity_types as $entity_type ) {
162
+                $entity_type_service->set( $post_id, $entity_type, false );
163
+            }
164
+        } );
165
+    }
166
+
167
+    /**
168
+     * Count the number of posts that need to be assigned with the entity types.
169
+     *
170
+     * @since 3.20.0
171
+     *
172
+     * @param string $post_type The post type.
173
+     * @param array  $entity_types An array of entity types.
174
+     *
175
+     * @return int The number of posts to be assigned with entity types.
176
+     */
177
+    public function count( $post_type, $entity_types ) {
178
+
179
+        $tax_query = $this->get_tax_query( $entity_types );
180
+
181
+        return Wordlift_Batch_Action::count( $post_type, $tax_query );
182
+    }
183
+
184
+    /**
185
+     * Get the taxonomy query for the specified entity types.
186
+     *
187
+     * @since 3.20.0
188
+     *
189
+     * @param array $entity_types The entity types.
190
+     *
191
+     * @return array The tax query.
192
+     */
193
+    private function get_tax_query( $entity_types ) {
194
+
195
+        $entity_type_service = $this->entity_type_service;
196
+        $entity_types_terms  = array_filter( array_map( function ( $item ) use ( $entity_type_service ) {
197
+            return $entity_type_service->get_term_by_uri( $item );
198
+        }, $entity_types ) );
199
+
200
+        $entity_types_terms_ids = array_map( function ( $term ) {
201
+            return $term->term_id;
202
+        }, $entity_types_terms );
203
+
204
+        $tax_query = array(
205
+            'tax_query' => array(
206
+                array(
207
+                    'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
208
+                    'field'    => 'term_id',
209
+                    'terms'    => $entity_types_terms_ids,
210
+                    'operator' => 'NOT IN',
211
+                ),
212
+            ),
213
+        );
214
+
215
+        return $tax_query;
216
+    }
217 217
 
218 218
 }
Please login to merge, or discard this patch.
Spacing   +26 added lines, -26 removed lines patch added patch discarded remove patch
@@ -48,20 +48,20 @@  discard block
 block discarded – undo
48 48
 	 *
49 49
 	 * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance.
50 50
 	 */
51
-	public function __construct( $entity_type_service ) {
51
+	public function __construct($entity_type_service) {
52 52
 
53 53
 		// Set the entity type service instance.
54 54
 		$this->entity_type_service = $entity_type_service;
55 55
 
56 56
 		// Load the options.
57
-		$this->options = get_option( 'wl_mappings', array() );
57
+		$this->options = get_option('wl_mappings', array());
58 58
 
59 59
 		// Hook to `wl_valid_entity_post_types` and to `wl_default_entity_types_for_post_type`.
60
-		add_filter( 'wl_valid_entity_post_types', array( $this, 'valid_entity_post_types', ), 9 );
61
-		add_filter( 'wl_default_entity_types_for_post_type', array(
60
+		add_filter('wl_valid_entity_post_types', array($this, 'valid_entity_post_types',), 9);
61
+		add_filter('wl_default_entity_types_for_post_type', array(
62 62
 			$this,
63 63
 			'default_entity_types_for_post_type',
64
-		), 9, 2 );
64
+		), 9, 2);
65 65
 
66 66
 		// Set the singleton instance.
67 67
 		self::$instance = $this;
@@ -87,7 +87,7 @@  discard block
 block discarded – undo
87 87
 	 */
88 88
 	private function save_options() {
89 89
 
90
-		update_option( 'wl_mappings', $this->options, true );
90
+		update_option('wl_mappings', $this->options, true);
91 91
 
92 92
 	}
93 93
 
@@ -99,9 +99,9 @@  discard block
 block discarded – undo
99 99
 	 * @param string $post_type Post type.
100 100
 	 * @param array  $entity_types An array of entity types slugs.
101 101
 	 */
102
-	public function set_entity_types_for_post_type( $post_type, $entity_types ) {
102
+	public function set_entity_types_for_post_type($post_type, $entity_types) {
103 103
 
104
-		$this->options[ $post_type ] = $entity_types;
104
+		$this->options[$post_type] = $entity_types;
105 105
 		$this->save_options();
106 106
 
107 107
 	}
@@ -115,9 +115,9 @@  discard block
 block discarded – undo
115 115
 	 *
116 116
 	 * @return array The supported post types.
117 117
 	 */
118
-	public function valid_entity_post_types( $post_types ) {
118
+	public function valid_entity_post_types($post_types) {
119 119
 
120
-		return array_merge( $post_types, array_keys( $this->options ) );
120
+		return array_merge($post_types, array_keys($this->options));
121 121
 	}
122 122
 
123 123
 	/**
@@ -130,9 +130,9 @@  discard block
 block discarded – undo
130 130
 	 *
131 131
 	 * @return array The default entity types.
132 132
 	 */
133
-	public function default_entity_types_for_post_type( $default, $post_type ) {
133
+	public function default_entity_types_for_post_type($default, $post_type) {
134 134
 
135
-		return isset( $this->options[ $post_type ] ) ? $this->options[ $post_type ] : $default;
135
+		return isset($this->options[$post_type]) ? $this->options[$post_type] : $default;
136 136
 	}
137 137
 
138 138
 	/**
@@ -152,14 +152,14 @@  discard block
 block discarded – undo
152 152
 	 * @type int     $count The total element count.
153 153
 	 * }
154 154
 	 */
155
-	public function update( $post_type, $entity_types, $offset = 0 ) {
155
+	public function update($post_type, $entity_types, $offset = 0) {
156 156
 
157 157
 		$entity_type_service = $this->entity_type_service;
158
-		$tax_query           = $this->get_tax_query( $entity_types );
158
+		$tax_query           = $this->get_tax_query($entity_types);
159 159
 
160
-		return Wordlift_Batch_Action::process( $post_type, $offset, $tax_query, function ( $post_id ) use ( $entity_type_service, $entity_types ) {
161
-			foreach ( $entity_types as $entity_type ) {
162
-				$entity_type_service->set( $post_id, $entity_type, false );
160
+		return Wordlift_Batch_Action::process($post_type, $offset, $tax_query, function($post_id) use ($entity_type_service, $entity_types) {
161
+			foreach ($entity_types as $entity_type) {
162
+				$entity_type_service->set($post_id, $entity_type, false);
163 163
 			}
164 164
 		} );
165 165
 	}
@@ -174,11 +174,11 @@  discard block
 block discarded – undo
174 174
 	 *
175 175
 	 * @return int The number of posts to be assigned with entity types.
176 176
 	 */
177
-	public function count( $post_type, $entity_types ) {
177
+	public function count($post_type, $entity_types) {
178 178
 
179
-		$tax_query = $this->get_tax_query( $entity_types );
179
+		$tax_query = $this->get_tax_query($entity_types);
180 180
 
181
-		return Wordlift_Batch_Action::count( $post_type, $tax_query );
181
+		return Wordlift_Batch_Action::count($post_type, $tax_query);
182 182
 	}
183 183
 
184 184
 	/**
@@ -190,16 +190,16 @@  discard block
 block discarded – undo
190 190
 	 *
191 191
 	 * @return array The tax query.
192 192
 	 */
193
-	private function get_tax_query( $entity_types ) {
193
+	private function get_tax_query($entity_types) {
194 194
 
195 195
 		$entity_type_service = $this->entity_type_service;
196
-		$entity_types_terms  = array_filter( array_map( function ( $item ) use ( $entity_type_service ) {
197
-			return $entity_type_service->get_term_by_uri( $item );
198
-		}, $entity_types ) );
196
+		$entity_types_terms  = array_filter(array_map(function($item) use ($entity_type_service) {
197
+			return $entity_type_service->get_term_by_uri($item);
198
+		}, $entity_types));
199 199
 
200
-		$entity_types_terms_ids = array_map( function ( $term ) {
200
+		$entity_types_terms_ids = array_map(function($term) {
201 201
 			return $term->term_id;
202
-		}, $entity_types_terms );
202
+		}, $entity_types_terms);
203 203
 
204 204
 		$tax_query = array(
205 205
 			'tax_query' => array(
Please login to merge, or discard this patch.