@@ -90,16 +90,14 @@ public static function update_meta( $post_id ) {
9090 * 2. Now allows updating during `WP_AJAX`.
9191 * @since 5.0.0 1. Moved from `\The_SEO_Framework\Load`.
9292 * 2. Renamed from `_save_inpost_primary_term`.
93+ * @since 5.1.3 Now supports quick-edit and bulk-edit.
9394 *
9495 * @param int $post_id The post ID.
9596 * @return void
9697 */
9798 public static function update_primary_term ( $ post_id ) {
9899
99- // The 'autodescription' index should only be used when using the editor.
100- // Quick and bulk-edit should be halted here.
101- if ( empty ( $ _POST ['autodescription ' ] ) ) return ;
102-
100+ // This resolves a quirk, since wp_insert_post() has no proper guard.
103101 $ post_id = \get_post ( $ post_id )->ID ?? null ;
104102
105103 if ( empty ( $ post_id ) ) return ;
@@ -113,25 +111,70 @@ public static function update_primary_term( $post_id ) {
113111 */
114112 if ( \wp_is_post_autosave ( $ post_id ) || \wp_is_post_revision ( $ post_id ) ) return ;
115113
116- // Check that the user is allowed to edit the post. Nonce checks are done in bulk later.
117114 if ( ! \current_user_can ( 'edit_post ' , $ post_id ) ) return ;
118115
119- $ post_type = \get_post_type ( $ post_id ) ?: false ;
116+ $ post_type = \get_post_type ( $ post_id );
120117 // Can this even fail?
121118 if ( empty ( $ post_type ) ) return ;
122119
123- foreach ( Taxonomy::get_hierarchical ( 'names ' , $ post_type ) as $ taxonomy ) {
124- // Redundant. Fortified.
125- if ( ! \wp_verify_nonce (
126- $ _POST [ static ::$ nonce_name . "_pt_ {$ taxonomy }" ] ?? '' , // If empty, wp_verify_nonce will return false.
127- static ::$ nonce_action . '_pt ' ,
128- ) ) continue ;
129-
130- Data \Plugin \Post::update_primary_term_id (
131- $ post_id ,
132- $ taxonomy ,
133- \absint ( $ _POST ['autodescription ' ][ "_primary_term_ {$ taxonomy }" ] ?? 0 ),
134- );
120+ // Determine edit type: post-edit, quick-edit, or bulk-edit
121+ if ( ! empty ( $ _POST ['autodescription ' ] ) ) {
122+ // Post-edit
123+ foreach ( Taxonomy::get_hierarchical ( 'names ' , $ post_type ) as $ taxonomy ) {
124+ if ( ! \wp_verify_nonce (
125+ $ _POST [ static ::$ nonce_name . "_pt_ {$ taxonomy }" ] ?? '' ,
126+ static ::$ nonce_action . '_pt ' ,
127+ ) ) continue ;
128+
129+ Data \Plugin \Post::update_primary_term_id (
130+ $ post_id ,
131+ $ taxonomy ,
132+ \absint ( $ _POST ['autodescription ' ][ "_primary_term_ {$ taxonomy }" ] ?? 0 ),
133+ );
134+ }
135+ } elseif ( ! empty ( $ _POST ['autodescription-quick ' ] ) ) {
136+ // Quick-edit
137+ if ( ! \check_ajax_referer ( 'inlineeditnonce ' , '_inline_edit ' , false ) ) return ;
138+
139+ foreach ( Taxonomy::get_hierarchical ( 'names ' , $ post_type ) as $ taxonomy ) {
140+ if ( ! isset ( $ _POST ['autodescription-quick ' ][ "primary_term_ {$ taxonomy }" ] ) ) continue ;
141+
142+ $ term_id = \absint ( \wp_unslash ( $ _POST ['autodescription-quick ' ][ "primary_term_ {$ taxonomy }" ] ) );
143+
144+ if ( $ term_id > 0 )
145+ Data \Plugin \Post::update_primary_term_id ( $ post_id , $ taxonomy , $ term_id );
146+ }
147+ } elseif ( ! empty ( $ _REQUEST ['autodescription-bulk ' ] ) ) {
148+ // Bulk-edit
149+ static $ verified_bulk_referer = false ;
150+
151+ if ( ! $ verified_bulk_referer ) {
152+ \check_admin_referer ( 'bulk-posts ' );
153+ $ verified_bulk_referer = true ;
154+ }
155+
156+ foreach ( Taxonomy::get_hierarchical ( 'names ' , $ post_type ) as $ taxonomy ) {
157+ if ( ! isset ( $ _REQUEST ['autodescription-bulk ' ][ "primary_term_ {$ taxonomy }" ] ) ) continue ;
158+
159+ $ value = $ _REQUEST ['autodescription-bulk ' ][ "primary_term_ {$ taxonomy }" ];
160+
161+ if ( 'nochange ' === $ value ) continue ;
162+
163+ $ term_id = \absint ( $ value );
164+
165+ if ( $ term_id > 0 ) {
166+ $ terms = \get_the_terms ( $ post_id , $ taxonomy );
167+
168+ if ( $ terms && ! \is_wp_error ( $ terms ) ) {
169+ $ valid_term_ids = \array_column ( $ terms , 'term_id ' );
170+
171+ if ( \in_array ( $ term_id , $ valid_term_ids , true ) )
172+ Data \Plugin \Post::update_primary_term_id ( $ post_id , $ taxonomy , $ term_id );
173+ }
174+ } else {
175+ Data \Plugin \Post::update_primary_term_id ( $ post_id , $ taxonomy , 0 );
176+ }
177+ }
135178 }
136179 }
137180
@@ -221,6 +264,7 @@ private static function update_via_quick_edit( $post_id ) {
221264
222265 case 'canonical ' :
223266 $ new_data ['_genesis_canonical_uri ' ] = $ value ;
267+ break ;
224268 }
225269 }
226270
0 commit comments