Completed
Push — master ( 182e17...b61276 )
by David
02:29
created
src/includes/class-wordlift-entity-type-taxonomy-service.php 2 patches
Indentation   +137 added lines, -137 removed lines patch added patch discarded remove patch
@@ -14,147 +14,147 @@
 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
-		// Add filter to change the metabox CSS class.
94
-		add_filter( 'postbox_classes_entity_wl_entity_typediv', 'wl_admin_metaboxes_add_css_class' );
95
-
96
-		// Add a filter to preset the object term if none is set.
97
-		//
98
-		// DO NOT hook to `wp_get_object_terms`, because `wp_get_object_terms` returns imploded values for SQL queries.
99
-		//
100
-		// @see https://github.com/insideout10/wordlift-plugin/issues/995
101
-		// @since 3.23.6
102
-		add_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10, 4 );
103
-
104
-	}
105
-
106
-	/**
107
-	 * Hook to the `get_object_terms` filter.
108
-	 *
109
-	 * We check if our taxonomy is requested and whether a term has been returned. If no term has been returned we
110
-	 * preset `Article` for posts/pages and 'Thing' for everything else and we query the terms again.
111
-	 *
112
-	 * @param array $terms Array of terms for the given object or objects.
113
-	 * @param int[] $object_ids Array of object IDs for which terms were retrieved.
114
-	 * @param string[] $taxonomies Array of taxonomy names from which terms were retrieved.
115
-	 * @param array $args Array of arguments for retrieving terms for the given
116
-	 *                             object(s). See get_object_terms() for details.
117
-	 *
118
-	 * @return array|WP_Error
119
-	 * @since 3.23.6
120
-	 */
121
-	public function get_object_terms( $terms, $object_ids, $taxonomies, $args ) {
122
-		// Get our entity type.
123
-		$entity_type = self::TAXONOMY_NAME;
124
-
125
-		// Check if this is a query for our entity type, that no terms have been found and that we have an article
126
-		// term to preset in case.
127
-		if ( ! taxonomy_exists( $entity_type )
128
-		     || array( $entity_type ) !== (array) $taxonomies
129
-		     || ! empty( $terms )
130
-		     || ! term_exists( 'article', $entity_type )
131
-		     || ! term_exists( 'thing', $entity_type ) ) {
132
-
133
-			// Return the input value.
134
-			return $terms;
135
-		}
136
-
137
-		// Avoid nested calls in case of issues.
138
-		remove_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10 );
139
-
140
-		// Set the default term for all the queried object.
141
-		foreach ( (array) $object_ids as $object_id ) {
142
-			$post_type = get_post_type( $object_id );
143
-			if ( Wordlift_Entity_Type_Service::is_valid_entity_post_type( $post_type ) ) {
144
-				// Set the term to article for posts and pages, or to thing for everything else.
145
-				$term = Wordlift_Entity_Service::TYPE_NAME === $post_type
146
-					? 'thing' : 'article';
147
-				wp_set_object_terms( $object_id, $term, $entity_type, true );
148
-			}
149
-		}
150
-
151
-		// Finally return the object terms.
152
-		$terms = wp_get_object_terms( $object_ids, $taxonomies, $args );
153
-
154
-		// Re-enable nested calls in case of issues.
155
-		add_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10, 4 );
156
-
157
-		return $terms;
158
-	}
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
+        // Add filter to change the metabox CSS class.
94
+        add_filter( 'postbox_classes_entity_wl_entity_typediv', 'wl_admin_metaboxes_add_css_class' );
95
+
96
+        // Add a filter to preset the object term if none is set.
97
+        //
98
+        // DO NOT hook to `wp_get_object_terms`, because `wp_get_object_terms` returns imploded values for SQL queries.
99
+        //
100
+        // @see https://github.com/insideout10/wordlift-plugin/issues/995
101
+        // @since 3.23.6
102
+        add_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10, 4 );
103
+
104
+    }
105
+
106
+    /**
107
+     * Hook to the `get_object_terms` filter.
108
+     *
109
+     * We check if our taxonomy is requested and whether a term has been returned. If no term has been returned we
110
+     * preset `Article` for posts/pages and 'Thing' for everything else and we query the terms again.
111
+     *
112
+     * @param array $terms Array of terms for the given object or objects.
113
+     * @param int[] $object_ids Array of object IDs for which terms were retrieved.
114
+     * @param string[] $taxonomies Array of taxonomy names from which terms were retrieved.
115
+     * @param array $args Array of arguments for retrieving terms for the given
116
+     *                             object(s). See get_object_terms() for details.
117
+     *
118
+     * @return array|WP_Error
119
+     * @since 3.23.6
120
+     */
121
+    public function get_object_terms( $terms, $object_ids, $taxonomies, $args ) {
122
+        // Get our entity type.
123
+        $entity_type = self::TAXONOMY_NAME;
124
+
125
+        // Check if this is a query for our entity type, that no terms have been found and that we have an article
126
+        // term to preset in case.
127
+        if ( ! taxonomy_exists( $entity_type )
128
+             || array( $entity_type ) !== (array) $taxonomies
129
+             || ! empty( $terms )
130
+             || ! term_exists( 'article', $entity_type )
131
+             || ! term_exists( 'thing', $entity_type ) ) {
132
+
133
+            // Return the input value.
134
+            return $terms;
135
+        }
136
+
137
+        // Avoid nested calls in case of issues.
138
+        remove_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10 );
139
+
140
+        // Set the default term for all the queried object.
141
+        foreach ( (array) $object_ids as $object_id ) {
142
+            $post_type = get_post_type( $object_id );
143
+            if ( Wordlift_Entity_Type_Service::is_valid_entity_post_type( $post_type ) ) {
144
+                // Set the term to article for posts and pages, or to thing for everything else.
145
+                $term = Wordlift_Entity_Service::TYPE_NAME === $post_type
146
+                    ? 'thing' : 'article';
147
+                wp_set_object_terms( $object_id, $term, $entity_type, true );
148
+            }
149
+        }
150
+
151
+        // Finally return the object terms.
152
+        $terms = wp_get_object_terms( $object_ids, $taxonomies, $args );
153
+
154
+        // Re-enable nested calls in case of issues.
155
+        add_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10, 4 );
156
+
157
+        return $terms;
158
+    }
159 159
 
