Completed
Branch fix/category-count-issue (cab1cb)
by
unknown
26:18 queued 24:06
created
core/db_models/EEM_Term_Relationship.model.php 2 patches
Indentation   +227 added lines, -227 removed lines patch added patch discarded remove patch
@@ -9,252 +9,252 @@
 block discarded – undo
9 9
  */
10 10
 class EEM_Term_Relationship extends EEM_Base
11 11
 {
12
-    // private instance of the Attendee object
13
-    protected static $_instance = null;
12
+	// private instance of the Attendee object
13
+	protected static $_instance = null;
14 14
 
15 15
 
16
-    /**
17
-     * EEM_Term_Relationship constructor.
18
-     *
19
-     * @param string $timezone
20
-     * @throws EE_Error
21
-     */
22
-    protected function __construct($timezone = null)
23
-    {
24
-        $this->singular_item       = esc_html__('Term Relationship', 'event_espresso');
25
-        $this->plural_item         = esc_html__('Term Relationships', 'event_espresso');
26
-        $this->_tables             = [
27
-            'Term_Relationship' => new EE_Primary_Table('term_relationships'),
28
-        ];
29
-        $models_this_can_attach_to = array_keys(EE_Registry::instance()->cpt_models());
30
-        $this->_fields             = [
31
-            'Term_Relationship' => [
32
-                'object_id'        => new EE_Foreign_Key_Int_Field(
33
-                    'object_id',
34
-                    esc_html__('Object(Post) ID', 'event_espresso'),
35
-                    false,
36
-                    0,
37
-                    $models_this_can_attach_to
38
-                ),
39
-                'term_taxonomy_id' => new EE_Foreign_Key_Int_Field(
40
-                    'term_taxonomy_id',
41
-                    esc_html__(
42
-                        'Term (in context of a taxonomy) ID',
43
-                        'event_espresso'
44
-                    ),
45
-                    false,
46
-                    0,
47
-                    'Term_Taxonomy'
48
-                ),
49
-                'term_order'       => new EE_Integer_Field(
50
-                    'term_order',
51
-                    esc_html__('Term Order', 'event_espresso'),
52
-                    false,
53
-                    0
54
-                ),
55
-            ],
56
-        ];
57
-        $this->_model_relations    = [
58
-            'Term_Taxonomy' => new EE_Belongs_To_Relation(),
59
-        ];
60
-        foreach ($models_this_can_attach_to as $model_name) {
61
-            $this->_model_relations[ $model_name ] = new EE_Belongs_To_Relation();
62
-        }
63
-        $this->_wp_core_model                                           = true;
64
-        $this->_indexes                                                 = [
65
-            'PRIMARY' => new EE_Primary_Key_Index(['object_id', 'term_taxonomy_id']),
66
-        ];
67
-        $path_to_event_model                                            = 'Event';
68
-        $this->_cap_restriction_generators[ EEM_Base::caps_read ]       =
69
-            new EE_Restriction_Generator_Event_Related_Public(
70
-                $path_to_event_model
71
-            );
72
-        $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] =
73
-            new EE_Restriction_Generator_Event_Related_Protected(
74
-                $path_to_event_model
75
-            );
76
-        $this->_cap_restriction_generators[ EEM_Base::caps_edit ]       =
77
-            new EE_Restriction_Generator_Event_Related_Protected(
78
-                $path_to_event_model
79
-            );
80
-        $this->_cap_restriction_generators[ EEM_Base::caps_delete ]     =
81
-            new EE_Restriction_Generator_Event_Related_Protected(
82
-                $path_to_event_model,
83
-                EEM_Base::caps_edit
84
-            );
85
-        $path_to_tax_model                                              = 'Term_Taxonomy.';
86
-        // add cap restrictions for editing term relations to the "ee_assign_*"
87
-        // and for deleting term relations too
88
-        $cap_contexts_affected = [EEM_Base::caps_edit, EEM_Base::caps_delete];
89
-        foreach ($cap_contexts_affected as $cap_context_affected) {
90
-            $this->_cap_restrictions[ $cap_context_affected ]['ee_assign_event_category'] =
91
-                new EE_Default_Where_Conditions(
92
-                    [
93
-                        $path_to_tax_model . 'taxonomy*ee_assign_event_category' => [
94
-                            '!=',
95
-                            'espresso_event_categories',
96
-                        ],
97
-                    ]
98
-                );
99
-            $this->_cap_restrictions[ $cap_context_affected ]['ee_assign_venue_category'] =
100
-                new EE_Default_Where_Conditions(
101
-                    [
102
-                        $path_to_tax_model . 'taxonomy*ee_assign_venue_category' => [
103
-                            '!=',
104
-                            'espresso_venue_categories',
105
-                        ],
106
-                    ]
107
-                );
108
-            $this->_cap_restrictions[ $cap_context_affected ]['ee_assign_event_type']     =
109
-                new EE_Default_Where_Conditions(
110
-                    [
111
-                        $path_to_tax_model . 'taxonomy*ee_assign_event_type' => ['!=', 'espresso_event_type'],
112
-                    ]
113
-                );
114
-        }
115
-        parent::__construct($timezone);
116
-        add_filter(
117
-            'FHEE__Read__create_model_query_params',
118
-            ['EEM_Term_Relationship', 'rest_api_query_params'],
119
-            10,
120
-            3
121
-        );
122
-    }
16
+	/**
17
+	 * EEM_Term_Relationship constructor.
18
+	 *
19
+	 * @param string $timezone
20
+	 * @throws EE_Error
21
+	 */
22
+	protected function __construct($timezone = null)
23
+	{
24
+		$this->singular_item       = esc_html__('Term Relationship', 'event_espresso');
25
+		$this->plural_item         = esc_html__('Term Relationships', 'event_espresso');
26
+		$this->_tables             = [
27
+			'Term_Relationship' => new EE_Primary_Table('term_relationships'),
28
+		];
29
+		$models_this_can_attach_to = array_keys(EE_Registry::instance()->cpt_models());
30
+		$this->_fields             = [
31
+			'Term_Relationship' => [
32
+				'object_id'        => new EE_Foreign_Key_Int_Field(
33
+					'object_id',
34
+					esc_html__('Object(Post) ID', 'event_espresso'),
35
+					false,
36
+					0,
37
+					$models_this_can_attach_to
38
+				),
39
+				'term_taxonomy_id' => new EE_Foreign_Key_Int_Field(
40
+					'term_taxonomy_id',
41
+					esc_html__(
42
+						'Term (in context of a taxonomy) ID',
43
+						'event_espresso'
44
+					),
45
+					false,
46
+					0,
47
+					'Term_Taxonomy'
48
+				),
49
+				'term_order'       => new EE_Integer_Field(
50
+					'term_order',
51
+					esc_html__('Term Order', 'event_espresso'),
52
+					false,
53
+					0
54
+				),
55
+			],
56
+		];
57
+		$this->_model_relations    = [
58
+			'Term_Taxonomy' => new EE_Belongs_To_Relation(),
59
+		];
60
+		foreach ($models_this_can_attach_to as $model_name) {
61
+			$this->_model_relations[ $model_name ] = new EE_Belongs_To_Relation();
62
+		}
63
+		$this->_wp_core_model                                           = true;
64
+		$this->_indexes                                                 = [
65
+			'PRIMARY' => new EE_Primary_Key_Index(['object_id', 'term_taxonomy_id']),
66
+		];
67
+		$path_to_event_model                                            = 'Event';
68
+		$this->_cap_restriction_generators[ EEM_Base::caps_read ]       =
69
+			new EE_Restriction_Generator_Event_Related_Public(
70
+				$path_to_event_model
71
+			);
72
+		$this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] =
73
+			new EE_Restriction_Generator_Event_Related_Protected(
74
+				$path_to_event_model
75
+			);
76
+		$this->_cap_restriction_generators[ EEM_Base::caps_edit ]       =
77
+			new EE_Restriction_Generator_Event_Related_Protected(
78
+				$path_to_event_model
79
+			);
80
+		$this->_cap_restriction_generators[ EEM_Base::caps_delete ]     =
81
+			new EE_Restriction_Generator_Event_Related_Protected(
82
+				$path_to_event_model,
83
+				EEM_Base::caps_edit
84
+			);
85
+		$path_to_tax_model                                              = 'Term_Taxonomy.';
86
+		// add cap restrictions for editing term relations to the "ee_assign_*"
87
+		// and for deleting term relations too
88
+		$cap_contexts_affected = [EEM_Base::caps_edit, EEM_Base::caps_delete];
89
+		foreach ($cap_contexts_affected as $cap_context_affected) {
90
+			$this->_cap_restrictions[ $cap_context_affected ]['ee_assign_event_category'] =
91
+				new EE_Default_Where_Conditions(
92
+					[
93
+						$path_to_tax_model . 'taxonomy*ee_assign_event_category' => [
94
+							'!=',
95
+							'espresso_event_categories',
96
+						],
97
+					]
98
+				);
99
+			$this->_cap_restrictions[ $cap_context_affected ]['ee_assign_venue_category'] =
100
+				new EE_Default_Where_Conditions(
101
+					[
102
+						$path_to_tax_model . 'taxonomy*ee_assign_venue_category' => [
103
+							'!=',
104
+							'espresso_venue_categories',
105
+						],
106
+					]
107
+				);
108
+			$this->_cap_restrictions[ $cap_context_affected ]['ee_assign_event_type']     =
109
+				new EE_Default_Where_Conditions(
110
+					[
111
+						$path_to_tax_model . 'taxonomy*ee_assign_event_type' => ['!=', 'espresso_event_type'],
112
+					]
113
+				);
114
+		}
115
+		parent::__construct($timezone);
116
+		add_filter(
117
+			'FHEE__Read__create_model_query_params',
118
+			['EEM_Term_Relationship', 'rest_api_query_params'],
119
+			10,
120
+			3
121
+		);
122
+	}
123 123
 
