Completed
Push — develop ( cf0659...0af133 )
by David
01:28 queued 12s
created
src/install/class-wordlift-install-service.php 2 patches
Indentation   +191 added lines, -191 removed lines patch added patch discarded remove patch
@@ -18,198 +18,198 @@
 block discarded – undo
18 18
  */
19 19
 class Wordlift_Install_Service {
20 20
 
21
-	/**
22
-	 * A {@link Wordlift_Log_Service} instance.
23
-	 *
24
-	 * @since  3.18.0
25
-	 * @access private
26
-	 * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
27
-	 */
28
-	private $log;
29
-
30
-	/**
31
-	 * The singleton instance.
32
-	 *
33
-	 * @since  3.18.0
34
-	 * @access private
35
-	 * @var \Wordlift_Install_Service $instance A {@link Wordlift_Install_Service} instance.
36
-	 */
37
-	private static $instance;
38
-
39
-	/**
40
-	 * @var array
41
-	 */
42
-	private $installs;
43
-
44
-	/**
45
-	 * Wordlift_Install_Service constructor.
46
-	 *
47
-	 * @since 3.18.0
48
-	 */
49
-	public function __construct() {
50
-
51
-		/** Installs. */
52
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install.php';
53
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-1-0-0.php';
54
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-10-0.php';
55
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-12-0.php';
56
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-14-0.php';
57
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-15-0.php';
58
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-18-0.php';
59
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-18-3.php';
60
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-19-5.php';
61
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-20-0.php';
62
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-23-4.php';
63
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-24-2.php';
64
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-all-entity-types.php';
65
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-package-type.php';
66
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-25-0.php';
67
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-27-0.php';
68
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-27-1.php';
69
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-28-0.php';
70
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-32-0.php';
71
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-33-9.php';
72
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-36-0.php';
73
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-38-5.php';
74
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-39-1.php';
75
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-40-1.php';
76
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-40-2.php';
77
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-41-0.php';
78
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-42-0.php';
79
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-44-1.php';
80
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-44-4.php';
81
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-45-0.php';
82
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-45-1.php';
83
-
84
-		// Get the install services.
85
-		$this->installs = array(
86
-			new Wordlift_Install_1_0_0(),
87
-			new Wordlift_Install_3_10_0(),
88
-			new Wordlift_Install_3_12_0(),
89
-			new Wordlift_Install_3_14_0(),
90
-			new Wordlift_Install_3_15_0(),
91
-			new Wordlift_Install_3_18_0(),
92
-			new Wordlift_Install_3_18_3(),
93
-			new Wordlift_Install_3_19_5(),
94
-			new Wordlift_Install_3_20_0(),
95
-
96
-			/*
21
+    /**
22
+     * A {@link Wordlift_Log_Service} instance.
23
+     *
24
+     * @since  3.18.0
25
+     * @access private
26
+     * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
27
+     */
28
+    private $log;
29
+
30
+    /**
31
+     * The singleton instance.
32
+     *
33
+     * @since  3.18.0
34
+     * @access private
35
+     * @var \Wordlift_Install_Service $instance A {@link Wordlift_Install_Service} instance.
36
+     */
37
+    private static $instance;
38
+
39
+    /**
40
+     * @var array
41
+     */
42
+    private $installs;
43
+
44
+    /**
45
+     * Wordlift_Install_Service constructor.
46
+     *
47
+     * @since 3.18.0
48
+     */
49
+    public function __construct() {
50
+
51
+        /** Installs. */
52
+        require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install.php';
53
+        require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-1-0-0.php';
54
+        require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-10-0.php';
55
+        require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-12-0.php';
56
+        require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-14-0.php';
57
+        require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-15-0.php';
58
+        require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-18-0.php';
59
+        require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-18-3.php';
60
+        require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-19-5.php';
61
+        require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-20-0.php';
62
+        require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-23-4.php';
63
+        require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-24-2.php';
64
+        require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-all-entity-types.php';
65
+        require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-package-type.php';
66
+        require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-25-0.php';
67
+        require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-27-0.php';
68
+        require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-27-1.php';
69
+        require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-28-0.php';
70
+        require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-32-0.php';
71
+        require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-33-9.php';
72
+        require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-36-0.php';
73
+        require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-38-5.php';
74
+        require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-39-1.php';
75
+        require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-40-1.php';
76
+        require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-40-2.php';
77
+        require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-41-0.php';
78
+        require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-42-0.php';
79
+        require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-44-1.php';
80
+        require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-44-4.php';
81
+        require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-45-0.php';
82
+        require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-45-1.php';
83
+
84
+        // Get the install services.
85
+        $this->installs = array(
86
+            new Wordlift_Install_1_0_0(),
87
+            new Wordlift_Install_3_10_0(),
88
+            new Wordlift_Install_3_12_0(),
89
+            new Wordlift_Install_3_14_0(),
90
+            new Wordlift_Install_3_15_0(),
91
+            new Wordlift_Install_3_18_0(),
92
+            new Wordlift_Install_3_18_3(),
93
+            new Wordlift_Install_3_19_5(),
94
+            new Wordlift_Install_3_20_0(),
95
+
96
+            /*
97 97
 			 * This should be enabled with #852.
98 98
 			 */
99
-			new Wordlift_Install_Package_Type(),
100
-			new Wordlift_Install_3_23_4(),
101
-			new Wordlift_Install_3_24_2(),
102
-			new Wordlift_Install_3_25_0(),
103
-			new Wordlift_Install_3_27_0(),
104
-			new Wordlift_Install_3_27_1(),
105
-			new Wordlift_Install_3_28_0(),
106
-			// Add column to represent term
107
-			new Wordlift_Install_3_32_0(),
108
-			// Add the entities table.
109
-			new Wordlift_Install_3_33_9(),
110
-			// When woocommerce extension installed, acf4so should be installed automatically.
111
-			new Wordlift_Install_3_36_0(),
112
-
113
-			new Wordlift_Install_3_38_5(),
114
-			new Wordlift_Install_3_39_1(),
115
-
116
-			// See #1621.
117
-			new Wordlift_Install_3_40_1(),
118
-
119
-			// @link https://github.com/insideout10/wordlift-plugin/issues/1627
120
-			new Wordlift_Install_3_40_2(),
121
-
122
-			new Wordlift_Install_3_41_0(),
123
-			new Wordlift_Install_3_42_0(),
124
-
125
-			new Wordlift_Install_3_44_1(),
126
-			new Wordlift_Install_3_44_4(),
127
-
128
-			new Wordlift_Install_3_45_0(),
129
-			new Wordlift_Install_3_45_1(),
130
-		);
131
-		self::$instance = $this;
132
-
133
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
134
-
135
-		add_action( 'init', array( $this, 'install' ) );
136
-
137
-	}
138
-
139
-	/**
140
-	 * Get the singleton instance.
141
-	 *
142
-	 * @since 3.18.0
143
-	 */
144
-	public static function get_instance() {
145
-
146
-		return self::$instance;
147
-	}
148
-
149
-	/**
150
-	 * Loop thought all versions and install the updates.
151
-	 *
152
-	 * @return void
153
-	 * @since 3.18.0
154
-	 *
155
-	 * @since 3.20.0 use a transient to avoid concurrent installation calls.
156
-	 */
157
-	public function install() {
158
-
159
-		$version = null;
160
-
161
-		if ( $this->install_required() && false === get_transient( '_wl_installing' ) ) {
162
-			set_transient( '_wl_installing', true, 5 * MINUTE_IN_SECONDS );
163
-			/** @var Wordlift_Install $install */
164
-			foreach ( $this->installs as $install ) {
165
-				// Get the install version.
166
-				$version = $install->get_version();
167
-
168
-				if ( version_compare( $version, $this->get_current_version(), '>' )
169
-					 || $install->must_install() ) {
170
-					$class_name = get_class( $install );
171
-
172
-					$this->log->info( "Current version is {$this->get_current_version()}, installing $class_name..." );
173
-					// Install version.
174
-					$install->install();
175
-
176
-					$this->log->info( "$class_name installed." );
177
-
178
-					// Bump the version.
179
-					update_option( 'wl_db_version', $version );
180
-				}
181
-			}
182
-
183
-			// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
184
-			@delete_transient( '_wl_installing' );
185
-
186
-		}
187
-
188
-	}
189
-
190
-	private function install_required() {
191
-
192
-		/** @var Wordlift_Install $install */
193
-		foreach ( $this->installs as $install ) {
194
-			// Get the install version.
195
-			$version = $install->get_version();
196
-
197
-			if ( version_compare( $version, $this->get_current_version(), '>' )
198
-				 || $install->must_install() ) {
199
-				return true;
200
-			}
201
-		}
202
-
203
-		return false;
204
-	}
205
-
206
-	/**
207
-	 * Retrieve the current db version.
208
-	 *
209
-	 * @return type
210
-	 */
211
-	private function get_current_version() {
212
-		return get_option( 'wl_db_version', '0.0.0' );
213
-	}
99
+            new Wordlift_Install_Package_Type(),
100
+            new Wordlift_Install_3_23_4(),
101
+            new Wordlift_Install_3_24_2(),
102
+            new Wordlift_Install_3_25_0(),
103
+            new Wordlift_Install_3_27_0(),
104
+            new Wordlift_Install_3_27_1(),
105
+            new Wordlift_Install_3_28_0(),
106
+            // Add column to represent term
107
+            new Wordlift_Install_3_32_0(),
108
+            // Add the entities table.
109
+            new Wordlift_Install_3_33_9(),
110
+            // When woocommerce extension installed, acf4so should be installed automatically.
111
+            new Wordlift_Install_3_36_0(),
112
+
113
+            new Wordlift_Install_3_38_5(),
114
+            new Wordlift_Install_3_39_1(),
115
+
116
+            // See #1621.
117
+            new Wordlift_Install_3_40_1(),
118
+
119
+            // @link https://github.com/insideout10/wordlift-plugin/issues/1627
120
+            new Wordlift_Install_3_40_2(),
121
+
122
+            new Wordlift_Install_3_41_0(),
123
+            new Wordlift_Install_3_42_0(),
124
+
125
+            new Wordlift_Install_3_44_1(),
126
+            new Wordlift_Install_3_44_4(),
127
+
128
+            new Wordlift_Install_3_45_0(),
129
+            new Wordlift_Install_3_45_1(),
130
+        );
131
+        self::$instance = $this;
132
+
133
+        $this->log = Wordlift_Log_Service::get_logger( get_class() );
134
+
135
+        add_action( 'init', array( $this, 'install' ) );
136
+
137
+    }
138
+
139
+    /**
140
+     * Get the singleton instance.
141
+     *
142
+     * @since 3.18.0
143
+     */
144
+    public static function get_instance() {
145
+
146
+        return self::$instance;
147
+    }
148
+
149
+    /**
150
+     * Loop thought all versions and install the updates.
151
+     *
152
+     * @return void
153
+     * @since 3.18.0
154
+     *
155
+     * @since 3.20.0 use a transient to avoid concurrent installation calls.
156
+     */
157
+    public function install() {
158
+
159
+        $version = null;
160
+
161
+        if ( $this->install_required() && false === get_transient( '_wl_installing' ) ) {
162
+            set_transient( '_wl_installing', true, 5 * MINUTE_IN_SECONDS );
163
+            /** @var Wordlift_Install $install */
164
+            foreach ( $this->installs as $install ) {
165
+                // Get the install version.
166
+                $version = $install->get_version();
167
+
168
+                if ( version_compare( $version, $this->get_current_version(), '>' )
169
+                     || $install->must_install() ) {
170
+                    $class_name = get_class( $install );
171
+
172
+                    $this->log->info( "Current version is {$this->get_current_version()}, installing $class_name..." );
173
+                    // Install version.
174
+                    $install->install();
175
+
176
+                    $this->log->info( "$class_name installed." );
177
+
178
+                    // Bump the version.
179
+                    update_option( 'wl_db_version', $version );
180
+                }
181
+            }
182
+
183
+            // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
184
+            @delete_transient( '_wl_installing' );
185
+
186
+        }
187
+
188
+    }
189
+
190
+    private function install_required() {
191
+
192
+        /** @var Wordlift_Install $install */
193
+        foreach ( $this->installs as $install ) {
194
+            // Get the install version.
195
+            $version = $install->get_version();
196
+
197
+            if ( version_compare( $version, $this->get_current_version(), '>' )
198
+                 || $install->must_install() ) {
199
+                return true;
200
+            }
201
+        }
202
+
203
+        return false;
204
+    }
205
+
206
+    /**
207
+     * Retrieve the current db version.
208
+     *
209
+     * @return type
210
+     */
211
+    private function get_current_version() {
212
+        return get_option( 'wl_db_version', '0.0.0' );
213
+    }
214 214
 
215 215
 }
Please login to merge, or discard this patch.
Spacing   +47 added lines, -47 removed lines patch added patch discarded remove patch
@@ -49,37 +49,37 @@  discard block
 block discarded – undo