160 160
 }
Please login to merge, or discard this patch.
Spacing   +30 added lines, -30 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,14 +84,14 @@  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
 		// Add filter to change the metabox CSS class.
94
-		add_filter( 'postbox_classes_entity_wl_entity_typediv', 'wl_admin_metaboxes_add_css_class' );
94
+		add_filter('postbox_classes_entity_wl_entity_typediv', 'wl_admin_metaboxes_add_css_class');
95 95
 
96 96
 		// Add a filter to preset the object term if none is set.
97 97
 		//
@@ -99,7 +99,7 @@  discard block
 block discarded – undo
99 99
 		//
100 100
 		// @see https://github.com/insideout10/wordlift-plugin/issues/995
101 101
 		// @since 3.23.6
102
-		add_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10, 4 );
102
+		add_filter('get_object_terms', array($this, 'get_object_terms'), 10, 4);
103 103
 
104 104
 	}
105 105
 
@@ -118,41 +118,41 @@  discard block
 block discarded – undo
118 118
 	 * @return array|WP_Error
119 119
 	 * @since 3.23.6
120 120
 	 */
121
-	public function get_object_terms( $terms, $object_ids, $taxonomies, $args ) {
121
+	public function get_object_terms($terms, $object_ids, $taxonomies, $args) {
122 122
 		// Get our entity type.
123 123
 		$entity_type = self::TAXONOMY_NAME;
124 124
 
125 125
 		// Check if this is a query for our entity type, that no terms have been found and that we have an article
126 126
 		// term to preset in case.
127
-		if ( ! taxonomy_exists( $entity_type )
128
-		     || array( $entity_type ) !== (array) $taxonomies
129
-		     || ! empty( $terms )
130
-		     || ! term_exists( 'article', $entity_type )
131
-		     || ! term_exists( 'thing', $entity_type ) ) {
127
+		if ( ! taxonomy_exists($entity_type)
128
+		     || array($entity_type) !== (array) $taxonomies
129
+		     || ! empty($terms)
130
+		     || ! term_exists('article', $entity_type)
131
+		     || ! term_exists('thing', $entity_type)) {
132 132
 
133 133
 			// Return the input value.
134 134
 			return $terms;
135 135
 		}
136 136
 
137 137
 		// Avoid nested calls in case of issues.
138
-		remove_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10 );
138
+		remove_filter('get_object_terms', array($this, 'get_object_terms'), 10);
139 139
 
140 140
 		// Set the default term for all the queried object.
141
-		foreach ( (array) $object_ids as $object_id ) {
142
-			$post_type = get_post_type( $object_id );
143
-			if ( Wordlift_Entity_Type_Service::is_valid_entity_post_type( $post_type ) ) {
141
+		foreach ((array) $object_ids as $object_id) {
142
+			$post_type = get_post_type($object_id);
143
+			if (Wordlift_Entity_Type_Service::is_valid_entity_post_type($post_type)) {
144 144
 				// Set the term to article for posts and pages, or to thing for everything else.
145 145
 				$term = Wordlift_Entity_Service::TYPE_NAME === $post_type
146 146
 					? 'thing' : 'article';
147
-				wp_set_object_terms( $object_id, $term, $entity_type, true );
147
+				wp_set_object_terms($object_id, $term, $entity_type, true);
148 148
 			}
149 149
 		}
150 150
 
151 151
 		// Finally return the object terms.
152
-		$terms = wp_get_object_terms( $object_ids, $taxonomies, $args );
152
+		$terms = wp_get_object_terms($object_ids, $taxonomies, $args);
153 153
 
154 154
 		// Re-enable nested calls in case of issues.
155
-		add_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10, 4 );
155
+		add_filter('get_object_terms', array($this, 'get_object_terms'), 10, 4);
156 156
 
157 157
 		return $terms;
158 158
 	}
Please login to merge, or discard this patch.