124 124
 
125
-    /**
126
-     * Makes sure all term-taxonomy counts are correct
127
-     *
128
-     * @param int   $term_taxonomy_id the id of the term taxonomy to update. If NULL, updates ALL
129
-     * @return int the number of rows affected
130
-     * @throws EE_Error
131
-     * @global wpdb $wpdb
132
-     */
133
-    public function update_term_taxonomy_counts($term_taxonomy_id = null)
134
-    {
135
-        // because this uses a subquery and sometimes assigning to column to be another column's
136
-        // value, we just write the SQL directly.
137
-        global $wpdb;
125
+	/**
126
+	 * Makes sure all term-taxonomy counts are correct
127
+	 *
128
+	 * @param int   $term_taxonomy_id the id of the term taxonomy to update. If NULL, updates ALL
129
+	 * @return int the number of rows affected
130
+	 * @throws EE_Error
131
+	 * @global wpdb $wpdb
132
+	 */
133
+	public function update_term_taxonomy_counts($term_taxonomy_id = null)
134
+	{
135
+		// because this uses a subquery and sometimes assigning to column to be another column's
136
+		// value, we just write the SQL directly.
137
+		global $wpdb;
138 138
 
139
-        $query = "
139
+		$query = "
140 140
                 UPDATE {$wpdb->term_taxonomy} AS tt 
141 141
                 SET count = (
142 142
                     select count(*) as proper_count from {$wpdb->term_relationships} AS tr 
143 143
                     WHERE tt.term_taxonomy_id = tr.term_taxonomy_id
144 144
                 )";
145 145
 
146
-        if ($term_taxonomy_id) {
147
-            $query .= ' WHERE tt.term_taxonomy_id = %d';
148
-            $query = $wpdb->prepare(
149
-                $query,
150
-                $term_taxonomy_id
151
-            );
152
-        }
153
-        return $this->_do_wpdb_query(
154
-            'query',
155
-            [
156
-                $query,
157
-            ]
158
-        );
159
-    }
146
+		if ($term_taxonomy_id) {
147
+			$query .= ' WHERE tt.term_taxonomy_id = %d';
148
+			$query = $wpdb->prepare(
149
+				$query,
150
+				$term_taxonomy_id
151
+			);
152
+		}
153
+		return $this->_do_wpdb_query(
154
+			'query',
155
+			[
156
+				$query,
157
+			]
158
+		);
159
+	}
160 160
 