49 49
 	public function __construct() {
50 50
 
51 51
 		/** Installs. */
52
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install.php';
53
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-1-0-0.php';
54
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-10-0.php';
55
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-12-0.php';
56
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-14-0.php';
57
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-15-0.php';
58
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-18-0.php';
59
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-18-3.php';
60
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-19-5.php';
61
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-20-0.php';
62
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-23-4.php';
63
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-24-2.php';
64
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-all-entity-types.php';
65
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-package-type.php';
66
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-25-0.php';
67
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-27-0.php';
68
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-27-1.php';
69
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-28-0.php';
70
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-32-0.php';
71
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-33-9.php';
72
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-36-0.php';
73
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-38-5.php';
74
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-39-1.php';
75
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-40-1.php';
76
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-40-2.php';
77
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-41-0.php';
78
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-42-0.php';
79
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-44-1.php';
80
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-44-4.php';
81
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-45-0.php';
82
-		require_once plugin_dir_path( __DIR__ ) . 'install/class-wordlift-install-3-45-1.php';
52
+		require_once plugin_dir_path(__DIR__).'install/class-wordlift-install.php';
53
+		require_once plugin_dir_path(__DIR__).'install/class-wordlift-install-1-0-0.php';
54
+		require_once plugin_dir_path(__DIR__).'install/class-wordlift-install-3-10-0.php';
55
+		require_once plugin_dir_path(__DIR__).'install/class-wordlift-install-3-12-0.php';
56
+		require_once plugin_dir_path(__DIR__).'install/class-wordlift-install-3-14-0.php';
57
+		require_once plugin_dir_path(__DIR__).'install/class-wordlift-install-3-15-0.php';
58
+		require_once plugin_dir_path(__DIR__).'install/class-wordlift-install-3-18-0.php';
59
+		require_once plugin_dir_path(__DIR__).'install/class-wordlift-install-3-18-3.php';
60
+		require_once plugin_dir_path(__DIR__).'install/class-wordlift-install-3-19-5.php';
61
+		require_once plugin_dir_path(__DIR__).'install/class-wordlift-install-3-20-0.php';
62
+		require_once plugin_dir_path(__DIR__).'install/class-wordlift-install-3-23-4.php';
63
+		require_once plugin_dir_path(__DIR__).'install/class-wordlift-install-3-24-2.php';
64
+		require_once plugin_dir_path(__DIR__).'install/class-wordlift-install-all-entity-types.php';
65
+		require_once plugin_dir_path(__DIR__).'install/class-wordlift-install-package-type.php';
66
+		require_once plugin_dir_path(__DIR__).'install/class-wordlift-install-3-25-0.php';
67
+		require_once plugin_dir_path(__DIR__).'install/class-wordlift-install-3-27-0.php';
68
+		require_once plugin_dir_path(__DIR__).'install/class-wordlift-install-3-27-1.php';
69
+		require_once plugin_dir_path(__DIR__).'install/class-wordlift-install-3-28-0.php';
70
+		require_once plugin_dir_path(__DIR__).'install/class-wordlift-install-3-32-0.php';
71
+		require_once plugin_dir_path(__DIR__).'install/class-wordlift-install-3-33-9.php';
72
+		require_once plugin_dir_path(__DIR__).'install/class-wordlift-install-3-36-0.php';
73
+		require_once plugin_dir_path(__DIR__).'install/class-wordlift-install-3-38-5.php';
74
+		require_once plugin_dir_path(__DIR__).'install/class-wordlift-install-3-39-1.php';
75
+		require_once plugin_dir_path(__DIR__).'install/class-wordlift-install-3-40-1.php';
76
+		require_once plugin_dir_path(__DIR__).'install/class-wordlift-install-3-40-2.php';
77
+		require_once plugin_dir_path(__DIR__).'install/class-wordlift-install-3-41-0.php';
78
+		require_once plugin_dir_path(__DIR__).'install/class-wordlift-install-3-42-0.php';
79
+		require_once plugin_dir_path(__DIR__).'install/class-wordlift-install-3-44-1.php';
80
+		require_once plugin_dir_path(__DIR__).'install/class-wordlift-install-3-44-4.php';
81
+		require_once plugin_dir_path(__DIR__).'install/class-wordlift-install-3-45-0.php';
82
+		require_once plugin_dir_path(__DIR__).'install/class-wordlift-install-3-45-1.php';
83 83
 
84 84
 		// Get the install services.
85 85
 		$this->installs = array(
@@ -130,9 +130,9 @@  discard block
 block discarded – undo
130 130
 		);
131 131
 		self::$instance = $this;
132 132
 
133
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
133
+		$this->log = Wordlift_Log_Service::get_logger(get_class());
134 134
 
135
-		add_action( 'init', array( $this, 'install' ) );
135
+		add_action('init', array($this, 'install'));
136 136
 
137 137
 	}
138 138
 
@@ -158,30 +158,30 @@  discard block
 block discarded – undo
158 158
 
159 159
 		$version = null;
160 160
 
