Completed
Push — develop ( 084649...c9fe7e )
by David
03:58 queued 49s
created
src/install/class-wordlift-install-3-25-0.php 1 patch
Indentation   +145 added lines, -145 removed lines patch added patch discarded remove patch
@@ -60,54 +60,54 @@  discard block
 block discarded – undo
60 60
  * @subpackage Wordlift/install
61 61
  */
62 62
 class Wordlift_Install_3_25_0 extends Wordlift_Install {
63
-	/**
64
-	 * @inheritdoc
65
-	 */
66
-	protected static $version = '3.25.3';
67
-
68
-	/**
69
-	 * Reference to global $wpdb instance.
70
-	 *
71
-	 * @var $wpdb
72
-	 * */
73
-	private $wpdb;
74
-
75
-	/** Constructor for 3_25_0 installer. */
76
-	public function __construct() {
77
-		global $wpdb;
78
-		$this->wpdb = $wpdb;
79
-	}
80
-
81
-	/**
82
-	 * @inheritdoc
83
-	 */
84
-	public function install() {
85
-		$this->create_mappings_table();
86
-		$this->create_rule_group_table();
87
-		$this->create_rule_table();
88
-		$this->create_property_table();
89
-	}
90
-
91
-	/**
92
-	 * Install mappings table.
93
-	 *
94
-	 * +----------------+--------------+------+-----+---------+----------------+
95
-	 * | Field          | Type         | Null | Key | Default | Extra          |
96
-	 * +----------------+--------------+------+-----+---------+----------------+
97
-	 * | mapping_id     | int(11)      | NO   | PRI | NULL    | auto_increment |
98
-	 * | mapping_title  | varchar(255) | NO   |     | NULL    |                |
99
-	 * | mapping_status | varchar(255) | NO   |     | active  |                |
100
-	 * +----------------+--------------+------+-----+---------+----------------+
101
-	 *
102
-	 * @return void
103
-	 * @since 3.25.0
104
-	 *
105
-	 */
106
-	public function create_mappings_table() {
107
-		$table_name      = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME;
108
-		$charset_collate = $this->wpdb->get_charset_collate();
109
-		// @@todo: is necessary to prefix the column names with `mapping_` ? we're the mappings table already.
110
-		$sql = <<<EOF
63
+    /**
64
+     * @inheritdoc
65
+     */
66
+    protected static $version = '3.25.3';
67
+
68
+    /**
69
+     * Reference to global $wpdb instance.
70
+     *
71
+     * @var $wpdb
72
+     * */
73
+    private $wpdb;
74
+
75
+    /** Constructor for 3_25_0 installer. */
76
+    public function __construct() {
77
+        global $wpdb;
78
+        $this->wpdb = $wpdb;
79
+    }
80
+
81
+    /**
82
+     * @inheritdoc
83
+     */
84
+    public function install() {
85
+        $this->create_mappings_table();
86
+        $this->create_rule_group_table();
87
+        $this->create_rule_table();
88
+        $this->create_property_table();
89
+    }
90
+
91
+    /**
92
+     * Install mappings table.
93
+     *
94
+     * +----------------+--------------+------+-----+---------+----------------+
95
+     * | Field          | Type         | Null | Key | Default | Extra          |
96
+     * +----------------+--------------+------+-----+---------+----------------+
97
+     * | mapping_id     | int(11)      | NO   | PRI | NULL    | auto_increment |
98
+     * | mapping_title  | varchar(255) | NO   |     | NULL    |                |
99
+     * | mapping_status | varchar(255) | NO   |     | active  |                |
100
+     * +----------------+--------------+------+-----+---------+----------------+
101
+     *
102
+     * @return void
103
+     * @since 3.25.0
104
+     *
105
+     */
106
+    public function create_mappings_table() {
107
+        $table_name      = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME;
108
+        $charset_collate = $this->wpdb->get_charset_collate();
109
+        // @@todo: is necessary to prefix the column names with `mapping_` ? we're the mappings table already.
110
+        $sql = <<<EOF
111 111
         CREATE TABLE $table_name (
112 112
 			mapping_id INT(11) NOT NULL AUTO_INCREMENT, 
113 113
 			mapping_title VARCHAR(255) NOT NULL,
@@ -115,33 +115,33 @@  discard block
 block discarded – undo
115 115
 			PRIMARY KEY  (mapping_id)
116 116
         ) $charset_collate;
117 117
 EOF;
118
-		// Execute the query for mappings table.
119
-		dbDelta( $sql );
120
-	}
121
-
122
-
123
-	/**
124
-	 * Install rule table
125
-	 *
126
-	 * +------------------+--------------+------+-----+---------+----------------+
127
-	 * | Field            | Type         | Null | Key | Default | Extra          |
128
-	 * +------------------+--------------+------+-----+---------+----------------+
129
-	 * | rule_id          | int(11)      | NO   | PRI | NULL    | auto_increment |
130
-	 * | rule_field_one   | varchar(255) | NO   |     | NULL    |                |
131
-	 * | rule_logic_field | varchar(255) | NO   |     | NULL    |                |
132
-	 * | rule_field_two   | varchar(255) | NO   |     | NULL    |                |
133
-	 * | rule_group_id    | int(11)      | NO   | MUL | NULL    |                |
134
-	 * +------------------+--------------+------+-----+---------+----------------+
135
-	 *
136
-	 * @return void
137
-	 * @since 3.25.0
138
-	 */
139
-	public function create_rule_table() {
140
-		$table_name            = $this->wpdb->prefix . WL_RULE_TABLE_NAME;
141
-		$rule_group_table_name = $this->wpdb->prefix . WL_RULE_GROUP_TABLE_NAME;
142
-		$charset_collate       = $this->wpdb->get_charset_collate();
143
-		// @@todo: is necessary to prefix the column names with `rule_` ? we're the rules table already.
144
-		$sql = <<<EOF
118
+        // Execute the query for mappings table.
119
+        dbDelta( $sql );
120
+    }
121
+
122
+
123
+    /**
124
+     * Install rule table
125
+     *
126
+     * +------------------+--------------+------+-----+---------+----------------+
127
+     * | Field            | Type         | Null | Key | Default | Extra          |
128
+     * +------------------+--------------+------+-----+---------+----------------+
129
+     * | rule_id          | int(11)      | NO   | PRI | NULL    | auto_increment |
130
+     * | rule_field_one   | varchar(255) | NO   |     | NULL    |                |
131
+     * | rule_logic_field | varchar(255) | NO   |     | NULL    |                |
132
+     * | rule_field_two   | varchar(255) | NO   |     | NULL    |                |
133
+     * | rule_group_id    | int(11)      | NO   | MUL | NULL    |                |
134
+     * +------------------+--------------+------+-----+---------+----------------+
135
+     *
136
+     * @return void
137
+     * @since 3.25.0
138
+     */
139
+    public function create_rule_table() {
140
+        $table_name            = $this->wpdb->prefix . WL_RULE_TABLE_NAME;
141
+        $rule_group_table_name = $this->wpdb->prefix . WL_RULE_GROUP_TABLE_NAME;
142
+        $charset_collate       = $this->wpdb->get_charset_collate();
143
+        // @@todo: is necessary to prefix the column names with `rule_` ? we're the rules table already.
144
+        $sql = <<<EOF
145 145
         CREATE TABLE IF NOT EXISTS $table_name (
146 146
 				rule_id INT(11) NOT NULL AUTO_INCREMENT,
147 147
 				rule_field_one VARCHAR(255) NOT NULL,
@@ -153,31 +153,31 @@  discard block
 block discarded – undo
153 153
 				PRIMARY KEY  (rule_id)
154 154
         ) $charset_collate;
155 155
 EOF;
156
-		// Execute the query for mappings table.
157
-		$this->wpdb->query( $sql );
158
-	}
159
-
160
-	/**
161
-	 * Install rule group table, should run after creating mapping and
162
-	 * rule table due to foreign key reference.
163
-	 *
164
-	 * +---------------+---------+------+-----+---------+----------------+
165
-	 * | Field         | Type    | Null | Key | Default | Extra          |
166
-	 * +---------------+---------+------+-----+---------+----------------+
167
-	 * | rule_group_id | int(11) | NO   | PRI | NULL    | auto_increment |
168
-	 * | mapping_id    | int(11) | NO   | MUL | NULL    |                |
169
-	 * +---------------+---------+------+-----+---------+----------------+
170
-	 *
171
-	 * @return void
172
-	 * @since 3.25.0
173
-	 */
174
-	public function create_rule_group_table() {
175
-		$table_name      = $this->wpdb->prefix . WL_RULE_GROUP_TABLE_NAME;
176
-		$charset_collate = $this->wpdb->get_charset_collate();
177
-
178
-		$mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME;
179
-		// @@todo is this table actually needed? I am not understanding what it does.
180
-		$sql = <<<EOF
156
+        // Execute the query for mappings table.
157
+        $this->wpdb->query( $sql );
158
+    }
159
+
160
+    /**
161
+     * Install rule group table, should run after creating mapping and
162
+     * rule table due to foreign key reference.
163
+     *
164
+     * +---------------+---------+------+-----+---------+----------------+
165
+     * | Field         | Type    | Null | Key | Default | Extra          |
166
+     * +---------------+---------+------+-----+---------+----------------+
167
+     * | rule_group_id | int(11) | NO   | PRI | NULL    | auto_increment |
168
+     * | mapping_id    | int(11) | NO   | MUL | NULL    |                |
169
+     * +---------------+---------+------+-----+---------+----------------+
170
+     *
171
+     * @return void
172
+     * @since 3.25.0
173
+     */
174
+    public function create_rule_group_table() {
175
+        $table_name      = $this->wpdb->prefix . WL_RULE_GROUP_TABLE_NAME;
176
+        $charset_collate = $this->wpdb->get_charset_collate();
177
+
178
+        $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME;
179
+        // @@todo is this table actually needed? I am not understanding what it does.
180
+        $sql = <<<EOF
181 181
         CREATE TABLE IF NOT EXISTS $table_name (
182 182
                 rule_group_id INT(11) NOT NULL AUTO_INCREMENT,
183 183
                 mapping_id INT(11) NOT NULL,
@@ -186,38 +186,38 @@  discard block
 block discarded – undo
186 186
                 ON DELETE CASCADE
187 187
         ) $charset_collate;
188 188
 EOF;
189
-		// Execute the query for rule group table, we cant use db delta
190
-		// due to lack of support for foreign keys.
191
-		$this->wpdb->query( $sql );
192
-	}
193
-
194
-
195
-	/**
196
-	 * Install property table, should run after mapping table due to
197
-	 * foreign key reference.
198
-	 *
199
-	 * +--------------------+--------------+------+-----+---------+----------------+
200
-	 * | Field              | Type         | Null | Key | Default | Extra          |
201
-	 * +--------------------+--------------+------+-----+---------+----------------+
202
-	 * | property_id        | int(11)      | NO   | PRI | NULL    | auto_increment |
203
-	 * | mapping_id         | int(11)      | NO   | MUL | NULL    |                |
204
-	 * | property_name      | varchar(255) | NO   |     | NULL    |                |
205
-	 * | field_type         | varchar(255) | NO   |     | NULL    |                |
206
-	 * | field_name         | varchar(255) | NO   |     | NULL    |                |
207
-	 * | transform_function | varchar(255) | NO   |     | NULL    |                |
208
-	 * | property_status    | varchar(255) | NO   |     | active  |                |
209
-	 * +--------------------+--------------+------+-----+---------+----------------+
210
-	 *
211
-	 * @return void
212
-	 * @since 3.25.0
213
-	 */
214
-	public function create_property_table() {
215
-		$table_name      = $this->wpdb->prefix . WL_PROPERTY_TABLE_NAME;
216
-		$charset_collate = $this->wpdb->get_charset_collate();
217
-
218
-		$mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME;
219
-		// @@todo do you we need the `property_` prefix? this is the property table anyway.
220
-		$sql = <<<EOF
189
+        // Execute the query for rule group table, we cant use db delta
190
+        // due to lack of support for foreign keys.
191
+        $this->wpdb->query( $sql );
192
+    }
193
+
194
+
195
+    /**
196
+     * Install property table, should run after mapping table due to
197
+     * foreign key reference.
198
+     *
199
+     * +--------------------+--------------+------+-----+---------+----------------+
200
+     * | Field              | Type         | Null | Key | Default | Extra          |
201
+     * +--------------------+--------------+------+-----+---------+----------------+
202
+     * | property_id        | int(11)      | NO   | PRI | NULL    | auto_increment |
203
+     * | mapping_id         | int(11)      | NO   | MUL | NULL    |                |
204
+     * | property_name      | varchar(255) | NO   |     | NULL    |                |
205
+     * | field_type         | varchar(255) | NO   |     | NULL    |                |
206
+     * | field_name         | varchar(255) | NO   |     | NULL    |                |
207
+     * | transform_function | varchar(255) | NO   |     | NULL    |                |
208
+     * | property_status    | varchar(255) | NO   |     | active  |                |
209
+     * +--------------------+--------------+------+-----+---------+----------------+
210
+     *
211
+     * @return void
212
+     * @since 3.25.0
213
+     */
214
+    public function create_property_table() {
215
+        $table_name      = $this->wpdb->prefix . WL_PROPERTY_TABLE_NAME;
216
+        $charset_collate = $this->wpdb->get_charset_collate();
217
+
218
+        $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME;
219
+        // @@todo do you we need the `property_` prefix? this is the property table anyway.
220
+        $sql = <<<EOF
221 221
         CREATE TABLE IF NOT EXISTS $table_name (
222 222
                 property_id INT(11) NOT NULL AUTO_INCREMENT,
223 223
                 mapping_id INT(11) NOT NULL,
@@ -231,18 +231,18 @@  discard block
 block discarded – undo
231 231
                 ON DELETE CASCADE
232 232
         ) $charset_collate;
233 233
 EOF;
234
-		// Execute the query for property table, we cant use db delta
235
-		// due to lack of support for foreign keys.
236
-		$this->wpdb->query( $sql );
237
-	}
238
-
239
-	public static function drop_tables() {
240
-		global $wpdb;
241
-
242
-		$wpdb->query( 'DROP TABLE IF EXISTS ' . $wpdb->prefix . WL_PROPERTY_TABLE_NAME );
243
-		$wpdb->query( 'DROP TABLE IF EXISTS ' . $wpdb->prefix . WL_RULE_TABLE_NAME );
244
-		$wpdb->query( 'DROP TABLE IF EXISTS ' . $wpdb->prefix . WL_RULE_GROUP_TABLE_NAME );
245
-		$wpdb->query( 'DROP TABLE IF EXISTS ' . $wpdb->prefix . WL_MAPPING_TABLE_NAME );
246
-	}
234
+        // Execute the query for property table, we cant use db delta
235
+        // due to lack of support for foreign keys.
236
+        $this->wpdb->query( $sql );
237
+    }
238
+
239
+    public static function drop_tables() {
240
+        global $wpdb;
241
+
242
+        $wpdb->query( 'DROP TABLE IF EXISTS ' . $wpdb->prefix . WL_PROPERTY_TABLE_NAME );
243
+        $wpdb->query( 'DROP TABLE IF EXISTS ' . $wpdb->prefix . WL_RULE_TABLE_NAME );
244
+        $wpdb->query( 'DROP TABLE IF EXISTS ' . $wpdb->prefix . WL_RULE_GROUP_TABLE_NAME );
245
+        $wpdb->query( 'DROP TABLE IF EXISTS ' . $wpdb->prefix . WL_MAPPING_TABLE_NAME );
246
+    }
247 247
 
248 248
 }