161 161
 
162
-    /**
163
-     * Overrides the parent to also make sure term-taxonomy counts are up-to-date after
164
-     * inserting
165
-     *
166
-     * @param array $field_n_values
167
-     * @return bool
168
-     * @throws EE_Error
169
-     * @see EEM_Base::insert
170
-     */
171
-    public function insert($field_n_values)
172
-    {
173
-        $return = parent::insert($field_n_values);
174
-        if (isset($field_n_values['term_taxonomy_id'])) {
175
-            $this->update_term_taxonomy_counts($field_n_values['term_taxonomy_id']);
176
-        }
177
-        return $return;
178
-    }
162
+	/**
163
+	 * Overrides the parent to also make sure term-taxonomy counts are up-to-date after
164
+	 * inserting
165
+	 *
166
+	 * @param array $field_n_values
167
+	 * @return bool
168
+	 * @throws EE_Error
169
+	 * @see EEM_Base::insert
170
+	 */
171
+	public function insert($field_n_values)
172
+	{
173
+		$return = parent::insert($field_n_values);
174
+		if (isset($field_n_values['term_taxonomy_id'])) {
175
+			$this->update_term_taxonomy_counts($field_n_values['term_taxonomy_id']);
176
+		}
177
+		return $return;
178
+	}
179 179
 
