| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace BulkWP\BulkDelete\Core\Terms; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use BulkWP\BulkDelete\Core\Base\BaseModule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | defined( 'ABSPATH' ) || exit; // Exit if accessed directly. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Module for deleting terms. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @since 6.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | abstract class TermsModule extends BaseModule { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	 * Build query params for WP_Query by using delete options. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	 * Return an empty query array to short-circuit deletion. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	 * @param array $options Delete options. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	 * @return array Query. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	abstract protected function build_query( $options ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	protected $item_type = 'terms'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	 * Handle common filters. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	 * @param array $request Request array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	 * @return array User options. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	protected function parse_common_filters( $request ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 		$options = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 		$options['restrict']     = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_restrict', false ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 		$options['limit_to']     = absint( bd_array_get( $request, 'smbd_' . $this->field_slug . '_limit_to', 0 ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 		$options['force_delete'] = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_force_delete', false ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 		$options['date_op'] = bd_array_get( $request, 'smbd_' . $this->field_slug . '_op' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 		$options['days']    = absint( bd_array_get( $request, 'smbd_' . $this->field_slug . '_days' ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 		return $options; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	public function filter_js_array( $js_array ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 		return $js_array; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	protected function do_delete( $options ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		$query = $this->build_query( $options ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		if ( empty( $query ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 			// Short circuit deletion, if nothing needs to be deleted. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 			return 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 		return $this->delete_terms_from_query( $query, $options ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	 * Build the query using query params and then Delete posts. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	 * @param array $query   Params for WP Query. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	 * @param array $options Delete Options. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	 * @return int Number of posts deleted. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 69 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  | 	protected function delete_terms_from_query( $query, $options ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  | 		$term_ids = bd_term_query( $query, $options['taxonomy'] ); | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  | 		return $this->delete_terms_by_id( $term_ids, $options ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	 * Render the "private post" setting fields. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	protected function render_private_post_settings() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		bd_render_private_post_settings( $this->field_slug ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	 * Delete terms by ids. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	 * @param int[] $term_ids List of term ids to delete. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	 * @param mixed $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	 * @return int Number of posts deleted. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	protected function delete_terms_by_id( $term_ids, $options ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 		$count = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		foreach ( $term_ids as $term_id ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 			$term = get_term( $term_id, $options['taxonomy'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 			if( isset( $options['no_posts'] ) ){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 				if( $term->count == 0 ){ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 					wp_delete_term( $term_id, $options['taxonomy'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 					$count++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 			}else{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 				wp_delete_term( $term_id, $options['taxonomy'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 				$count++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 		return $count; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 	protected function bd_starts_with($haystack, $needle){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 	     $length = strlen($needle); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 	     return (substr($haystack, 0, $length) === $needle); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 	protected function bd_ends_with($haystack, $needle){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 	    $length = strlen($needle); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 	    return $length === 0 ||  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	    (substr($haystack, -$length) === $needle); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 	protected function bd_term_starts( $term_text , $options ){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 		$term_ids = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 		$terms = get_terms( $options['taxonomy'], array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 		    'hide_empty' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 		) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 		foreach( $terms as $term ){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 			if( $this->bd_starts_with( $term->name, $term_text ) ){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 				$term_ids[] = $term->term_id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 		return $term_ids; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 	protected function bd_term_ends( $term_text , $options ){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 		$term_ids = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 		$terms = get_terms( $options['taxonomy'], array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 		    'hide_empty' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 		) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 		foreach( $terms as $term ){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 			if( $this->bd_ends_with( $term->name, $term_text ) ){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 				$term_ids[] = $term->term_id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 		return $term_ids; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 	protected function bd_term_contains( $term_text , $options ){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 		$term_ids = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 		$terms = get_terms( $options['taxonomy'], array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 		    'hide_empty' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 		) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 		foreach( $terms as $term ){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 			if ( strpos( $term->name, $term_text ) !== false ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 				$term_ids[] = $term->term_id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 		return $term_ids; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 	protected function bd_term_count_query( $options ){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 		$term_ids = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 		$terms = get_terms( $options['taxonomy'], array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 		    'hide_empty' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 		) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 		foreach( $terms as $term ){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  | 			$args = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 				'post_type' => 'post', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | 				'tax_query' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 					array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  | 						'taxonomy' => $options['taxonomy'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  | 						'field'    => 'slug', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  | 						'terms'    => $term->slug, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  | 					), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  | 				), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  | 			); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  | 			$posts = get_posts($args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  | 			if( count($posts) == $options['term_text'] && $options['term_opt'] == "equal_to" ){ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  | 				$term_ids[] = $term->term_id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  | 			}else if( count($posts) != $options['term_text'] && $options['term_opt'] == "not_equal_to" ){ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  | 				$term_ids[] = $term->term_id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  | 			}else if( count($posts) < $options['term_text'] && $options['term_opt'] == "less_than" ){ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  | 				$term_ids[] = $term->term_id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  | 			}else if( count($posts) > $options['term_text'] && $options['term_opt'] == "greater_than" ){ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  | 				$term_ids[] = $term->term_id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  | 		return $term_ids; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 196 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 197 |  |  |  |