Please login to merge, or discard this patch.
src/includes/class-wordlift-entity-type-taxonomy-service.php 2 patches
Indentation   +150 added lines, -150 removed lines patch added patch discarded remove patch
@@ -14,160 +14,160 @@
 block discarded – undo
14 14
  */
15 15
 class Wordlift_Entity_Type_Taxonomy_Service {
16 16
 
17
-	/**
18
-	 * The WordPress taxonomy name.
19
-	 *
20
-	 * @since 1.0.0
21
-	 */
22
-	const TAXONOMY_NAME = 'wl_entity_type';
23
-
24
-	/**
25
-	 * Register the taxonomies.
26
-	 *
27
-	 * @since 3.23.6 we hook to `wp_get_object_terms` to ensure that a term is returned when a post is queries for the
28
-	 *               `wl_entity_type` taxonomy.
29
-	 * @since 3.18.0
30
-	 */
31
-	public function init() {
32
-
33
-		$labels = array(
34
-			'name'              => _x( 'Entity Types', 'taxonomy general name', 'wordlift' ),
35
-			'singular_name'     => _x( 'Entity Type', 'taxonomy singular name', 'wordlift' ),
36
-			'search_items'      => __( 'Search Entity Types', 'wordlift' ),
37
-			'all_items'         => __( 'All Entity Types', 'wordlift' ),
38
-			'parent_item'       => __( 'Parent Entity Type', 'wordlift' ),
39
-			'parent_item_colon' => __( 'Parent Entity Type:', 'wordlift' ),
40
-			'edit_item'         => __( 'Edit Entity Type', 'wordlift' ),
41
-			'update_item'       => __( 'Update Entity Type', 'wordlift' ),
42
-			'add_new_item'      => __( 'Add New Entity Type', 'wordlift' ),
43
-			'new_item_name'     => __( 'New Entity Type', 'wordlift' ),
44
-			'menu_name'         => __( 'Entity Types', 'wordlift' ),
45
-		);
46
-
47
-		// Take away GUI for taxonomy editing.
48
-		// TODO: read capabilities when editing of the WL <-> schema.org mapping is possible.
49
-		$capabilities = array(
50
-			// We enable editors to change the title/description of terms:
51
-			//
52
-			// @see https://github.com/insideout10/wordlift-plugin/issues/398.
53
-			'manage_terms' => 'manage_options',
54
-			'edit_terms'   => 'wl_entity_type_edit_term',
55
-			'delete_terms' => 'wl_entity_type_delete_term',
56
-			'assign_terms' => 'edit_posts',
57
-		);
58
-
59
-		$args = array(
60
-			'labels'             => $labels,
61
-			'capabilities'       => $capabilities,
62
-			'hierarchical'       => true,
63
-			'show_admin_column'  => true,
64
-			'show_in_rest'       => true,
65
-			'show_in_quick_edit' => false,
66
-		);
67
-
68
-		/*
17
+    /**
18
+     * The WordPress taxonomy name.
19
+     *
20
+     * @since 1.0.0
21
+     */
22
+    const TAXONOMY_NAME = 'wl_entity_type';
23
+
24
+    /**
25
+     * Register the taxonomies.
26
+     *
27
+     * @since 3.23.6 we hook to `wp_get_object_terms` to ensure that a term is returned when a post is queries for the
28
+     *               `wl_entity_type` taxonomy.
29
+     * @since 3.18.0
30
+     */
31
+    public function init() {
32
+
33
+        $labels = array(
34
+            'name'              => _x( 'Entity Types', 'taxonomy general name', 'wordlift' ),
35
+            'singular_name'     => _x( 'Entity Type', 'taxonomy singular name', 'wordlift' ),
36
+            'search_items'      => __( 'Search Entity Types', 'wordlift' ),
37
+            'all_items'         => __( 'All Entity Types', 'wordlift' ),
38
+            'parent_item'       => __( 'Parent Entity Type', 'wordlift' ),
39
+            'parent_item_colon' => __( 'Parent Entity Type:', 'wordlift' ),
40
+            'edit_item'         => __( 'Edit Entity Type', 'wordlift' ),
41
+            'update_item'       => __( 'Update Entity Type', 'wordlift' ),
42
+            'add_new_item'      => __( 'Add New Entity Type', 'wordlift' ),
43
+            'new_item_name'     => __( 'New Entity Type', 'wordlift' ),
44
+            'menu_name'         => __( 'Entity Types', 'wordlift' ),
45
+        );
46
+
47
+        // Take away GUI for taxonomy editing.
48
+        // TODO: read capabilities when editing of the WL <-> schema.org mapping is possible.
49
+        $capabilities = array(
50
+            // We enable editors to change the title/description of terms:
51
+            //
52
+            // @see https://github.com/insideout10/wordlift-plugin/issues/398.
53
+            'manage_terms' => 'manage_options',
54
+            'edit_terms'   => 'wl_entity_type_edit_term',
55
+            'delete_terms' => 'wl_entity_type_delete_term',
56
+            'assign_terms' => 'edit_posts',
57
+        );
58
+
59
+        $args = array(
60
+            'labels'             => $labels,
61
+            'capabilities'       => $capabilities,
62
+            'hierarchical'       => true,
63
+            'show_admin_column'  => true,
64
+            'show_in_rest'       => true,
65
+            'show_in_quick_edit' => false,
66
+        );
67
+
68
+        /*
69 69
 		 * If `All Entity Types` is enabled, use the new metabox.
70 70
 		 *
71 71
 		 * @see https://github.com/insideout10/wordlift-plugin/issues/835
72 72
 		 * @since 3.20.0
73 73
 		 */
74
-		if ( WL_ALL_ENTITY_TYPES ) {
75
-			$args['meta_box_cb'] = array( 'Wordlift_Admin_Schemaorg_Taxonomy_Metabox', 'render' );
76
-		}
77
-
78
-		register_taxonomy(
79
-			self::TAXONOMY_NAME, // Taxonomy name.
80
-			Wordlift_Entity_Service::valid_entity_post_types(), // Taxonomy post types.
81
-			$args // Taxonomy args.
82
-		);
83
-
84
-		/**
85
-		 * Register meta wl_entities_gutenberg for use in Gutenberg
86
-		 */
87
-		register_meta( 'post', 'wl_entities_gutenberg', array(
88
-			'show_in_rest' => true,
89
-			'single'       => true,
90
-			'type'         => 'string',
91
-		) );
92
-
93
-		/**
94
-		 * Register meta _wl_alt_label for use in Gutenberg
95
-		 */
96
-		register_meta( 'post', Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY, array(
97
-			'object_subtype' => '',
98
-			'show_in_rest'   => true,
99
-			'single'         => false,
100
-			'type'           => 'string',
101
-			'auth_callback'  => function () {
102
-				return current_user_can( 'edit_posts' );
103
-			}
104
-		) );
105
-
106
-		// Add filter to change the metabox CSS class.
107
-		add_filter( 'postbox_classes_entity_wl_entity_typediv', 'wl_admin_metaboxes_add_css_class' );
108
-
109
-		// Add a filter to preset the object term if none is set.
110
-		//
111
-		// DO NOT hook to `wp_get_object_terms`, because `wp_get_object_terms` returns imploded values for SQL queries.
112
-		//
113
-		// @see https://github.com/insideout10/wordlift-plugin/issues/995
114
-		// @since 3.23.6
115
-		add_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10, 4 );
116
-
117
-	}
118
-
119
-	/**
120
-	 * Hook to the `get_object_terms` filter.
121
-	 *
122
-	 * We check if our taxonomy is requested and whether a term has been returned. If no term has been returned we
123
-	 * preset `Article` for posts/pages and 'Thing' for everything else and we query the terms again.
124
-	 *
125
-	 * @param array $terms Array of terms for the given object or objects.
126
-	 * @param int[] $object_ids Array of object IDs for which terms were retrieved.
127
-	 * @param string[] $taxonomies Array of taxonomy names from which terms were retrieved.
128
-	 * @param array $args Array of arguments for retrieving terms for the given
129
-	 *                             object(s). See get_object_terms() for details.
130
-	 *
131
-	 * @return array|WP_Error
132
-	 * @since 3.23.6
133
-	 */
134
-	public function get_object_terms( $terms, $object_ids, $taxonomies, $args ) {
135
-		// Get our entity type.
136
-		$entity_type = self::TAXONOMY_NAME;
137
-
138
-		// Check if this is a query for our entity type, that no terms have been found and that we have an article
139
-		// term to preset in case.
140
-		if ( ! taxonomy_exists( $entity_type )
141
-		     || array( $entity_type ) !== (array) $taxonomies
142
-		     || ! empty( $terms )
143
-		     || ! term_exists( 'article', $entity_type )
144
-		     || ! term_exists( 'thing', $entity_type ) ) {
145
-
146
-			// Return the input value.
147
-			return $terms;
148
-		}
149
-
150
-		// Avoid nested calls in case of issues.
151
-		remove_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10 );
152
-
153
-		// Set the default term for all the queried object.
154
-		foreach ( (array) $object_ids as $object_id ) {
155
-			$post_type = get_post_type( $object_id );
156
-			if ( Wordlift_Entity_Type_Service::is_valid_entity_post_type( $post_type ) ) {
157
-				// Set the term to article for posts and pages, or to thing for everything else.
158
-				$term = Wordlift_Entity_Service::TYPE_NAME === $post_type
159
-					? 'thing' : 'article';
160
-				wp_set_object_terms( $object_id, $term, $entity_type, true );
161
-			}
162
-		}
163
-
164
-		// Finally return the object terms.
165
-		$terms = wp_get_object_terms( $object_ids, $taxonomies, $args );
166
-
167
-		// Re-enable nested calls in case of issues.
168
-		add_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10, 4 );
169
-
170
-		return $terms;
171
-	}
74
+        if ( WL_ALL_ENTITY_TYPES ) {
75
+            $args['meta_box_cb'] = array( 'Wordlift_Admin_Schemaorg_Taxonomy_Metabox', 'render' );
76
+        }
77
+
78
+        register_taxonomy(
79
+            self::TAXONOMY_NAME, // Taxonomy name.
80
+            Wordlift_Entity_Service::valid_entity_post_types(), // Taxonomy post types.
81
+            $args // Taxonomy args.
82
+        );
83
+
84
+        /**
85
+         * Register meta wl_entities_gutenberg for use in Gutenberg
86
+         */
87
+        register_meta( 'post', 'wl_entities_gutenberg', array(
88
+            'show_in_rest' => true,
89
+            'single'       => true,
90
+            'type'         => 'string',
91
+        ) );
92
+
93
+        /**
94
+         * Register meta _wl_alt_label for use in Gutenberg
95
+         */
96
+        register_meta( 'post', Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY, array(
97
+            'object_subtype' => '',
98
+            'show_in_rest'   => true,
99
+            'single'         => false,
100
+            'type'           => 'string',
101
+            'auth_callback'  => function () {
102
+                return current_user_can( 'edit_posts' );
103
+            }
104
+        ) );
105
+
106
+        // Add filter to change the metabox CSS class.
107
+        add_filter( 'postbox_classes_entity_wl_entity_typediv', 'wl_admin_metaboxes_add_css_class' );
108
+
109
+        // Add a filter to preset the object term if none is set.
110
+        //
111
+        // DO NOT hook to `wp_get_object_terms`, because `wp_get_object_terms` returns imploded values for SQL queries.
112
+        //
113
+        // @see https://github.com/insideout10/wordlift-plugin/issues/995
114
+        // @since 3.23.6
115
+        add_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10, 4 );
116
+
117
+    }
118
+
119
+    /**
120
+     * Hook to the `get_object_terms` filter.
121
+     *
122
+     * We check if our taxonomy is requested and whether a term has been returned. If no term has been returned we
123
+     * preset `Article` for posts/pages and 'Thing' for everything else and we query the terms again.
124
+     *
125
+     * @param array $terms Array of terms for the given object or objects.
126
+     * @param int[] $object_ids Array of object IDs for which terms were retrieved.
127
+     * @param string[] $taxonomies Array of taxonomy names from which terms were retrieved.
128
+     * @param array $args Array of arguments for retrieving terms for the given
129
+     *                             object(s). See get_object_terms() for details.
130
+     *
131
+     * @return array|WP_Error
132
+     * @since 3.23.6
133
+     */
134
+    public function get_object_terms( $terms, $object_ids, $taxonomies, $args ) {
135
+        // Get our entity type.
136
+        $entity_type = self::TAXONOMY_NAME;
137
+
138
+        // Check if this is a query for our entity type, that no terms have been found and that we have an article
139
+        // term to preset in case.
140
+        if ( ! taxonomy_exists( $entity_type )
141
+             || array( $entity_type ) !== (array) $taxonomies
142
+             || ! empty( $terms )
143
+             || ! term_exists( 'article', $entity_type )
144
+             || ! term_exists( 'thing', $entity_type ) ) {
145
+
146
+            // Return the input value.
147
+            return $terms;
148
+        }
149
+
150
+        // Avoid nested calls in case of issues.
151
+        remove_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10 );
152
+
153
+        // Set the default term for all the queried object.
154
+        foreach ( (array) $object_ids as $object_id ) {
155
+            $post_type = get_post_type( $object_id );
156
+            if ( Wordlift_Entity_Type_Service::is_valid_entity_post_type( $post_type ) ) {
157
+                // Set the term to article for posts and pages, or to thing for everything else.
158
+                $term = Wordlift_Entity_Service::TYPE_NAME === $post_type
159
+                    ? 'thing' : 'article';
160
+                wp_set_object_terms( $object_id, $term, $entity_type, true );
161
+            }
162
+        }
163
+
164
+        // Finally return the object terms.
165
+        $terms = wp_get_object_terms( $object_ids, $taxonomies, $args );
166
+
167
+        // Re-enable nested calls in case of issues.
168
+        add_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10, 4 );
169
+
170
+        return $terms;
171
+    }
172 172
 