180 180
 
181
-    /**
182
-     * Overrides parent so that after an update, we also check the term_taxonomy_counts are
183
-     * all ok
184
-     *
185
-     * @param array $fields_n_values           see EEM_Base::update
186
-     * @param array $query_params
187
-     * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
188
-     * @param bool  $keep_model_objs_in_sync   if TRUE, makes sure we ALSO update model objects
189
-     *                                         in this model's entity map according to $fields_n_values that match
190
-     *                                         $query_params. This obviously has some overhead, so you can disable it
191
-     *                                         by setting this to FALSE, but be aware that model objects being used
192
-     *                                         could get out-of-sync with the database
193
-     * @return int
194
-     * @throws EE_Error
195
-     */
196
-    public function update($fields_n_values, $query_params, $keep_model_objs_in_sync = true)
197
-    {
198
-        $count = parent::update($fields_n_values, $query_params, $keep_model_objs_in_sync);
199
-        if ($count) {
200
-            $this->update_term_taxonomy_counts();
201
-        }
202
-        return $count;
203
-    }
181
+	/**
182
+	 * Overrides parent so that after an update, we also check the term_taxonomy_counts are
183
+	 * all ok
184
+	 *
185
+	 * @param array $fields_n_values           see EEM_Base::update
186
+	 * @param array $query_params
187
+	 * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
188
+	 * @param bool  $keep_model_objs_in_sync   if TRUE, makes sure we ALSO update model objects
189
+	 *                                         in this model's entity map according to $fields_n_values that match
190
+	 *                                         $query_params. This obviously has some overhead, so you can disable it
191
+	 *                                         by setting this to FALSE, but be aware that model objects being used
192
+	 *                                         could get out-of-sync with the database
193
+	 * @return int
194
+	 * @throws EE_Error
195
+	 */
196
+	public function update($fields_n_values, $query_params, $keep_model_objs_in_sync = true)
197
+	{
198
+		$count = parent::update($fields_n_values, $query_params, $keep_model_objs_in_sync);
199
+		if ($count) {
200
+			$this->update_term_taxonomy_counts();
201
+		}
202
+		return $count;
203
+	}
204 204
 
205 205
 