161
-		if ( $this->install_required() && false === get_transient( '_wl_installing' ) ) {
162
-			set_transient( '_wl_installing', true, 5 * MINUTE_IN_SECONDS );
161
+		if ($this->install_required() && false === get_transient('_wl_installing')) {
162
+			set_transient('_wl_installing', true, 5 * MINUTE_IN_SECONDS);
163 163
 			/** @var Wordlift_Install $install */
164
-			foreach ( $this->installs as $install ) {
164
+			foreach ($this->installs as $install) {
165 165
 				// Get the install version.
166 166
 				$version = $install->get_version();
167 167
 
168
-				if ( version_compare( $version, $this->get_current_version(), '>' )
169
-					 || $install->must_install() ) {
170
-					$class_name = get_class( $install );
168
+				if (version_compare($version, $this->get_current_version(), '>')
169
+					 || $install->must_install()) {
170
+					$class_name = get_class($install);
171 171
 
172
-					$this->log->info( "Current version is {$this->get_current_version()}, installing $class_name..." );
172
+					$this->log->info("Current version is {$this->get_current_version()}, installing $class_name...");
173 173
 					// Install version.
174 174
 					$install->install();
175 175
 
176
-					$this->log->info( "$class_name installed." );
176
+					$this->log->info("$class_name installed.");
177 177
 
178 178
 					// Bump the version.
179
-					update_option( 'wl_db_version', $version );
179
+					update_option('wl_db_version', $version);
180 180
 				}
181 181
 			}
182 182
 
183 183
 			// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
184
-			@delete_transient( '_wl_installing' );
184
+			@delete_transient('_wl_installing');
185 185
 
186 186
 		}
187 187
 
@@ -190,12 +190,12 @@  discard block
 block discarded – undo
190 190
 	private function install_required() {
191 191
 
192 192
 		/** @var Wordlift_Install $install */
193
-		foreach ( $this->installs as $install ) {
193
+		foreach ($this->installs as $install) {
194 194
 			// Get the install version.
195 195
 			$version = $install->get_version();
196 196
 
197
-			if ( version_compare( $version, $this->get_current_version(), '>' )
198
-				 || $install->must_install() ) {
197
+			if (version_compare($version, $this->get_current_version(), '>')
198
+				 || $install->must_install()) {
199 199
 				return true;
200 200
 			}
201 201
 		}
@@ -209,7 +209,7 @@  discard block
 block discarded – undo
209 209
 	 * @return type
210 210
 	 */
211 211
 	private function get_current_version() {
212
-		return get_option( 'wl_db_version', '0.0.0' );
212
+		return get_option('wl_db_version', '0.0.0');
213 213
 	}
214 214
 
215 215
 }
Please login to merge, or discard this patch.
src/install/class-wordlift-install-3-45-1.php 1 patch
Indentation   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -7,12 +7,12 @@
 block discarded – undo
7 7
  */
8 8
 class Wordlift_Install_3_45_1 extends Wordlift_Install {
9 9
 
10
-	/**
11
-	 * {@inheritdoc}
12
-	 */
13
-	protected static $version = '3.45.1';
10
+    /**
11
+     * {@inheritdoc}
12
+     */
13
+    protected static $version = '3.45.1';
14 14
 
15
-	public function install() {
16
-		Ttl_Cache::flush_all();
17
-	}
15
+    public function install() {
16
+        Ttl_Cache::flush_all();
17
+    }
18 18
 }
Please login to merge, or discard this patch.
src/includes/class-wordlift-abstract-post-to-jsonld-converter.php 2 patches
Indentation   +477 added lines, -477 removed lines patch added patch discarded remove patch
@@ -24,114 +24,114 @@  discard block
 block discarded – undo
24 24
  */
25 25
 abstract class Wordlift_Abstract_Post_To_Jsonld_Converter implements Wordlift_Post_Converter {
26 26
 
27
-	/**
28
-	 * The JSON-LD context.
29
-	 *
30
-	 * @since 3.10.0
31
-	 */
32
-	const CONTEXT = 'http://schema.org';
33
-
34
-	/**
35
-	 * A {@link Wordlift_Entity_Type_Service} instance.
36
-	 *
37
-	 * @since  3.10.0
38
-	 * @access protected
39
-	 * @var \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance.
40
-	 */
41
-	protected $entity_type_service;
42
-
43
-	/**
44
-	 * A {@link Wordlift_User_Service} instance.
45
-	 *
46
-	 * @since  3.10.0
47
-	 * @access private
48
-	 * @var \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance.
49
-	 */
50
-	protected $user_service;
51
-
52
-	/**
53
-	 * A {@link Wordlift_Attachment_Service} instance.
54
-	 *
55
-	 * @since  3.10.0
56
-	 * @access private
57
-	 * @var \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance.
58
-	 */
59
-	protected $attachment_service;
60
-
61
-	/**
62
-	 * @var Wordlift_Property_Getter
63
-	 */
64
-	private $property_getter;
65
-
66
-	/**
67
-	 * Wordlift_Post_To_Jsonld_Converter constructor.
68
-	 *
69
-	 * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance.
70
-	 * @param \Wordlift_User_Service        $user_service A {@link Wordlift_User_Service} instance.
71
-	 * @param \Wordlift_Attachment_Service  $attachment_service A {@link Wordlift_Attachment_Service} instance.
72
-	 * @param \Wordlift_Property_Getter     $property_getter
73
-	 *
74
-	 * @since 3.10.0
75
-	 */
76
-	public function __construct( $entity_type_service, $user_service, $attachment_service, $property_getter ) {
77
-		$this->entity_type_service = $entity_type_service;
78
-		$this->user_service        = $user_service;
79
-		$this->attachment_service  = $attachment_service;
80
-		$this->property_getter     = $property_getter;
81
-	}
82
-
83
-	/**
84
-	 * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference
85
-	 * found while processing the post is set in the $references array.
86
-	 *
87
-	 * @param int $post_id The post id.
88
-	 * @param array $references An array of entity references.
89
-	 * @param array $relations
90
-	 *
91
-	 * @return array A JSON-LD array.
92
-	 * @since 3.10.0
93
-	 */
94
-	// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
95
-	public function convert( $post_id, &$references = array(), &$references_infos = array(), $relations = null ) {
96
-
97
-		// Get the post instance.
98
-		$post = get_post( $post_id );
99
-		if ( null === $post ) {
100
-			// Post not found.
101
-			return null;
102
-		}
103
-
104
-		// Get the post URI @id.
105
-		$id = Wordlift_Entity_Service::get_instance()->get_uri( $post->ID );
106
-		if ( $id === null ) {
107
-			$id = 'get_uri returned null, dataset is ' . wl_configuration_get_redlink_dataset_uri();
108
-		}
109
-
110
-		/*
27
+    /**
28
+     * The JSON-LD context.
29
+     *
30
+     * @since 3.10.0
31
+     */
32
+    const CONTEXT = 'http://schema.org';
33
+
34
+    /**
35
+     * A {@link Wordlift_Entity_Type_Service} instance.
36
+     *
37
+     * @since  3.10.0
38
+     * @access protected
39
+     * @var \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance.
40
+     */
41
+    protected $entity_type_service;
42
+
43
+    /**
44
+     * A {@link Wordlift_User_Service} instance.
45
+     *
46
+     * @since  3.10.0
47
+     * @access private
48
+     * @var \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance.
49
+     */
50
+    protected $user_service;
51
+
52
+    /**
53
+     * A {@link Wordlift_Attachment_Service} instance.
54
+     *
55
+     * @since  3.10.0
56
+     * @access private
57
+     * @var \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance.
58
+     */
59
+    protected $attachment_service;
60
+
61
+    /**
62
+     * @var Wordlift_Property_Getter
63
+     */
64
+    private $property_getter;
65
+
66
+    /**
67
+     * Wordlift_Post_To_Jsonld_Converter constructor.
68
+     *
69
+     * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance.
70
+     * @param \Wordlift_User_Service        $user_service A {@link Wordlift_User_Service} instance.
71
+     * @param \Wordlift_Attachment_Service  $attachment_service A {@link Wordlift_Attachment_Service} instance.
72
+     * @param \Wordlift_Property_Getter     $property_getter
73
+     *
74
+     * @since 3.10.0
75
+     */
76
+    public function __construct( $entity_type_service, $user_service, $attachment_service, $property_getter ) {
77
+        $this->entity_type_service = $entity_type_service;
78
+        $this->user_service        = $user_service;
79
+        $this->attachment_service  = $attachment_service;
80
+        $this->property_getter     = $property_getter;
81
+    }
82
+
83
+    /**
84
+     * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference
85
+     * found while processing the post is set in the $references array.
86
+     *
87
+     * @param int $post_id The post id.
88
+     * @param array $references An array of entity references.
89
+     * @param array $relations
90
+     *
91
+     * @return array A JSON-LD array.
92
+     * @since 3.10.0
93
+     */
94
+    // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
95
+    public function convert( $post_id, &$references = array(), &$references_infos = array(), $relations = null ) {
96
+
97
+        // Get the post instance.
98
+        $post = get_post( $post_id );
99
+        if ( null === $post ) {
100
+            // Post not found.
101
+            return null;
102
+        }
103
+
104
+        // Get the post URI @id.
105
+        $id = Wordlift_Entity_Service::get_instance()->get_uri( $post->ID );
106
+        if ( $id === null ) {
107
+            $id = 'get_uri returned null, dataset is ' . wl_configuration_get_redlink_dataset_uri();
108
+        }
109
+
110
+        /*
111 111
 		 * The `types` variable holds one or more entity types. The `type` variable isn't used anymore.
112 112
 		 *
113 113
 		 * @since 3.20.0 We support more than one entity type.
114 114
 		 *
115 115
 		 * @see https://github.com/insideout10/wordlift-plugin/issues/835
116 116
 		 */
117
-		// Get the entity @type. We consider `post` BlogPostings.
118
-		// $type = $this->entity_type_service->get( $post_id );
119
-		$types = $this->entity_type_service->get_names( $post_id );
120
-		$type  = self::make_one( $types );
121
-
122
-		// Prepare the response.
123
-		$jsonld = array(
124
-			'@context' => self::CONTEXT,
125
-			'@id'      => $id,
126
-			'@type'    => $type,
127
-		);
128
-
129
-		if ( post_type_supports( $post->post_type, 'excerpt' ) ) {
130
-			$jsonld['description'] = Wordlift_Post_Excerpt_Helper::get_text_excerpt( $post );
131
-		}
132
-
133
-		// Set the `mainEntityOfPage` property if the post has some contents.
134
-		/*
117
+        // Get the entity @type. We consider `post` BlogPostings.
118
+        // $type = $this->entity_type_service->get( $post_id );
119
+        $types = $this->entity_type_service->get_names( $post_id );
120
+        $type  = self::make_one( $types );
121
+
122
+        // Prepare the response.
123
+        $jsonld = array(
124
+            '@context' => self::CONTEXT,
125
+            '@id'      => $id,
126
+            '@type'    => $type,
127
+        );
128
+
129
+        if ( post_type_supports( $post->post_type, 'excerpt' ) ) {
130
+            $jsonld['description'] = Wordlift_Post_Excerpt_Helper::get_text_excerpt( $post );
131
+        }
132
+
133
+        // Set the `mainEntityOfPage` property if the post has some contents.
134
+        /*
135 135
 		 * Apply the `wl_post_content` filter, in case 3rd parties want to change the post content, e.g.
136 136
 		 * because the content is written elsewhere.
137 137
 		 *
@@ -139,384 +139,384 @@  discard block
 block discarded – undo
139 139
 		 *
140 140
 		 * @since 3.20.0
141 141
 		 */
142
-		$post_content = apply_filters( 'wl_post_content', $post->post_content, $post );
143
-		if ( ! empty( $post_content ) || in_array( 'Product', (array) $type, true ) ) {
144
-			// We're setting the `mainEntityOfPage` to signal which one is the
145
-			// main entity for the specified URL. It might be however that the
146
-			// post/page is actually about another specific entity. How WL deals
147
-			// with that hasn't been defined yet (see https://github.com/insideout10/wordlift-plugin/issues/451).
148
-			//
149
-			// See http://schema.org/mainEntityOfPage
150
-			//
151
-			// No need to specify `'@type' => 'WebPage'.
152
-			//
153
-			// See https://github.com/insideout10/wordlift-plugin/issues/451.
154
-			$jsonld['mainEntityOfPage'] = get_the_permalink( $post->ID );
155
-
156
-			/**
157
-			 * @see https://github.com/insideout10/wordlift-plugin/issues/1207
158
-			 *
159
-			 * @since 3.27.7
160
-			 */
161
-			if ( in_array(
162
-				$type,
163
-				array(
164
-					'Occupation',
165
-					'OccupationAggregationByEmployer',
166
-				),
167
-				true
168
-			) ) {
169
-				$jsonld['mainEntityOfPage'] = array(
170
-					'@type'        => 'WebPage',
171
-					'lastReviewed' => get_post_time( 'Y-m-d\TH:i:sP', true, $post, false ),
172
-				);
173
-			}
174
-		};
175
-
176
-		$this->set_images( $this->attachment_service, $post, $jsonld );
177
-
178
-		// Naveen: Get the entities referenced by this post and set it to the `references`
179
-		// array so that the caller can do further processing, such as printing out
180
-		// more of those references.
181
-		//
182
-		// David: you can't set this to the `references`, because `references` it's an array of post
183
-		// IDs and client are using this. As of 3.42.1 (2023-04-20) I am refactoring this to use a
184
-		// new variable $relations.
185
-		// ** @var Object_Relation_Service $object_relation_service */
186
-		// $object_relation_service      = Object_Relation_Service::get_instance();
187
-		// $references_without_locations = $object_relation_service->get_references( $post_id, Object_Type_Enum::POST );
188
-
189
-		$relation_service = Relation_Service::get_instance();
190
-		$content_id       = Wordpress_Content_Id::create_post( $post_id );
191
-		$relation_service->add_relations( $content_id, $relations );
192
-		$this->add_relations_to_references( $relations, $references );
193
-
194
-		$this->expand_references_with_location( $references );
195
-
196
-		return $jsonld;
197
-	}
198
-
199
-	public function expand_references_with_location( &$references ) {
200
-		// Bail out if ACF isn't available.
201
-		if ( ! function_exists( 'get_field' ) ) {
202
-			return;
203
-		}
204
-		$entity_type_service = Wordlift_Entity_Type_Service::get_instance();
205
-
206
-		// check if any of the references has the entity type set to Action, Event or organisation.
207
-		$references_with_location = array_filter(
208
-			$references,
209
-			function ( $post_id ) use ( $entity_type_service ) {
210
-				return ( $entity_type_service->has_entity_type( $post_id, 'http://schema.org/Action' )
211
-						 || $entity_type_service->has_entity_type( $post_id, 'http://schema.org/Event' )
212
-						 || $entity_type_service->has_entity_type( $post_id, 'http://schema.org/Organization' ) );
213
-			}
214
-		);
215
-
216
-		// If set, then get all the location ids and push them to references.
217
-		foreach ( $references_with_location as $post_id ) {
218
-			// We need to know whether the user chose a `Place` or smtg else (we're not interested in smtg else).
219
-			$target_field = get_field( 'field_wl_18117ac085e38f4fc3b077340a9548ee7c9fb9f5_radio', $post_id );
220
-			if ( 1 !== preg_match( '@^.*_as_place$@', $target_field ) ) {
221
-				continue;
222
-			}
223
-
224
-			// Get the target posts
225
-			$target_posts = get_field( $target_field, $post_id );
226
-			if ( ! is_array( $target_posts ) || empty( $target_posts ) ) {
227
-				continue;
228
-			}
229
-
230
-			// Add the target posts to the references
231
-			foreach ( $target_posts as $target_post ) {
232
-				if ( ! is_a( $target_post, '\WP_Post' ) ) {
233
-					continue;
234
-				}
235
-
236
-				$references[] = $target_post->ID;
237
-			}
238
-		}
239
-
240
-		// Location as WP custom meta
241
-		foreach ( $references_with_location as $post_id ) {
242
-			$post_location_ids = get_post_meta( $post_id, Wordlift_Schema_Service::FIELD_LOCATION );
243
-			$post_location_ids = is_array( $post_location_ids ) ? $post_location_ids : array();
244
-			// Only numeric values here pls
245
-			$post_location_ids = array_filter( $post_location_ids, 'is_numeric' );
246
-			$references        = array_merge( $post_location_ids, $references );
247
-		}
248
-
249
-	}
250
-
251
-	/**
252
-	 * Copies the Post relations to referenced post IDs (legacy).
253
-	 *
254
-	 * @param Relations $relations
255
-	 */
256
-	private function add_relations_to_references( $relations, &$references ) {
257
-		/** @var Relation $relation */
258
-		foreach ( $relations->toArray() as $relation ) {
259
-			$object    = $relation->get_object();
260
-			$object_id = $object->get_id();
261
-			if ( $object->get_type() === Object_Type_Enum::POST && ! in_array( $object_id, $references, true ) ) {
262
-				$references[] = $object_id;
263
-			}
264
-		}
265
-	}
266
-
267
-	// private function add_location_reference_objects() {
268
-	// array_reduce(
269
-	// $references_without_locations,
270
-	// function ( $carry, $reference ) use ( $entity_type_service ) {
271
-	// **
272
-	// * @var $reference Reference
273
-	// */
274
-	// @see https://schema.org/location for the schema.org types using the `location` property.
275
-	// if ( ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Action' )
276
-	// && ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Event' )
277
-	// && ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Organization' ) ) {
278
-	//
279
-	// return $carry;
280
-	// }
281
-	// $post_location_ids        = get_post_meta( $reference->get_id(), Wordlift_Schema_Service::FIELD_LOCATION );
282
-	// $post_location_references = array_map(
283
-	// function ( $post_id ) {
284
-	// return new Post_Reference( $post_id );
285
-	// },
286
-	// $post_location_ids
287
-	// );
288
-	//
289
-	// return array_merge( $carry, $post_location_references );
290
-	// },
291
-	// array()
292
-	// );
293
-	// }
294
-
295
-	/**
296
-	 * If the provided value starts with the schema.org context, we remove the schema.org
297
-	 * part since it is set with the '@context'.
298
-	 *
299
-	 * @param string $value The property value.
300
-	 *
301
-	 * @return string The property value without the context.
302
-	 * @since 3.10.0
303
-	 */
304
-	public function relative_to_context( $value ) {
305
-		return ! is_array( $value ) && 0 === strpos( $value, self::CONTEXT . '/' ) ? substr( $value, strlen( self::CONTEXT ) + 1 ) : $value;
306
-	}
307
-
308
-	/**
309
-	 * Set the images, by looking for embedded images, for images loaded via the
310
-	 * gallery and for the featured image.
311
-	 *
312
-	 * Uses the cache service to store the results of this function for a day.
313
-	 *
314
-	 * @param $attachment_service Wordlift_Attachment_Service
315
-	 * @param WP_Post                                        $post The target {@link WP_Post}.
316
-	 * @param array                                          $jsonld The JSON-LD array.
317
-	 *
318
-	 * @since 3.10.0
319
-	 */
320
-	public static function set_images( $attachment_service, $post, &$jsonld ) {
321
-
322
-		// Prepare the attachment ids array.
323
-		$ids = array();
324
-
325
-		// Set the thumbnail id as first attachment id, if any.
326
-		$thumbnail_id = get_post_thumbnail_id( $post->ID );
327
-		if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) {
328
-			$ids[] = $thumbnail_id;
329
-		}
330
-
331
-		// For the time being the following is being removed since the query
332
-		// initiated by `get_image_embeds` is consuming lots of CPU.
333
-		//
334
-		// See https://github.com/insideout10/wordlift-plugin/issues/689.
335
-		//
336
-		// Get the embeds, removing existing ids.
337
-		// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
338
-		if ( apply_filters( 'wl_feature__enable__image-embeds', false ) ) {
339
-			$embeds = array_diff( $attachment_service->get_image_embeds( $post->post_content ), $ids );
340
-		} else {
341
-			$embeds = array();
342
-		}
343
-
344
-		// Get the gallery, removing existing ids.
345
-		$gallery = array_diff( $attachment_service->get_gallery( $post ), $ids, $embeds );
346
-
347
-		// Map the attachment ids to images' data structured for schema.org use.
348
-		$images_with_sizes = array_filter(
349
-			array_reduce(
350
-				array_merge( $ids, $embeds, $gallery ),
351
-				function ( $carry, $item ) {
352
-					/*
142
+        $post_content = apply_filters( 'wl_post_content', $post->post_content, $post );
143
+        if ( ! empty( $post_content ) || in_array( 'Product', (array) $type, true ) ) {
144
+            // We're setting the `mainEntityOfPage` to signal which one is the
145
+            // main entity for the specified URL. It might be however that the
146
+            // post/page is actually about another specific entity. How WL deals
147
+            // with that hasn't been defined yet (see https://github.com/insideout10/wordlift-plugin/issues/451).
148
+            //
149
+            // See http://schema.org/mainEntityOfPage
150
+            //
151
+            // No need to specify `'@type' => 'WebPage'.
152
+            //
153
+            // See https://github.com/insideout10/wordlift-plugin/issues/451.
154
+            $jsonld['mainEntityOfPage'] = get_the_permalink( $post->ID );
155
+
156
+            /**
157
+             * @see https://github.com/insideout10/wordlift-plugin/issues/1207
158
+             *
159
+             * @since 3.27.7
160
+             */
161
+            if ( in_array(
162
+                $type,
163
+                array(
164
+                    'Occupation',
165
+                    'OccupationAggregationByEmployer',
166
+                ),
167
+                true
168
+            ) ) {
169
+                $jsonld['mainEntityOfPage'] = array(
170
+                    '@type'        => 'WebPage',
171
+                    'lastReviewed' => get_post_time( 'Y-m-d\TH:i:sP', true, $post, false ),
172
+                );
173
+            }
174
+        };
175
+
176
+        $this->set_images( $this->attachment_service, $post, $jsonld );
177
+
178
+        // Naveen: Get the entities referenced by this post and set it to the `references`
179
+        // array so that the caller can do further processing, such as printing out
180
+        // more of those references.
181
+        //
182
+        // David: you can't set this to the `references`, because `references` it's an array of post
183
+        // IDs and client are using this. As of 3.42.1 (2023-04-20) I am refactoring this to use a
184
+        // new variable $relations.
185
+        // ** @var Object_Relation_Service $object_relation_service */
186
+        // $object_relation_service      = Object_Relation_Service::get_instance();
187
+        // $references_without_locations = $object_relation_service->get_references( $post_id, Object_Type_Enum::POST );
188
+
189
+        $relation_service = Relation_Service::get_instance();
190
+        $content_id       = Wordpress_Content_Id::create_post( $post_id );
191
+        $relation_service->add_relations( $content_id, $relations );
192
+        $this->add_relations_to_references( $relations, $references );
193
+
194
+        $this->expand_references_with_location( $references );
195
+
196
+        return $jsonld;
197
+    }
198
+
199
+    public function expand_references_with_location( &$references ) {
200
+        // Bail out if ACF isn't available.
201
+        if ( ! function_exists( 'get_field' ) ) {
202
+            return;
203
+        }
204
+        $entity_type_service = Wordlift_Entity_Type_Service::get_instance();
205
+
206
+        // check if any of the references has the entity type set to Action, Event or organisation.
207
+        $references_with_location = array_filter(
208
+            $references,
209
+            function ( $post_id ) use ( $entity_type_service ) {
210
+                return ( $entity_type_service->has_entity_type( $post_id, 'http://schema.org/Action' )
211
+                         || $entity_type_service->has_entity_type( $post_id, 'http://schema.org/Event' )
212
+                         || $entity_type_service->has_entity_type( $post_id, 'http://schema.org/Organization' ) );
213
+            }
214
+        );
215
+
216
+        // If set, then get all the location ids and push them to references.
217
+        foreach ( $references_with_location as $post_id ) {
218
+            // We need to know whether the user chose a `Place` or smtg else (we're not interested in smtg else).
219
+            $target_field = get_field( 'field_wl_18117ac085e38f4fc3b077340a9548ee7c9fb9f5_radio', $post_id );
220
+            if ( 1 !== preg_match( '@^.*_as_place$@', $target_field ) ) {
221
+                continue;
222
+            }
223
+
224
+            // Get the target posts
225
+            $target_posts = get_field( $target_field, $post_id );
226
+            if ( ! is_array( $target_posts ) || empty( $target_posts ) ) {
227
+                continue;
228
+            }
229
+
230
+            // Add the target posts to the references
231
+            foreach ( $target_posts as $target_post ) {
232
+                if ( ! is_a( $target_post, '\WP_Post' ) ) {
233
+                    continue;
234
+                }
235
+
236
+                $references[] = $target_post->ID;
237
+            }
238
+        }
239
+
240
+        // Location as WP custom meta
241
+        foreach ( $references_with_location as $post_id ) {
242
+            $post_location_ids = get_post_meta( $post_id, Wordlift_Schema_Service::FIELD_LOCATION );
243
+            $post_location_ids = is_array( $post_location_ids ) ? $post_location_ids : array();
244
+            // Only numeric values here pls
245
+            $post_location_ids = array_filter( $post_location_ids, 'is_numeric' );
246
+            $references        = array_merge( $post_location_ids, $references );
247
+        }
248
+
249
+    }
250
+
251
+    /**
252
+     * Copies the Post relations to referenced post IDs (legacy).
253
+     *
254
+     * @param Relations $relations
255
+     */
256
+    private function add_relations_to_references( $relations, &$references ) {
257
+        /** @var Relation $relation */
258
+        foreach ( $relations->toArray() as $relation ) {
259
+            $object    = $relation->get_object();
260
+            $object_id = $object->get_id();
261
+            if ( $object->get_type() === Object_Type_Enum::POST && ! in_array( $object_id, $references, true ) ) {
262
+                $references[] = $object_id;
263
+            }
264
+        }
265
+    }
266
+
267
+    // private function add_location_reference_objects() {
268
+    // array_reduce(
269
+    // $references_without_locations,
270
+    // function ( $carry, $reference ) use ( $entity_type_service ) {
271
+    // **
272
+    // * @var $reference Reference
273
+    // */
274
+    // @see https://schema.org/location for the schema.org types using the `location` property.
275
+    // if ( ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Action' )
276
+    // && ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Event' )
277
+    // && ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Organization' ) ) {
278
+    //
279
+    // return $carry;
280
+    // }
281
+    // $post_location_ids        = get_post_meta( $reference->get_id(), Wordlift_Schema_Service::FIELD_LOCATION );
282
+    // $post_location_references = array_map(
283
+    // function ( $post_id ) {
284
+    // return new Post_Reference( $post_id );
285
+    // },
286
+    // $post_location_ids
287
+    // );
288
+    //
289
+    // return array_merge( $carry, $post_location_references );
290
+    // },
291
+    // array()
292
+    // );
293
+    // }
294
+
295
+    /**
296
+     * If the provided value starts with the schema.org context, we remove the schema.org
297
+     * part since it is set with the '@context'.
298
+     *
299
+     * @param string $value The property value.
300
+     *
301
+     * @return string The property value without the context.
302
+     * @since 3.10.0
303
+     */
304
+    public function relative_to_context( $value ) {
305
+        return ! is_array( $value ) && 0 === strpos( $value, self::CONTEXT . '/' ) ? substr( $value, strlen( self::CONTEXT ) + 1 ) : $value;
306
+    }
307
+
308
+    /**
309
+     * Set the images, by looking for embedded images, for images loaded via the
310
+     * gallery and for the featured image.
311
+     *
312
+     * Uses the cache service to store the results of this function for a day.
313
+     *
314
+     * @param $attachment_service Wordlift_Attachment_Service
315
+     * @param WP_Post                                        $post The target {@link WP_Post}.
316
+     * @param array                                          $jsonld The JSON-LD array.
317
+     *
318
+     * @since 3.10.0
319
+     */
320
+    public static function set_images( $attachment_service, $post, &$jsonld ) {
321
+
322
+        // Prepare the attachment ids array.
323
+        $ids = array();
324
+
325
+        // Set the thumbnail id as first attachment id, if any.
326
+        $thumbnail_id = get_post_thumbnail_id( $post->ID );
327
+        if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) {
328
+            $ids[] = $thumbnail_id;
329
+        }
330
+
331
+        // For the time being the following is being removed since the query
332
+        // initiated by `get_image_embeds` is consuming lots of CPU.
333
+        //
334
+        // See https://github.com/insideout10/wordlift-plugin/issues/689.
335
+        //
336
+        // Get the embeds, removing existing ids.
337
+        // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
338
+        if ( apply_filters( 'wl_feature__enable__image-embeds', false ) ) {
339
+            $embeds = array_diff( $attachment_service->get_image_embeds( $post->post_content ), $ids );
340
+        } else {
341
+            $embeds = array();
342
+        }
343
+
344
+        // Get the gallery, removing existing ids.
345
+        $gallery = array_diff( $attachment_service->get_gallery( $post ), $ids, $embeds );
346
+
347
+        // Map the attachment ids to images' data structured for schema.org use.
348
+        $images_with_sizes = array_filter(
349
+            array_reduce(
350
+                array_merge( $ids, $embeds, $gallery ),
351
+                function ( $carry, $item ) {
352
+                    /*
353 353
 					* @todo: we're not sure that we're getting attachment data here, we
354 354
 					* should filter `false`s.
355 355
 					*/
356 356
 
357
-					$sources = array_merge(
358
-						Wordlift_Image_Service::get_sources( $item ),
359
-						array( wp_get_attachment_image_src( $item, 'full' ) )
360
-					);
361
-
362
-					$sources_with_image = array_filter(
363
-						$sources,
364
-						function ( $source ) {
365
-							return ! empty( $source[0] );
366
-						}
367
-					);
368
-
369
-					// Get the attachment data.
370
-					// $attachment = wp_get_attachment_image_src( $item, 'full' );
371
-
372
-					// var_dump( array( "sources-$item" => Wordlift_Image_Service::get_sources( $item ) ) );
373
-
374
-					// Bail if image is not found.
375
-					// In some cases, you can delete the image from the database
376
-					// or from uploads dir, but the image id still exists as featured image
377
-					// or in [gallery] shortcode.
378
-					// if ( empty( $attachment[0] ) ) {
379
-					if ( empty( $sources_with_image ) ) {
380
-						return $carry;
381
-					}
382
-
383
-					// Merge the arrays.
384
-					return array_merge(
385
-						$carry,
386
-						$sources_with_image
387
-					);
388
-				},
389
-				// Initial array.
390
-
391
-				array()
392
-			)
393
-		);
394
-
395
-		// Refactor data as per schema.org specifications.
396
-		$images = array_map(
397
-			function ( $attachment ) {
398
-				return Wordlift_Abstract_Post_To_Jsonld_Converter::set_image_size(
399
-					array(
400
-						'@type' => 'ImageObject',
401
-						'url'   => $attachment[0],
402
-					),
403
-					$attachment
404
-				);
405
-			},
406
-			$images_with_sizes
407
-		);
408
-
409
-		// Add images if present.
410
-		if ( 0 < count( $images ) ) {
411
-			$jsonld['image'] = $images;
412
-		}
413
-
414
-	}
415
-
416
-	/**
417
-	 * If the provided array of values contains only one value, then one single
418
-	 * value is returned, otherwise the original array is returned.
419
-	 *
420
-	 * @param array $value An array of values.
421
-	 *
422
-	 * @return mixed|array A single value or the original array.
423
-	 * @since 3.20.0 The function has been moved from {@link Wordlift_Entity_Post_To_Jsonld_Converter} to
424
-	 *  {@link Wordlift_Abstract_Post_To_Jsonld_Converter}.
425
-	 * @since  3.8.0
426
-	 * @access private
427
-	 */
428
-	protected static function make_one( $value ) {
429
-
430
-		return 1 === count( $value ) ? $value[0] : $value;
431
-	}
432
-
433
-	/**
434
-	 * Process the provided array by adding the width / height if the values
435
-	 * are available and are greater than 0.
436
-	 *
437
-	 * @param array $image The `ImageObject` array.
438
-	 * @param array $attachment The attachment array.
439
-	 *
440
-	 * @return array The enriched `ImageObject` array.
441
-	 * @since 3.14.0
442
-	 */
443
-	public static function set_image_size( $image, $attachment ) {
444
-
445
-		// If you specify a "width" or "height" value you should leave out
446
-		// 'px'. For example: "width":"4608px" should be "width":"4608".
447
-		//
448
-		// See https://github.com/insideout10/wordlift-plugin/issues/451.
449
-		if ( isset( $attachment[1] ) && is_numeric( $attachment[1] ) && 0 < $attachment[1] ) {
450
-			$image['width'] = $attachment[1];
451
-		}
452
-
453
-		if ( isset( $attachment[2] ) && is_numeric( $attachment[2] ) && 0 < $attachment[2] ) {
454
-			$image['height'] = $attachment[2];
455
-		}
456
-
457
-		return $image;
458
-	}
459
-
460
-	/**
461
-	 * Add data to the JSON-LD using the `custom_fields` array which contains the definitions of property
462
-	 * for the post entity type.
463
-	 *
464
-	 * @param array   $jsonld The JSON-LD array.
465
-	 * @param array   $fields The entity types field array.
466
-	 * @param WP_Post $post The target {@link WP_Post} instance.
467
-	 * @param array   $references The references array.
468
-	 *
469
-	 * @since 3.20.0 This code moved from the above function `convert`, used for entity types defined in
470
-	 *  the {@link Wordlift_Schema_Service} class.
471
-	 */
472
-	protected function process_type_custom_fields( &$jsonld, $fields, $post, &$references, &$references_infos ) {
473
-
474
-		// Set a reference to use in closures.
475
-		$converter = $this;
476
-
477
-		// Try each field on the entity.
478
-		foreach ( $fields as $key => $value ) {
479
-
480
-			// Get the predicate.
481
-			$name = $this->relative_to_context( $value['predicate'] );
482
-
483
-			// Get the value, the property service will get the right extractor
484
-			// for that property.
485
-			$value = $this->property_getter->get( $post->ID, $key, Object_Type_Enum::POST );
486
-
487
-			if ( empty( $value ) ) {
488
-				continue;
489
-			}
490
-
491
-			// Map the value to the property name.
492
-			// If we got an array with just one value, we return that one value.
493
-			// If we got a Wordlift_Property_Entity_Reference we get the URL.
494
-			$jsonld[ $name ] = self::make_one(
495
-				array_map(
496
-					function ( $item ) use ( $converter, &$references, &$references_infos ) {
497
-
498
-						if ( $item instanceof Wordlift_Property_Entity_Reference ) {
499
-
500
-							$url = $item->get_url();
501
-
502
-							// The refactored converters require the entity id.
503
-							// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
504
-							$references[] = $item->get_id();
505
-
506
-							// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
507
-							$references_infos[] = array( 'reference' => $item );
508
-
509
-							return array( '@id' => $url );
510
-						}
511
-
512
-						return $converter->relative_to_context( $item );
513
-					},
514
-					$value
515
-				)
516
-			);
517
-
518
-		}
519
-
520
-	}
357
+                    $sources = array_merge(
358
+                        Wordlift_Image_Service::get_sources( $item ),
359
+                        array( wp_get_attachment_image_src( $item, 'full' ) )
360
+                    );
361
+
362
+                    $sources_with_image = array_filter(
363
+                        $sources,
364
+                        function ( $source ) {
365
+                            return ! empty( $source[0] );
366
+                        }
367
+                    );
368
+
369
+                    // Get the attachment data.
370
+                    // $attachment = wp_get_attachment_image_src( $item, 'full' );
371
+
372
+                    // var_dump( array( "sources-$item" => Wordlift_Image_Service::get_sources( $item ) ) );
373
+
374
+                    // Bail if image is not found.
375
+                    // In some cases, you can delete the image from the database
376
+                    // or from uploads dir, but the image id still exists as featured image
377
+                    // or in [gallery] shortcode.
378
+                    // if ( empty( $attachment[0] ) ) {
379
+                    if ( empty( $sources_with_image ) ) {
380
+                        return $carry;
381
+                    }
382
+
383
+                    // Merge the arrays.
384
+                    return array_merge(
385
+                        $carry,
386
+                        $sources_with_image
387
+                    );
388
+                },
389
+                // Initial array.
390
+
391
+                array()
392
+            )
393
+        );
394
+
395
+        // Refactor data as per schema.org specifications.
396
+        $images = array_map(
397
+            function ( $attachment ) {
398
+                return Wordlift_Abstract_Post_To_Jsonld_Converter::set_image_size(
399
+                    array(
400
+                        '@type' => 'ImageObject',
401
+                        'url'   => $attachment[0],
402
+                    ),
403
+                    $attachment
404
+                );
405
+            },
406
+            $images_with_sizes
407
+        );
408
+
409
+        // Add images if present.
410
+        if ( 0 < count( $images ) ) {
411
+            $jsonld['image'] = $images;
412
+        }
413
+
414
+    }
415
+
416
+    /**
417
+     * If the provided array of values contains only one value, then one single
418
+     * value is returned, otherwise the original array is returned.
419
+     *
420
+     * @param array $value An array of values.
421
+     *
422
+     * @return mixed|array A single value or the original array.
423
+     * @since 3.20.0 The function has been moved from {@link Wordlift_Entity_Post_To_Jsonld_Converter} to
424
+     *  {@link Wordlift_Abstract_Post_To_Jsonld_Converter}.
425
+     * @since  3.8.0
426
+     * @access private
427
+     */
428
+    protected static function make_one( $value ) {
429
+
430
+        return 1 === count( $value ) ? $value[0] : $value;
431
+    }
432
+
433
+    /**
434
+     * Process the provided array by adding the width / height if the values
435
+     * are available and are greater than 0.
436
+     *
437
+     * @param array $image The `ImageObject` array.
438
+     * @param array $attachment The attachment array.
439
+     *
440
+     * @return array The enriched `ImageObject` array.
441
+     * @since 3.14.0
442
+     */
443
+    public static function set_image_size( $image, $attachment ) {
444
+
445
+        // If you specify a "width" or "height" value you should leave out
446
+        // 'px'. For example: "width":"4608px" should be "width":"4608".
447
+        //
448
+        // See https://github.com/insideout10/wordlift-plugin/issues/451.
449
+        if ( isset( $attachment[1] ) && is_numeric( $attachment[1] ) && 0 < $attachment[1] ) {
450
+            $image['width'] = $attachment[1];
451
+        }
452
+
453
+        if ( isset( $attachment[2] ) && is_numeric( $attachment[2] ) && 0 < $attachment[2] ) {
454
+            $image['height'] = $attachment[2];
455
+        }
456
+
457
+        return $image;
458
+    }
459
+
460
+    /**
461
+     * Add data to the JSON-LD using the `custom_fields` array which contains the definitions of property
462
+     * for the post entity type.
463
+     *
464
+     * @param array   $jsonld The JSON-LD array.
465
+     * @param array   $fields The entity types field array.
466
+     * @param WP_Post $post The target {@link WP_Post} instance.
467
+     * @param array   $references The references array.
468
+     *
469
+     * @since 3.20.0 This code moved from the above function `convert`, used for entity types defined in
470
+     *  the {@link Wordlift_Schema_Service} class.
471
+     */
472
+    protected function process_type_custom_fields( &$jsonld, $fields, $post, &$references, &$references_infos ) {
473
+
474
+        // Set a reference to use in closures.
475
+        $converter = $this;
476
+
477
+        // Try each field on the entity.
478
+        foreach ( $fields as $key => $value ) {
479
+
480
+            // Get the predicate.
481
+            $name = $this->relative_to_context( $value['predicate'] );
482
+
483
+            // Get the value, the property service will get the right extractor
484
+            // for that property.
485
+            $value = $this->property_getter->get( $post->ID, $key, Object_Type_Enum::POST );
486
+
487
+            if ( empty( $value ) ) {
488
+                continue;
489
+            }
490
+
491
+            // Map the value to the property name.
492
+            // If we got an array with just one value, we return that one value.
493
+            // If we got a Wordlift_Property_Entity_Reference we get the URL.
494
+            $jsonld[ $name ] = self::make_one(
495
+                array_map(
496
+                    function ( $item ) use ( $converter, &$references, &$references_infos ) {
497
+
498
+                        if ( $item instanceof Wordlift_Property_Entity_Reference ) {
499
+
500
+                            $url = $item->get_url();
501
+
502
+                            // The refactored converters require the entity id.
503
+                            // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
504
+                            $references[] = $item->get_id();
505
+
506
+                            // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
507
+                            $references_infos[] = array( 'reference' => $item );
508
+
509
+                            return array( '@id' => $url );
510
+                        }
511
+
512
+                        return $converter->relative_to_context( $item );
513
+                    },
514
+                    $value
515
+                )
516
+            );
517
+
518
+        }
519
+
520
+    }
521 521
 
522 522
 }
Please login to merge, or discard this patch.
Spacing   +76 added lines, -76 removed lines patch added patch discarded remove patch
@@ -73,7 +73,7 @@  discard block
 block discarded – undo
73 73
 	 *
74 74
 	 * @since 3.10.0
75 75
 	 */
76
-	public function __construct( $entity_type_service, $user_service, $attachment_service, $property_getter ) {
76
+	public function __construct($entity_type_service, $user_service, $attachment_service, $property_getter) {
77 77
 		$this->entity_type_service = $entity_type_service;
78 78
 		$this->user_service        = $user_service;
79 79
 		$this->attachment_service  = $attachment_service;
@@ -92,19 +92,19 @@  discard block
 block discarded – undo
92 92
 	 * @since 3.10.0
93 93
 	 */
94 94
 	// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
95
-	public function convert( $post_id, &$references = array(), &$references_infos = array(), $relations = null ) {
95
+	public function convert($post_id, &$references = array(), &$references_infos = array(), $relations = null) {
96 96
 
97 97
 		// Get the post instance.
98
-		$post = get_post( $post_id );
99
-		if ( null === $post ) {
98
+		$post = get_post($post_id);
99
+		if (null === $post) {
100 100
 			// Post not found.
101 101
 			return null;
102 102
 		}
103 103
 
104 104
 		// Get the post URI @id.
105
-		$id = Wordlift_Entity_Service::get_instance()->get_uri( $post->ID );
106
-		if ( $id === null ) {
107
-			$id = 'get_uri returned null, dataset is ' . wl_configuration_get_redlink_dataset_uri();
105
+		$id = Wordlift_Entity_Service::get_instance()->get_uri($post->ID);
106
+		if ($id === null) {
107
+			$id = 'get_uri returned null, dataset is '.wl_configuration_get_redlink_dataset_uri();
108 108
 		}
109 109
 
110 110
 		/*
@@ -116,8 +116,8 @@  discard block
 block discarded – undo
116 116
 		 */
117 117
 		// Get the entity @type. We consider `post` BlogPostings.
118 118
 		// $type = $this->entity_type_service->get( $post_id );
119
-		$types = $this->entity_type_service->get_names( $post_id );
120
-		$type  = self::make_one( $types );
119
+		$types = $this->entity_type_service->get_names($post_id);
120
+		$type  = self::make_one($types);
121 121
 
122 122
 		// Prepare the response.
123 123
 		$jsonld = array(
@@ -126,8 +126,8 @@  discard block
 block discarded – undo
126 126
 			'@type'    => $type,
127 127
 		);
128 128
 
129
-		if ( post_type_supports( $post->post_type, 'excerpt' ) ) {
130
-			$jsonld['description'] = Wordlift_Post_Excerpt_Helper::get_text_excerpt( $post );
129
+		if (post_type_supports($post->post_type, 'excerpt')) {
130
+			$jsonld['description'] = Wordlift_Post_Excerpt_Helper::get_text_excerpt($post);
131 131
 		}
132 132
 
133 133
 		// Set the `mainEntityOfPage` property if the post has some contents.
@@ -139,8 +139,8 @@  discard block
 block discarded – undo
139 139
 		 *
140 140
 		 * @since 3.20.0
141 141
 		 */
142
-		$post_content = apply_filters( 'wl_post_content', $post->post_content, $post );
143
-		if ( ! empty( $post_content ) || in_array( 'Product', (array) $type, true ) ) {
142
+		$post_content = apply_filters('wl_post_content', $post->post_content, $post);
143
+		if ( ! empty($post_content) || in_array('Product', (array) $type, true)) {
144 144
 			// We're setting the `mainEntityOfPage` to signal which one is the
145 145
 			// main entity for the specified URL. It might be however that the
146 146
 			// post/page is actually about another specific entity. How WL deals
@@ -151,29 +151,29 @@  discard block
 block discarded – undo
151 151
 			// No need to specify `'@type' => 'WebPage'.
152 152
 			//
153 153
 			// See https://github.com/insideout10/wordlift-plugin/issues/451.
154
-			$jsonld['mainEntityOfPage'] = get_the_permalink( $post->ID );
154
+			$jsonld['mainEntityOfPage'] = get_the_permalink($post->ID);
155 155
 
156 156
 			/**
157 157
 			 * @see https://github.com/insideout10/wordlift-plugin/issues/1207
158 158
 			 *
159 159
 			 * @since 3.27.7
160 160
 			 */
161
-			if ( in_array(
161
+			if (in_array(
162 162
 				$type,
163 163
 				array(
164 164
 					'Occupation',
165 165
 					'OccupationAggregationByEmployer',
166 166
 				),
167 167
 				true
168
-			) ) {
168
+			)) {
169 169
 				$jsonld['mainEntityOfPage'] = array(
170 170
 					'@type'        => 'WebPage',
171
-					'lastReviewed' => get_post_time( 'Y-m-d\TH:i:sP', true, $post, false ),
171
+					'lastReviewed' => get_post_time('Y-m-d\TH:i:sP', true, $post, false),
172 172
 				);
173 173
 			}
174 174
 		};
175 175
 
176
-		$this->set_images( $this->attachment_service, $post, $jsonld );
176
+		$this->set_images($this->attachment_service, $post, $jsonld);
177 177
 
178 178
 		// Naveen: Get the entities referenced by this post and set it to the `references`
179 179
 		// array so that the caller can do further processing, such as printing out
@@ -187,18 +187,18 @@  discard block
 block discarded – undo
187 187
 		// $references_without_locations = $object_relation_service->get_references( $post_id, Object_Type_Enum::POST );
188 188
 
189 189
 		$relation_service = Relation_Service::get_instance();
190
-		$content_id       = Wordpress_Content_Id::create_post( $post_id );
191
-		$relation_service->add_relations( $content_id, $relations );
192
-		$this->add_relations_to_references( $relations, $references );
190
+		$content_id       = Wordpress_Content_Id::create_post($post_id);
191
+		$relation_service->add_relations($content_id, $relations);
192
+		$this->add_relations_to_references($relations, $references);
193 193
 
194
-		$this->expand_references_with_location( $references );
194
+		$this->expand_references_with_location($references);
195 195
 
196 196
 		return $jsonld;
197 197
 	}
198 198
 
199
-	public function expand_references_with_location( &$references ) {
199
+	public function expand_references_with_location(&$references) {
200 200
 		// Bail out if ACF isn't available.
201
-		if ( ! function_exists( 'get_field' ) ) {
201
+		if ( ! function_exists('get_field')) {
202 202
 			return;
203 203
 		}
204 204
 		$entity_type_service = Wordlift_Entity_Type_Service::get_instance();
@@ -206,30 +206,30 @@  discard block
 block discarded – undo
206 206
 		// check if any of the references has the entity type set to Action, Event or organisation.
207 207
 		$references_with_location = array_filter(
208 208
 			$references,
209
-			function ( $post_id ) use ( $entity_type_service ) {
210
-				return ( $entity_type_service->has_entity_type( $post_id, 'http://schema.org/Action' )
211
-						 || $entity_type_service->has_entity_type( $post_id, 'http://schema.org/Event' )
212
-						 || $entity_type_service->has_entity_type( $post_id, 'http://schema.org/Organization' ) );
209
+			function($post_id) use ($entity_type_service) {
210
+				return ($entity_type_service->has_entity_type($post_id, 'http://schema.org/Action')
211
+						 || $entity_type_service->has_entity_type($post_id, 'http://schema.org/Event')
212
+						 || $entity_type_service->has_entity_type($post_id, 'http://schema.org/Organization'));
213 213
 			}
214 214
 		);
215 215
 
216 216
 		// If set, then get all the location ids and push them to references.
217
-		foreach ( $references_with_location as $post_id ) {
217
+		foreach ($references_with_location as $post_id) {
218 218
 			// We need to know whether the user chose a `Place` or smtg else (we're not interested in smtg else).
219
-			$target_field = get_field( 'field_wl_18117ac085e38f4fc3b077340a9548ee7c9fb9f5_radio', $post_id );
220
-			if ( 1 !== preg_match( '@^.*_as_place$@', $target_field ) ) {
219
+			$target_field = get_field('field_wl_18117ac085e38f4fc3b077340a9548ee7c9fb9f5_radio', $post_id);
220
+			if (1 !== preg_match('@^.*_as_place$@', $target_field)) {
221 221
 				continue;
222 222
 			}
223 223
 
224 224
 			// Get the target posts
225
-			$target_posts = get_field( $target_field, $post_id );
226
-			if ( ! is_array( $target_posts ) || empty( $target_posts ) ) {
225
+			$target_posts = get_field($target_field, $post_id);
226
+			if ( ! is_array($target_posts) || empty($target_posts)) {
227 227
 				continue;
228 228
 			}
229 229
 
230 230
 			// Add the target posts to the references
231
-			foreach ( $target_posts as $target_post ) {
232
-				if ( ! is_a( $target_post, '\WP_Post' ) ) {
231
+			foreach ($target_posts as $target_post) {
232
+				if ( ! is_a($target_post, '\WP_Post')) {
233 233
 					continue;
234 234
 				}
235 235
 
@@ -238,12 +238,12 @@  discard block
 block discarded – undo
238 238
 		}
239 239
 
240 240
 		// Location as WP custom meta
241
-		foreach ( $references_with_location as $post_id ) {
242
-			$post_location_ids = get_post_meta( $post_id, Wordlift_Schema_Service::FIELD_LOCATION );
243
-			$post_location_ids = is_array( $post_location_ids ) ? $post_location_ids : array();
241
+		foreach ($references_with_location as $post_id) {
242
+			$post_location_ids = get_post_meta($post_id, Wordlift_Schema_Service::FIELD_LOCATION);
243
+			$post_location_ids = is_array($post_location_ids) ? $post_location_ids : array();
244 244
 			// Only numeric values here pls
245
-			$post_location_ids = array_filter( $post_location_ids, 'is_numeric' );
246
-			$references        = array_merge( $post_location_ids, $references );
245
+			$post_location_ids = array_filter($post_location_ids, 'is_numeric');
246
+			$references        = array_merge($post_location_ids, $references);
247 247
 		}
248 248
 
249 249
 	}
@@ -253,12 +253,12 @@  discard block
 block discarded – undo
253 253
 	 *
254 254
 	 * @param Relations $relations
255 255
 	 */
256
-	private function add_relations_to_references( $relations, &$references ) {
256
+	private function add_relations_to_references($relations, &$references) {
257 257
 		/** @var Relation $relation */
258
-		foreach ( $relations->toArray() as $relation ) {
258
+		foreach ($relations->toArray() as $relation) {
259 259
 			$object    = $relation->get_object();
260 260
 			$object_id = $object->get_id();
261
-			if ( $object->get_type() === Object_Type_Enum::POST && ! in_array( $object_id, $references, true ) ) {
261
+			if ($object->get_type() === Object_Type_Enum::POST && ! in_array($object_id, $references, true)) {
262 262
 				$references[] = $object_id;
263 263
 			}
264 264
 		}
@@ -301,8 +301,8 @@  discard block
 block discarded – undo
301 301
 	 * @return string The property value without the context.
302 302
 	 * @since 3.10.0
303 303
 	 */
304
-	public function relative_to_context( $value ) {
305
-		return ! is_array( $value ) && 0 === strpos( $value, self::CONTEXT . '/' ) ? substr( $value, strlen( self::CONTEXT ) + 1 ) : $value;
304
+	public function relative_to_context($value) {
305
+		return ! is_array($value) && 0 === strpos($value, self::CONTEXT.'/') ? substr($value, strlen(self::CONTEXT) + 1) : $value;
306 306
 	}
307 307
 
308 308
 	/**
@@ -317,14 +317,14 @@  discard block
 block discarded – undo
317 317
 	 *
318 318
 	 * @since 3.10.0
319 319
 	 */
320
-	public static function set_images( $attachment_service, $post, &$jsonld ) {
320
+	public static function set_images($attachment_service, $post, &$jsonld) {
321 321
 
322 322
 		// Prepare the attachment ids array.
323 323
 		$ids = array();
324 324
 
325 325
 		// Set the thumbnail id as first attachment id, if any.
326
-		$thumbnail_id = get_post_thumbnail_id( $post->ID );
327
-		if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) {
326
+		$thumbnail_id = get_post_thumbnail_id($post->ID);
327
+		if ('' !== $thumbnail_id && 0 !== $thumbnail_id) {
328 328
 			$ids[] = $thumbnail_id;
329 329
 		}
330 330
 
@@ -335,34 +335,34 @@  discard block
 block discarded – undo
335 335
 		//
336 336
 		// Get the embeds, removing existing ids.
337 337
 		// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
338
-		if ( apply_filters( 'wl_feature__enable__image-embeds', false ) ) {
339
-			$embeds = array_diff( $attachment_service->get_image_embeds( $post->post_content ), $ids );
338
+		if (apply_filters('wl_feature__enable__image-embeds', false)) {
339
+			$embeds = array_diff($attachment_service->get_image_embeds($post->post_content), $ids);
340 340
 		} else {
341 341
 			$embeds = array();
342 342
 		}
343 343
 
344 344
 		// Get the gallery, removing existing ids.
345
-		$gallery = array_diff( $attachment_service->get_gallery( $post ), $ids, $embeds );
345
+		$gallery = array_diff($attachment_service->get_gallery($post), $ids, $embeds);
346 346
 
347 347
 		// Map the attachment ids to images' data structured for schema.org use.
348 348
 		$images_with_sizes = array_filter(
349 349
 			array_reduce(
350
-				array_merge( $ids, $embeds, $gallery ),
351
-				function ( $carry, $item ) {
350
+				array_merge($ids, $embeds, $gallery),
351
+				function($carry, $item) {
352 352
 					/*
353 353
 					* @todo: we're not sure that we're getting attachment data here, we
354 354
 					* should filter `false`s.
355 355
 					*/
356 356
 
357 357
 					$sources = array_merge(
358
-						Wordlift_Image_Service::get_sources( $item ),
359
-						array( wp_get_attachment_image_src( $item, 'full' ) )
358
+						Wordlift_Image_Service::get_sources($item),
359
+						array(wp_get_attachment_image_src($item, 'full'))
360 360
 					);
361 361
 
362 362
 					$sources_with_image = array_filter(
363 363
 						$sources,
364
-						function ( $source ) {
365
-							return ! empty( $source[0] );
364
+						function($source) {
365
+							return ! empty($source[0]);
366 366
 						}
367 367
 					);
368 368
 
@@ -376,7 +376,7 @@  discard block
 block discarded – undo
376 376
 					// or from uploads dir, but the image id still exists as featured image
377 377
 					// or in [gallery] shortcode.
378 378
 					// if ( empty( $attachment[0] ) ) {
379
-					if ( empty( $sources_with_image ) ) {
379
+					if (empty($sources_with_image)) {
380 380
 						return $carry;
381 381
 					}
382 382
 
@@ -394,7 +394,7 @@  discard block
 block discarded – undo
394 394
 
395 395
 		// Refactor data as per schema.org specifications.
396 396
 		$images = array_map(
397
-			function ( $attachment ) {
397
+			function($attachment) {
398 398
 				return Wordlift_Abstract_Post_To_Jsonld_Converter::set_image_size(
399 399
 					array(
400 400
 						'@type' => 'ImageObject',
@@ -407,7 +407,7 @@  discard block
 block discarded – undo
407 407
 		);
408 408
 
409 409
 		// Add images if present.
410
-		if ( 0 < count( $images ) ) {
410
+		if (0 < count($images)) {
411 411
 			$jsonld['image'] = $images;
412 412
 		}
413 413
 
@@ -425,9 +425,9 @@  discard block
 block discarded – undo
425 425
 	 * @since  3.8.0
426 426
 	 * @access private
427 427
 	 */
428
-	protected static function make_one( $value ) {
428
+	protected static function make_one($value) {
429 429
 
430
-		return 1 === count( $value ) ? $value[0] : $value;
430
+		return 1 === count($value) ? $value[0] : $value;
431 431
 	}
432 432
 
433 433
 	/**
@@ -440,17 +440,17 @@  discard block
 block discarded – undo
440 440
 	 * @return array The enriched `ImageObject` array.
441 441
 	 * @since 3.14.0
442 442
 	 */
443
-	public static function set_image_size( $image, $attachment ) {
443
+	public static function set_image_size($image, $attachment) {
444 444
 
445 445
 		// If you specify a "width" or "height" value you should leave out
446 446
 		// 'px'. For example: "width":"4608px" should be "width":"4608".
447 447
 		//
448 448
 		// See https://github.com/insideout10/wordlift-plugin/issues/451.
449
-		if ( isset( $attachment[1] ) && is_numeric( $attachment[1] ) && 0 < $attachment[1] ) {
449
+		if (isset($attachment[1]) && is_numeric($attachment[1]) && 0 < $attachment[1]) {
450 450
 			$image['width'] = $attachment[1];
451 451
 		}
452 452
 
453
-		if ( isset( $attachment[2] ) && is_numeric( $attachment[2] ) && 0 < $attachment[2] ) {
453
+		if (isset($attachment[2]) && is_numeric($attachment[2]) && 0 < $attachment[2]) {
454 454
 			$image['height'] = $attachment[2];
455 455
 		}
456 456
 
@@ -469,33 +469,33 @@  discard block
 block discarded – undo
469 469
 	 * @since 3.20.0 This code moved from the above function `convert`, used for entity types defined in
470 470
 	 *  the {@link Wordlift_Schema_Service} class.
471 471
 	 */
472
-	protected function process_type_custom_fields( &$jsonld, $fields, $post, &$references, &$references_infos ) {
472
+	protected function process_type_custom_fields(&$jsonld, $fields, $post, &$references, &$references_infos) {
473 473
 
474 474
 		// Set a reference to use in closures.
475 475
 		$converter = $this;
476 476
 
477 477
 		// Try each field on the entity.
478
-		foreach ( $fields as $key => $value ) {
478
+		foreach ($fields as $key => $value) {
479 479
 
480 480
 			// Get the predicate.
481
-			$name = $this->relative_to_context( $value['predicate'] );
481
+			$name = $this->relative_to_context($value['predicate']);
482 482
 
483 483
 			// Get the value, the property service will get the right extractor
484 484
 			// for that property.
485
-			$value = $this->property_getter->get( $post->ID, $key, Object_Type_Enum::POST );
485
+			$value = $this->property_getter->get($post->ID, $key, Object_Type_Enum::POST);
486 486
 
487
-			if ( empty( $value ) ) {
487
+			if (empty($value)) {
488 488
 				continue;
489 489
 			}
490 490
 
491 491
 			// Map the value to the property name.
492 492
 			// If we got an array with just one value, we return that one value.
493 493
 			// If we got a Wordlift_Property_Entity_Reference we get the URL.
494
-			$jsonld[ $name ] = self::make_one(
494
+			$jsonld[$name] = self::make_one(
495 495
 				array_map(
496
-					function ( $item ) use ( $converter, &$references, &$references_infos ) {
496
+					function($item) use ($converter, &$references, &$references_infos) {
497 497
 
498
-						if ( $item instanceof Wordlift_Property_Entity_Reference ) {
498
+						if ($item instanceof Wordlift_Property_Entity_Reference) {
499 499
 
500 500
 							$url = $item->get_url();
501 501
 
@@ -504,12 +504,12 @@  discard block
 block discarded – undo
504 504
 							$references[] = $item->get_id();
505 505
 
506 506
 							// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
507
-							$references_infos[] = array( 'reference' => $item );
507
+							$references_infos[] = array('reference' => $item);
508 508
 
509
-							return array( '@id' => $url );
509
+							return array('@id' => $url);
510 510
 						}
511 511
 
512
-						return $converter->relative_to_context( $item );
512
+						return $converter->relative_to_context($item);
513 513
 					},
514 514
 					$value
515 515
 				)
Please login to merge, or discard this patch.
src/includes/class-wordlift-entity-uri-service.php 2 patches
Indentation   +242 added lines, -242 removed lines patch added patch discarded remove patch
@@ -20,100 +20,100 @@  discard block
 block discarded – undo
20 20
  */
21 21
 class Wordlift_Entity_Uri_Service {
22 22
 
23
-	/**
24
-	 * A {@link Wordlift_Log_Service} instance.
25
-	 *
26
-	 * @since  3.16.3
27
-	 * @access private
28
-	 * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
29
-	 */
30
-	private $log;
31
-
32
-	/**
33
-	 * An array of URIs to post ID valid for the current request.
34
-	 *
35
-	 * @since  3.16.3
36
-	 * @access private
37
-	 * @var array $uri_to_post An array of URIs to post ID valid for the current request.
38
-	 */
39
-	protected $uri_to_post;
40
-
41
-	/**
42
-	 * Create a {@link Wordlift_Entity_Uri_Service} instance.
43
-	 *
44
-	 * @since 3.16.3
45
-	 */
46
-	protected function __construct() {
47
-
48
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
49
-
50
-		// Add a filter to the `rest_post_dispatch` filter to add the wl_entity_url meta as `wl:entity_url`.
51
-		add_filter( 'rest_post_dispatch', array( $this, 'rest_post_dispatch' ) );
52
-		add_filter( 'wl_content_service__post__not_found', array( $this, 'content_service__post__not_found' ), 10, 2 );
53
-
54
-	}
55
-
56
-	/**
57
-	 * Holds the {@link Wordlift_Entity_Uri_Service} instance.
58
-	 *
59
-	 * @since 3.21.5
60
-	 * @access private
61
-	 * @var Wordlift_Entity_Uri_Service $instance The {@link Wordlift_Entity_Uri_Service} singleton.
62
-	 */
63
-	private static $instance = null;
64
-
65
-	/**
66
-	 * Get the singleton.
67
-	 *
68
-	 * @return Wordlift_Entity_Uri_Service The singleton instance.
69
-	 * @since 3.21.5
70
-	 */
71
-	public static function get_instance() {
72
-
73
-		if ( ! isset( self::$instance ) ) {
74
-			$entity_uri_cache_service = new Wordlift_File_Cache_Service( WL_TEMP_DIR . 'entity_uri/' );
75
-			self::$instance           = new Wordlift_Cached_Entity_Uri_Service( $entity_uri_cache_service );
76
-
77
-		}
78
-
79
-		return self::$instance;
80
-	}
81
-
82
-	/**
83
-	 * Try to find a post when the content service doesn't find it.
84
-	 *
85
-	 * @param WP_Post|null $post
86
-	 * @param string       $uri
87
-	 *
88
-	 * @return false|int
89
-	 */
90
-	public function content_service__post__not_found( $post, $uri ) {
91
-		return $this->get_post_id_from_url( $uri );
92
-	}
93
-
94
-	/**
95
-	 * Preload the provided URIs in the local cache.
96
-	 *
97
-	 * This function will populate the local `$uri_to_post` array by running a
98
-	 * single query with all the URIs and returning the mappings in the array.
99
-	 *
100
-	 * @param array $uris An array of URIs.
101
-	 *
102
-	 * @since 3.16.3
103
-	 */
104
-	public function preload_uris( $uris ) {
105
-
106
-		// Bail out if there are no URIs.
107
-		if ( 0 === count( $uris ) ) {
108
-			return;
109
-		}
110
-
111
-		$this->log->trace( 'Preloading ' . count( $uris ) . ' URI(s)...' );
112
-
113
-		global $wpdb;
114
-		$in_post_types  = implode( "','", array_map( 'esc_sql', Wordlift_Entity_Service::valid_entity_post_types() ) );
115
-		$in_entity_uris = implode( "','", array_map( 'esc_sql', $uris ) );
116
-		$sql            = "
23
+    /**
24
+     * A {@link Wordlift_Log_Service} instance.
25
+     *
26
+     * @since  3.16.3
27
+     * @access private
28
+     * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
29
+     */
30
+    private $log;
31
+
32
+    /**
33
+     * An array of URIs to post ID valid for the current request.
34
+     *
35
+     * @since  3.16.3
36
+     * @access private
37
+     * @var array $uri_to_post An array of URIs to post ID valid for the current request.
38
+     */
39
+    protected $uri_to_post;
40
+
41
+    /**
42
+     * Create a {@link Wordlift_Entity_Uri_Service} instance.
43
+     *
44
+     * @since 3.16.3
45
+     */
46
+    protected function __construct() {
47
+
48
+        $this->log = Wordlift_Log_Service::get_logger( get_class() );
49
+
50
+        // Add a filter to the `rest_post_dispatch` filter to add the wl_entity_url meta as `wl:entity_url`.
51
+        add_filter( 'rest_post_dispatch', array( $this, 'rest_post_dispatch' ) );
52
+        add_filter( 'wl_content_service__post__not_found', array( $this, 'content_service__post__not_found' ), 10, 2 );
53
+
54
+    }
55
+
56
+    /**
57
+     * Holds the {@link Wordlift_Entity_Uri_Service} instance.
58
+     *
59
+     * @since 3.21.5
60
+     * @access private
61
+     * @var Wordlift_Entity_Uri_Service $instance The {@link Wordlift_Entity_Uri_Service} singleton.
62
+     */
63
+    private static $instance = null;
64
+
65
+    /**
66
+     * Get the singleton.
67
+     *
68
+     * @return Wordlift_Entity_Uri_Service The singleton instance.
69
+     * @since 3.21.5
70
+     */
71
+    public static function get_instance() {
72
+
73
+        if ( ! isset( self::$instance ) ) {
74
+            $entity_uri_cache_service = new Wordlift_File_Cache_Service( WL_TEMP_DIR . 'entity_uri/' );
75
+            self::$instance           = new Wordlift_Cached_Entity_Uri_Service( $entity_uri_cache_service );
76
+
77
+        }
78
+
79
+        return self::$instance;
80
+    }
81
+
82
+    /**
83
+     * Try to find a post when the content service doesn't find it.
84
+     *
85
+     * @param WP_Post|null $post
86
+     * @param string       $uri
87
+     *
88
+     * @return false|int
89
+     */
90
+    public function content_service__post__not_found( $post, $uri ) {
91
+        return $this->get_post_id_from_url( $uri );
92
+    }
93
+
94
+    /**
95
+     * Preload the provided URIs in the local cache.
96
+     *
97
+     * This function will populate the local `$uri_to_post` array by running a
98
+     * single query with all the URIs and returning the mappings in the array.
99
+     *
100
+     * @param array $uris An array of URIs.
101
+     *
102
+     * @since 3.16.3
103
+     */
104
+    public function preload_uris( $uris ) {
105
+
106
+        // Bail out if there are no URIs.
107
+        if ( 0 === count( $uris ) ) {
108
+            return;
109
+        }
110
+
111
+        $this->log->trace( 'Preloading ' . count( $uris ) . ' URI(s)...' );
112
+
113
+        global $wpdb;
114
+        $in_post_types  = implode( "','", array_map( 'esc_sql', Wordlift_Entity_Service::valid_entity_post_types() ) );
115
+        $in_entity_uris = implode( "','", array_map( 'esc_sql', $uris ) );
116
+        $sql            = "
117 117
 			SELECT ID FROM $wpdb->posts p
118 118
 			INNER JOIN $wpdb->postmeta pm
119 119
 			 ON pm.post_id = p.ID
@@ -123,153 +123,153 @@  discard block
 block discarded – undo
123 123
 			  AND p.post_status IN ( 'publish', 'draft', 'private', 'future' )
124 124
   		";
125 125
 
126
-		// Get the posts.
127
-		$posts = $wpdb->get_col( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
128
-
129
-		// Populate the array. We reinitialize the array on purpose because
130
-		// we don't want these data to long live.
131
-		$this->uri_to_post = array_reduce(
132
-			$posts,
133
-			function ( $carry, $item ) {
134
-				$uris = get_post_meta( $item, Wordlift_Schema_Service::FIELD_SAME_AS );
135
-
136
-				$uri = Wordpress_Content_Service::get_instance()
137
-												->get_entity_id( Wordpress_Content_Id::create_post( $item ) );
138
-
139
-				if ( isset( $uri ) ) {
140
-					$uris[] = $uri;
141
-				}
142
-
143
-				return $carry
144
-					   // Get the URI related to the post and fill them with the item id.
145
-					   + array_fill_keys( $uris, $item );
146
-			},
147
-			array()
148
-		);
149
-
150
-		// Add the not found URIs.
151
-		$this->uri_to_post += array_fill_keys( $uris, null );
152
-
153
-		$this->log->debug( count( $this->uri_to_post ) . ' URI(s) preloaded.' );
154
-
155
-	}
156
-
157
-	/**
158
-	 * Reset the URI to post local cache.
159
-	 *
160
-	 * @since 3.16.3
161
-	 */
162
-	public function reset_uris() {
163
-
164
-		$this->uri_to_post = array();
165
-
166
-	}
167
-
168
-	/**
169
-	 * Find entity posts by the entity URI. Entity as searched by their entity URI or same as.
170
-	 *
171
-	 * @param string $uri The entity URI.
172
-	 *
173
-	 * @return WP_Post|null A WP_Post instance or null if not found.
174
-	 * @since 3.2.0
175
-	 */
176
-	public function get_entity( $uri ) {
177
-		if ( ! isset( $uri ) ) {
178
-			return null;
179
-		}
180
-
181
-		$this->log->trace( "Getting an entity post for URI $uri..." );
182
-
183
-		$content = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as( $uri );
184
-
185
-		// Return null if the content isn't found or isn't a post.
186
-		if ( ! isset( $content ) || ! is_a( $content->get_bag(), '\WP_Post' ) ) {
187
-			return null;
188
-		}
189
-
190
-		return $content->get_bag();
191
-	}
192
-
193
-	/**
194
-	 * Determines whether a given uri is an internal uri or not.
195
-	 *
196
-	 * @param string $uri An uri.
197
-	 *
198
-	 * @return true if the uri internal to the current dataset otherwise false.
199
-	 * @since 3.16.3
200
-	 */
201
-	public function is_internal( $uri ) {
202
-
203
-		return ( 0 === strrpos( $uri, (string) Wordlift_Configuration_Service::get_instance()->get_dataset_uri() ) );
204
-	}
205
-
206
-	/**
207
-	 * Hook to `rest_post_dispatch` to alter the response and add the `wl_entity_url` post meta as `wl:entity_url`.
208
-	 *
209
-	 * We're using this filter instead of the well known `register_meta` / `register_rest_field` because we still need
210
-	 * to provide full compatibility with WordPress 4.4+.
211
-	 *
212
-	 * @param WP_HTTP_Response $result Result to send to the client. Usually a WP_REST_Response.
213
-	 *
214
-	 * @return WP_HTTP_Response The result to send to the client.
215
-	 *
216
-	 * @since 3.23.0
217
-	 */
218
-	public function rest_post_dispatch( $result ) {
219
-
220
-		// Get a reference to the actual data.
221
-		$data = &$result->data;
222
-
223
-		// Bail out if we don't have the required parameters, or if the type is not a valid entity.
224
-		if ( ! is_array( $data ) || ! isset( $data['id'] ) || ! isset( $data['type'] )
225
-			 || ! Wordlift_Entity_Type_Service::is_valid_entity_post_type( $data['type'] ) ) {
226
-			return $result;
227
-		}
228
-
229
-		// Add the `wl:entity_url`.
230
-		$data['wl:entity_url'] = Wordlift_Entity_Service::get_instance()->get_uri( $data['id'] );
231
-
232
-		return $result;
233
-	}
234
-
235
-	/**
236
-	 * Helper function to fetch post_id from a WordPress URL
237
-	 * Primarily used when dataset is not enabled
238
-	 *
239
-	 * @param $url
240
-	 *
241
-	 * @return int Post ID | bool false
242
-	 */
243
-	public function get_post_id_from_url( $url ) {
244
-
245
-		// Try url_to_postid
246
-		$post_id = url_to_postid( htmlspecialchars_decode( $url ) );
247
-		if ( 0 !== $post_id ) {
248
-			return $post_id;
249
-		}
250
-
251
-		$parsed_url = wp_parse_url( $url );
252
-
253
-		if ( ! isset( $parsed_url['query'] ) ) {
254
-			return false;
255
-		}
256
-
257
-		parse_str( $parsed_url['query'], $parsed_query );
258
-
259
-		// Try to parse WooCommerce non-pretty product URL
260
-		if ( $parsed_query['product'] ) {
261
-			$posts = get_posts(
262
-				array(
263
-					'name'      => $parsed_query['product'],
264
-					'post_type' => 'product',
265
-				)
266
-			);
267
-			if ( count( $posts ) > 0 ) {
268
-				return $posts[0]->ID;
269
-			}
270
-		}
271
-
272
-		return false;
273
-	}
126
+        // Get the posts.
127
+        $posts = $wpdb->get_col( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
128
+
129
+        // Populate the array. We reinitialize the array on purpose because
130
+        // we don't want these data to long live.
131
+        $this->uri_to_post = array_reduce(
132
+            $posts,
133
+            function ( $carry, $item ) {
134
+                $uris = get_post_meta( $item, Wordlift_Schema_Service::FIELD_SAME_AS );
135
+
136
+                $uri = Wordpress_Content_Service::get_instance()
137
+                                                ->get_entity_id( Wordpress_Content_Id::create_post( $item ) );
138
+
139
+                if ( isset( $uri ) ) {
140
+                    $uris[] = $uri;
141
+                }
142
+
143
+                return $carry
144
+                        // Get the URI related to the post and fill them with the item id.
145
+                       + array_fill_keys( $uris, $item );
146
+            },
147
+            array()
148
+        );
149
+
150
+        // Add the not found URIs.
151
+        $this->uri_to_post += array_fill_keys( $uris, null );
152
+
153
+        $this->log->debug( count( $this->uri_to_post ) . ' URI(s) preloaded.' );
154
+
155
+    }
156
+
157
+    /**
158
+     * Reset the URI to post local cache.
159
+     *
160
+     * @since 3.16.3
161
+     */
162
+    public function reset_uris() {
163
+
164
+        $this->uri_to_post = array();
165
+
166
+    }
167
+
168
+    /**
169
+     * Find entity posts by the entity URI. Entity as searched by their entity URI or same as.
170
+     *
171
+     * @param string $uri The entity URI.
172
+     *
173
+     * @return WP_Post|null A WP_Post instance or null if not found.
174
+     * @since 3.2.0
175
+     */
176
+    public function get_entity( $uri ) {
177
+        if ( ! isset( $uri ) ) {
178
+            return null;
179
+        }
180
+
181
+        $this->log->trace( "Getting an entity post for URI $uri..." );
182
+
183
+        $content = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as( $uri );
184
+
185
+        // Return null if the content isn't found or isn't a post.
186
+        if ( ! isset( $content ) || ! is_a( $content->get_bag(), '\WP_Post' ) ) {
187
+            return null;
188
+        }
189
+
190
+        return $content->get_bag();
191
+    }
192
+
193
+    /**
194
+     * Determines whether a given uri is an internal uri or not.
195
+     *
196
+     * @param string $uri An uri.
197
+     *
198
+     * @return true if the uri internal to the current dataset otherwise false.
199
+     * @since 3.16.3
200
+     */
201
+    public function is_internal( $uri ) {
202
+
203
+        return ( 0 === strrpos( $uri, (string) Wordlift_Configuration_Service::get_instance()->get_dataset_uri() ) );
204
+    }
205
+
206
+    /**
207
+     * Hook to `rest_post_dispatch` to alter the response and add the `wl_entity_url` post meta as `wl:entity_url`.
208
+     *
209
+     * We're using this filter instead of the well known `register_meta` / `register_rest_field` because we still need
210
+     * to provide full compatibility with WordPress 4.4+.
211
+     *
212
+     * @param WP_HTTP_Response $result Result to send to the client. Usually a WP_REST_Response.
213
+     *
214
+     * @return WP_HTTP_Response The result to send to the client.
215
+     *
216
+     * @since 3.23.0
217
+     */
218
+    public function rest_post_dispatch( $result ) {
219
+
220
+        // Get a reference to the actual data.
221
+        $data = &$result->data;
222
+
223
+        // Bail out if we don't have the required parameters, or if the type is not a valid entity.
224
+        if ( ! is_array( $data ) || ! isset( $data['id'] ) || ! isset( $data['type'] )
225
+             || ! Wordlift_Entity_Type_Service::is_valid_entity_post_type( $data['type'] ) ) {
226
+            return $result;
227
+        }
228
+
229
+        // Add the `wl:entity_url`.
230
+        $data['wl:entity_url'] = Wordlift_Entity_Service::get_instance()->get_uri( $data['id'] );
231
+
232
+        return $result;
233
+    }
234
+
235
+    /**
236
+     * Helper function to fetch post_id from a WordPress URL
237
+     * Primarily used when dataset is not enabled
238
+     *
239
+     * @param $url
240
+     *
241
+     * @return int Post ID | bool false
242
+     */
243
+    public function get_post_id_from_url( $url ) {
244
+
245
+        // Try url_to_postid
246
+        $post_id = url_to_postid( htmlspecialchars_decode( $url ) );
247
+        if ( 0 !== $post_id ) {
248
+            return $post_id;
249
+        }
250
+
251
+        $parsed_url = wp_parse_url( $url );
252
+
253
+        if ( ! isset( $parsed_url['query'] ) ) {
254
+            return false;
255
+        }
256
+
257
+        parse_str( $parsed_url['query'], $parsed_query );
258
+
259
+        // Try to parse WooCommerce non-pretty product URL
260
+        if ( $parsed_query['product'] ) {
261
+            $posts = get_posts(
262
+                array(
263
+                    'name'      => $parsed_query['product'],
264
+                    'post_type' => 'product',
265
+                )
266
+            );
267
+            if ( count( $posts ) > 0 ) {
268
+                return $posts[0]->ID;
269
+            }
270
+        }
271
+
272
+        return false;
273
+    }
274 274
 
275 275
 }
Please login to merge, or discard this patch.
Spacing   +40 added lines, -40 removed lines patch added patch discarded remove patch
@@ -45,11 +45,11 @@  discard block
 block discarded – undo
45 45
 	 */
46 46
 	protected function __construct() {
47 47
 
48
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
48
+		$this->log = Wordlift_Log_Service::get_logger(get_class());
49 49
 
50 50
 		// Add a filter to the `rest_post_dispatch` filter to add the wl_entity_url meta as `wl:entity_url`.
51
-		add_filter( 'rest_post_dispatch', array( $this, 'rest_post_dispatch' ) );
52
-		add_filter( 'wl_content_service__post__not_found', array( $this, 'content_service__post__not_found' ), 10, 2 );
51
+		add_filter('rest_post_dispatch', array($this, 'rest_post_dispatch'));
52
+		add_filter('wl_content_service__post__not_found', array($this, 'content_service__post__not_found'), 10, 2);
53 53
 
54 54
 	}
55 55
 
@@ -70,9 +70,9 @@  discard block
 block discarded – undo
70 70
 	 */
71 71
 	public static function get_instance() {
72 72
 
73
-		if ( ! isset( self::$instance ) ) {
74
-			$entity_uri_cache_service = new Wordlift_File_Cache_Service( WL_TEMP_DIR . 'entity_uri/' );
75
-			self::$instance           = new Wordlift_Cached_Entity_Uri_Service( $entity_uri_cache_service );
73
+		if ( ! isset(self::$instance)) {
74
+			$entity_uri_cache_service = new Wordlift_File_Cache_Service(WL_TEMP_DIR.'entity_uri/');
75
+			self::$instance           = new Wordlift_Cached_Entity_Uri_Service($entity_uri_cache_service);
76 76
 
77 77
 		}
78 78
 
@@ -87,8 +87,8 @@  discard block
 block discarded – undo
87 87
 	 *
88 88
 	 * @return false|int
89 89
 	 */
90
-	public function content_service__post__not_found( $post, $uri ) {
91
-		return $this->get_post_id_from_url( $uri );
90
+	public function content_service__post__not_found($post, $uri) {
91
+		return $this->get_post_id_from_url($uri);
92 92
 	}
93 93
 
94 94
 	/**
@@ -101,18 +101,18 @@  discard block
 block discarded – undo
101 101
 	 *
102 102
 	 * @since 3.16.3
103 103
 	 */
104
-	public function preload_uris( $uris ) {
104
+	public function preload_uris($uris) {
105 105
 
106 106
 		// Bail out if there are no URIs.
107
-		if ( 0 === count( $uris ) ) {
107
+		if (0 === count($uris)) {
108 108
 			return;
109 109
 		}
110 110
 
111
-		$this->log->trace( 'Preloading ' . count( $uris ) . ' URI(s)...' );
111
+		$this->log->trace('Preloading '.count($uris).' URI(s)...');
112 112
 
113 113
 		global $wpdb;
114
-		$in_post_types  = implode( "','", array_map( 'esc_sql', Wordlift_Entity_Service::valid_entity_post_types() ) );
115
-		$in_entity_uris = implode( "','", array_map( 'esc_sql', $uris ) );
114
+		$in_post_types  = implode("','", array_map('esc_sql', Wordlift_Entity_Service::valid_entity_post_types()));
115
+		$in_entity_uris = implode("','", array_map('esc_sql', $uris));
116 116
 		$sql            = "
117 117
 			SELECT ID FROM $wpdb->posts p
118 118
 			INNER JOIN $wpdb->postmeta pm
@@ -124,33 +124,33 @@  discard block
 block discarded – undo
124 124
   		";
125 125
 
126 126
 		// Get the posts.
127
-		$posts = $wpdb->get_col( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
127
+		$posts = $wpdb->get_col($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
128 128
 
129 129
 		// Populate the array. We reinitialize the array on purpose because
130 130
 		// we don't want these data to long live.
131 131
 		$this->uri_to_post = array_reduce(
132 132
 			$posts,
133
-			function ( $carry, $item ) {
134
-				$uris = get_post_meta( $item, Wordlift_Schema_Service::FIELD_SAME_AS );
133
+			function($carry, $item) {
134
+				$uris = get_post_meta($item, Wordlift_Schema_Service::FIELD_SAME_AS);
135 135
 
136 136
 				$uri = Wordpress_Content_Service::get_instance()
137
-												->get_entity_id( Wordpress_Content_Id::create_post( $item ) );
137
+												->get_entity_id(Wordpress_Content_Id::create_post($item));
138 138
 
139
-				if ( isset( $uri ) ) {
139
+				if (isset($uri)) {
140 140
 					$uris[] = $uri;
141 141
 				}
142 142
 
143 143
 				return $carry
144 144
 					   // Get the URI related to the post and fill them with the item id.
145
-					   + array_fill_keys( $uris, $item );
145
+					   + array_fill_keys($uris, $item);
146 146
 			},
147 147
 			array()
148 148
 		);
149 149
 
150 150
 		// Add the not found URIs.
151
-		$this->uri_to_post += array_fill_keys( $uris, null );
151
+		$this->uri_to_post += array_fill_keys($uris, null);
152 152
 
153
-		$this->log->debug( count( $this->uri_to_post ) . ' URI(s) preloaded.' );
153
+		$this->log->debug(count($this->uri_to_post).' URI(s) preloaded.');
154 154
 
155 155
 	}
156 156
 
@@ -173,17 +173,17 @@  discard block
 block discarded – undo
173 173
 	 * @return WP_Post|null A WP_Post instance or null if not found.
174 174
 	 * @since 3.2.0
175 175
 	 */
176
-	public function get_entity( $uri ) {
177
-		if ( ! isset( $uri ) ) {
176
+	public function get_entity($uri) {
177
+		if ( ! isset($uri)) {
178 178
 			return null;
179 179
 		}
180 180
 
181
-		$this->log->trace( "Getting an entity post for URI $uri..." );
181
+		$this->log->trace("Getting an entity post for URI $uri...");
182 182
 
183
-		$content = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as( $uri );
183
+		$content = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as($uri);
184 184
 
185 185
 		// Return null if the content isn't found or isn't a post.
186
-		if ( ! isset( $content ) || ! is_a( $content->get_bag(), '\WP_Post' ) ) {
186
+		if ( ! isset($content) || ! is_a($content->get_bag(), '\WP_Post')) {
187 187
 			return null;
188 188
 		}
189 189
 
@@ -198,9 +198,9 @@  discard block
 block discarded – undo
198 198
 	 * @return true if the uri internal to the current dataset otherwise false.
199 199
 	 * @since 3.16.3
200 200
 	 */
201
-	public function is_internal( $uri ) {
201
+	public function is_internal($uri) {
202 202
 
203
-		return ( 0 === strrpos( $uri, (string) Wordlift_Configuration_Service::get_instance()->get_dataset_uri() ) );
203
+		return (0 === strrpos($uri, (string) Wordlift_Configuration_Service::get_instance()->get_dataset_uri()));
204 204
 	}
205 205
 
206 206
 	/**
@@ -215,19 +215,19 @@  discard block
 block discarded – undo
215 215
 	 *
216 216
 	 * @since 3.23.0
217 217
 	 */
218
-	public function rest_post_dispatch( $result ) {
218
+	public function rest_post_dispatch($result) {
219 219
 
220 220
 		// Get a reference to the actual data.
221 221
 		$data = &$result->data;
222 222
 
223 223
 		// Bail out if we don't have the required parameters, or if the type is not a valid entity.
224
-		if ( ! is_array( $data ) || ! isset( $data['id'] ) || ! isset( $data['type'] )
225
-			 || ! Wordlift_Entity_Type_Service::is_valid_entity_post_type( $data['type'] ) ) {
224
+		if ( ! is_array($data) || ! isset($data['id']) || ! isset($data['type'])
225
+			 || ! Wordlift_Entity_Type_Service::is_valid_entity_post_type($data['type'])) {
226 226
 			return $result;
227 227
 		}
228 228
 
229 229
 		// Add the `wl:entity_url`.
230
-		$data['wl:entity_url'] = Wordlift_Entity_Service::get_instance()->get_uri( $data['id'] );
230
+		$data['wl:entity_url'] = Wordlift_Entity_Service::get_instance()->get_uri($data['id']);
231 231
 
232 232
 		return $result;
233 233
 	}
@@ -240,31 +240,31 @@  discard block
 block discarded – undo
240 240
 	 *
241 241
 	 * @return int Post ID | bool false
242 242
 	 */
243
-	public function get_post_id_from_url( $url ) {
243
+	public function get_post_id_from_url($url) {
244 244
 
245 245
 		// Try url_to_postid
246
-		$post_id = url_to_postid( htmlspecialchars_decode( $url ) );
247
-		if ( 0 !== $post_id ) {
246
+		$post_id = url_to_postid(htmlspecialchars_decode($url));
247
+		if (0 !== $post_id) {
248 248
 			return $post_id;
249 249
 		}
250 250
 
251
-		$parsed_url = wp_parse_url( $url );
251
+		$parsed_url = wp_parse_url($url);
252 252
 
253
-		if ( ! isset( $parsed_url['query'] ) ) {
253
+		if ( ! isset($parsed_url['query'])) {
254 254
 			return false;
255 255
 		}
256 256
 
257
-		parse_str( $parsed_url['query'], $parsed_query );
257
+		parse_str($parsed_url['query'], $parsed_query);
258 258
 
259 259
 		// Try to parse WooCommerce non-pretty product URL
260
-		if ( $parsed_query['product'] ) {
260
+		if ($parsed_query['product']) {
261 261
 			$posts = get_posts(
262 262
 				array(
263 263
 					'name'      => $parsed_query['product'],
264 264
 					'post_type' => 'product',
265 265
 				)
266 266
 			);
267
-			if ( count( $posts ) > 0 ) {
267
+			if (count($posts) > 0) {
268 268
 				return $posts[0]->ID;
269 269
 			}
270 270
 		}
Please login to merge, or discard this patch.