173 173
 }
Please login to merge, or discard this patch.
Spacing   +34 added lines, -34 removed lines patch added patch discarded remove patch
@@ -31,17 +31,17 @@  discard block
 block discarded – undo
31 31
 	public function init() {
32 32
 
33 33
 		$labels = array(
34
-			'name'              => _x( 'Entity Types', 'taxonomy general name', 'wordlift' ),
35
-			'singular_name'     => _x( 'Entity Type', 'taxonomy singular name', 'wordlift' ),
36
-			'search_items'      => __( 'Search Entity Types', 'wordlift' ),
37
-			'all_items'         => __( 'All Entity Types', 'wordlift' ),
38
-			'parent_item'       => __( 'Parent Entity Type', 'wordlift' ),
39
-			'parent_item_colon' => __( 'Parent Entity Type:', 'wordlift' ),
40
-			'edit_item'         => __( 'Edit Entity Type', 'wordlift' ),
41
-			'update_item'       => __( 'Update Entity Type', 'wordlift' ),
42
-			'add_new_item'      => __( 'Add New Entity Type', 'wordlift' ),
43
-			'new_item_name'     => __( 'New Entity Type', 'wordlift' ),
44
-			'menu_name'         => __( 'Entity Types', 'wordlift' ),
34
+			'name'              => _x('Entity Types', 'taxonomy general name', 'wordlift'),
35
+			'singular_name'     => _x('Entity Type', 'taxonomy singular name', 'wordlift'),
36
+			'search_items'      => __('Search Entity Types', 'wordlift'),
37
+			'all_items'         => __('All Entity Types', 'wordlift'),
38
+			'parent_item'       => __('Parent Entity Type', 'wordlift'),
39
+			'parent_item_colon' => __('Parent Entity Type:', 'wordlift'),
40
+			'edit_item'         => __('Edit Entity Type', 'wordlift'),
41
+			'update_item'       => __('Update Entity Type', 'wordlift'),
42
+			'add_new_item'      => __('Add New Entity Type', 'wordlift'),
43
+			'new_item_name'     => __('New Entity Type', 'wordlift'),
44
+			'menu_name'         => __('Entity Types', 'wordlift'),
45 45
 		);
46 46
 
47 47
 		// Take away GUI for taxonomy editing.
@@ -71,8 +71,8 @@  discard block
 block discarded – undo
71 71
 		 * @see https://github.com/insideout10/wordlift-plugin/issues/835
72 72
 		 * @since 3.20.0
73 73
 		 */
74
-		if ( WL_ALL_ENTITY_TYPES ) {
75
-			$args['meta_box_cb'] = array( 'Wordlift_Admin_Schemaorg_Taxonomy_Metabox', 'render' );
74
+		if (WL_ALL_ENTITY_TYPES) {
75
+			$args['meta_box_cb'] = array('Wordlift_Admin_Schemaorg_Taxonomy_Metabox', 'render');
76 76
 		}
77 77
 
78 78
 		register_taxonomy(
@@ -84,27 +84,27 @@  discard block
 block discarded – undo
84 84
 		/**
85 85
 		 * Register meta wl_entities_gutenberg for use in Gutenberg
86 86
 		 */
87
-		register_meta( 'post', 'wl_entities_gutenberg', array(
87
+		register_meta('post', 'wl_entities_gutenberg', array(
88 88
 			'show_in_rest' => true,
89 89
 			'single'       => true,
90 90
 			'type'         => 'string',
91
-		) );
91
+		));
92 92
 
93 93
 		/**
94 94
 		 * Register meta _wl_alt_label for use in Gutenberg
95 95
 		 */
96
-		register_meta( 'post', Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY, array(
96
+		register_meta('post', Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY, array(
97 97
 			'object_subtype' => '',
98 98
 			'show_in_rest'   => true,
99 99
 			'single'         => false,
100 100
 			'type'           => 'string',
101
-			'auth_callback'  => function () {
102
-				return current_user_can( 'edit_posts' );
101
+			'auth_callback'  => function() {
102
+				return current_user_can('edit_posts');
103 103
 			}
104
-		) );
104
+		));
105 105
 
106 106
 		// Add filter to change the metabox CSS class.
107
-		add_filter( 'postbox_classes_entity_wl_entity_typediv', 'wl_admin_metaboxes_add_css_class' );
107
+		add_filter('postbox_classes_entity_wl_entity_typediv', 'wl_admin_metaboxes_add_css_class');
108 108
 
109 109
 		// Add a filter to preset the object term if none is set.
110 110
 		//
@@ -112,7 +112,7 @@  discard block
 block discarded – undo
112 112
 		//
113 113
 		// @see https://github.com/insideout10/wordlift-plugin/issues/995
114 114
 		// @since 3.23.6
115
-		add_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10, 4 );
115
+		add_filter('get_object_terms', array($this, 'get_object_terms'), 10, 4);
116 116
 
117 117
 	}
118 118
 
@@ -131,41 +131,41 @@  discard block
 block discarded – undo
131 131
 	 * @return array|WP_Error
132 132
 	 * @since 3.23.6
133 133
 	 */
134
-	public function get_object_terms( $terms, $object_ids, $taxonomies, $args ) {
134
+	public function get_object_terms($terms, $object_ids, $taxonomies, $args) {
135 135
 		// Get our entity type.
136 136
 		$entity_type = self::TAXONOMY_NAME;
137 137
 
138 138
 		// Check if this is a query for our entity type, that no terms have been found and that we have an article
139 139
 		// term to preset in case.
140
-		if ( ! taxonomy_exists( $entity_type )
141
-		     || array( $entity_type ) !== (array) $taxonomies
142
-		     || ! empty( $terms )
143
-		     || ! term_exists( 'article', $entity_type )
144
-		     || ! term_exists( 'thing', $entity_type ) ) {
140
+		if ( ! taxonomy_exists($entity_type)
141
+		     || array($entity_type) !== (array) $taxonomies
142
+		     || ! empty($terms)
143
+		     || ! term_exists('article', $entity_type)
144
+		     || ! term_exists('thing', $entity_type)) {
145 145
 
146 146
 			// Return the input value.
147 147
 			return $terms;
148 148
 		}
149 149
 
150 150
 		// Avoid nested calls in case of issues.
151
-		remove_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10 );
151
+		remove_filter('get_object_terms', array($this, 'get_object_terms'), 10);
152 152
 
153 153
 		// Set the default term for all the queried object.
154
-		foreach ( (array) $object_ids as $object_id ) {
155
-			$post_type = get_post_type( $object_id );
156
-			if ( Wordlift_Entity_Type_Service::is_valid_entity_post_type( $post_type ) ) {
154
+		foreach ((array) $object_ids as $object_id) {
155
+			$post_type = get_post_type($object_id);
156
+			if (Wordlift_Entity_Type_Service::is_valid_entity_post_type($post_type)) {
157 157
 				// Set the term to article for posts and pages, or to thing for everything else.
158 158
 				$term = Wordlift_Entity_Service::TYPE_NAME === $post_type
159 159
 					? 'thing' : 'article';
160
-				wp_set_object_terms( $object_id, $term, $entity_type, true );
160
+				wp_set_object_terms($object_id, $term, $entity_type, true);
161 161
 			}
162 162
 		}
163 163
 
164 164
 		// Finally return the object terms.
165
-		$terms = wp_get_object_terms( $object_ids, $taxonomies, $args );
165
+		$terms = wp_get_object_terms($object_ids, $taxonomies, $args);
166 166
 
167 167
 		// Re-enable nested calls in case of issues.
168
-		add_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10, 4 );
168
+		add_filter('get_object_terms', array($this, 'get_object_terms'), 10, 4);
169 169
 
170 170
 		return $terms;
171 171
 	}
Please login to merge, or discard this patch.