206
-    /**
207
-     * Overrides parent so that after running this, we also double-check
208
-     * the term taxonomy counts are up-to-date
209
-     *
210
-     * @param array $query_params
211
-     * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
212
-     * @param bool  $allow_blocking
213
-     * @return int
214
-     * @see EEM_Base::delete
215
-     * @throws EE_Error
216
-     */
217
-    public function delete($query_params, $allow_blocking = true)
218
-    {
219
-        $count = parent::delete($query_params, $allow_blocking);
220
-        if ($count) {
221
-            $this->update_term_taxonomy_counts();
222
-        }
223
-        return $count;
224
-    }
206
+	/**
207
+	 * Overrides parent so that after running this, we also double-check
208
+	 * the term taxonomy counts are up-to-date
209
+	 *
210
+	 * @param array $query_params
211
+	 * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
212
+	 * @param bool  $allow_blocking
213
+	 * @return int
214
+	 * @see EEM_Base::delete
215
+	 * @throws EE_Error
216
+	 */
217
+	public function delete($query_params, $allow_blocking = true)
218
+	{
219
+		$count = parent::delete($query_params, $allow_blocking);
220
+		if ($count) {
221
+			$this->update_term_taxonomy_counts();
222
+		}
223
+		return $count;
224
+	}
225 225
 
226 226
 
227
-    /**
228
-     * @inheritDoc
229
-     */
230
-    public function delete_permanently($query_params, $allow_blocking = true)
231
-    {
232
-        $count = parent::delete_permanently($query_params, $allow_blocking);
233
-        if ($count) {
234
-            $this->update_term_taxonomy_counts();
235
-        }
236
-        return $count;
237
-    }
227
+	/**
228
+	 * @inheritDoc
229
+	 */
230
+	public function delete_permanently($query_params, $allow_blocking = true)
231
+	{
232
+		$count = parent::delete_permanently($query_params, $allow_blocking);
233
+		if ($count) {
234
+			$this->update_term_taxonomy_counts();
235
+		}
236
+		return $count;
237
+	}
238 238
 
239 239
 
240
-    /**
241
-     * Makes sure that during REST API queries, we only return term relationships
242
-     * for term taxonomies which should be shown in the rest api
243
-     *
244
-     * @param array    $model_query_params
245
-     * @param array    $querystring_query_params
246
-     * @param EEM_Base $model
247
-     * @return array
248
-     * @throws EE_Error
249
-     */
250
-    public static function rest_api_query_params($model_query_params, $querystring_query_params, $model)
251
-    {
252
-        if ($model === EEM_Term_Relationship::instance()) {
253
-            $taxonomies = get_taxonomies(['show_in_rest' => true]);
254
-            if (! empty($taxonomies)) {
255
-                $model_query_params[0]['Term_Taxonomy.taxonomy'] = ['IN', $taxonomies];
256
-            }
257
-        }
258
-        return $model_query_params;
259
-    }
240
+	/**
241
+	 * Makes sure that during REST API queries, we only return term relationships
242
+	 * for term taxonomies which should be shown in the rest api
243
+	 *
244
+	 * @param array    $model_query_params
245
+	 * @param array    $querystring_query_params
246
+	 * @param EEM_Base $model
247
+	 * @return array
248
+	 * @throws EE_Error
249
+	 */
250
+	public static function rest_api_query_params($model_query_params, $querystring_query_params, $model)
251
+	{
252
+		if ($model === EEM_Term_Relationship::instance()) {
253
+			$taxonomies = get_taxonomies(['show_in_rest' => true]);
254
+			if (! empty($taxonomies)) {
255
+				$model_query_params[0]['Term_Taxonomy.taxonomy'] = ['IN', $taxonomies];
256
+			}
257
+		}
258
+		return $model_query_params;
259
+	}
260 260
 }
Please login to merge, or discard this patch.
Spacing   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -54,61 +54,61 @@  discard block
 block discarded – undo
54 54
                 ),
55 55
             ],
56 56
         ];
57
-        $this->_model_relations    = [
57
+        $this->_model_relations = [
58 58
             'Term_Taxonomy' => new EE_Belongs_To_Relation(),
59 59
         ];
60 60
         foreach ($models_this_can_attach_to as $model_name) {
61
-            $this->_model_relations[ $model_name ] = new EE_Belongs_To_Relation();
61
+            $this->_model_relations[$model_name] = new EE_Belongs_To_Relation();
62 62
         }
63 63
         $this->_wp_core_model                                           = true;
64 64
         $this->_indexes                                                 = [
65 65
             'PRIMARY' => new EE_Primary_Key_Index(['object_id', 'term_taxonomy_id']),
66 66
         ];
67 67
         $path_to_event_model                                            = 'Event';
68
-        $this->_cap_restriction_generators[ EEM_Base::caps_read ]       =
68
+        $this->_cap_restriction_generators[EEM_Base::caps_read]       =
69 69
             new EE_Restriction_Generator_Event_Related_Public(
70 70
                 $path_to_event_model
71 71
             );
72
-        $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] =
72
+        $this->_cap_restriction_generators[EEM_Base::caps_read_admin] =
73 73
             new EE_Restriction_Generator_Event_Related_Protected(
74 74
                 $path_to_event_model
75 75
             );
76
-        $this->_cap_restriction_generators[ EEM_Base::caps_edit ]       =
76
+        $this->_cap_restriction_generators[EEM_Base::caps_edit] =
77 77
             new EE_Restriction_Generator_Event_Related_Protected(
78 78
                 $path_to_event_model
79 79
             );
80
-        $this->_cap_restriction_generators[ EEM_Base::caps_delete ]     =
80
+        $this->_cap_restriction_generators[EEM_Base::caps_delete] =
81 81
             new EE_Restriction_Generator_Event_Related_Protected(
82 82
                 $path_to_event_model,
83 83
                 EEM_Base::caps_edit
84 84
             );
85
-        $path_to_tax_model                                              = 'Term_Taxonomy.';
85
+        $path_to_tax_model = 'Term_Taxonomy.';
86 86
         // add cap restrictions for editing term relations to the "ee_assign_*"
87 87
         // and for deleting term relations too
88 88
         $cap_contexts_affected = [EEM_Base::caps_edit, EEM_Base::caps_delete];
89 89
         foreach ($cap_contexts_affected as $cap_context_affected) {
90
-            $this->_cap_restrictions[ $cap_context_affected ]['ee_assign_event_category'] =
90
+            $this->_cap_restrictions[$cap_context_affected]['ee_assign_event_category'] =
91 91
                 new EE_Default_Where_Conditions(
92 92
                     [
93
-                        $path_to_tax_model . 'taxonomy*ee_assign_event_category' => [
93
+                        $path_to_tax_model.'taxonomy*ee_assign_event_category' => [
94 94
                             '!=',
95 95
                             'espresso_event_categories',
96 96
                         ],
97 97
                     ]
98 98
                 );
99
-            $this->_cap_restrictions[ $cap_context_affected ]['ee_assign_venue_category'] =
99
+            $this->_cap_restrictions[$cap_context_affected]['ee_assign_venue_category'] =
100 100
                 new EE_Default_Where_Conditions(
101 101
                     [
102
-                        $path_to_tax_model . 'taxonomy*ee_assign_venue_category' => [
102
+                        $path_to_tax_model.'taxonomy*ee_assign_venue_category' => [
103 103
                             '!=',
104 104
                             'espresso_venue_categories',
105 105
                         ],
106 106
                     ]
107 107
                 );
108
-            $this->_cap_restrictions[ $cap_context_affected ]['ee_assign_event_type']     =
108
+            $this->_cap_restrictions[$cap_context_affected]['ee_assign_event_type'] =
109 109
                 new EE_Default_Where_Conditions(
110 110
                     [
111
-                        $path_to_tax_model . 'taxonomy*ee_assign_event_type' => ['!=', 'espresso_event_type'],
111
+                        $path_to_tax_model.'taxonomy*ee_assign_event_type' => ['!=', 'espresso_event_type'],
112 112
                     ]
113 113
                 );
114 114
         }
@@ -251,7 +251,7 @@  discard block
 block discarded – undo
251 251
     {
252 252
         if ($model === EEM_Term_Relationship::instance()) {
253 253
             $taxonomies = get_taxonomies(['show_in_rest' => true]);
254
-            if (! empty($taxonomies)) {
254
+            if ( ! empty($taxonomies)) {
255 255
                 $model_query_params[0]['Term_Taxonomy.taxonomy'] = ['IN', $taxonomies];
256 256
             }
257 257
         }
Please login to merge, or discard this patch.