| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace BulkWP\BulkDelete\Core\Base\Mixin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 | 1 |  | defined( 'ABSPATH' ) || exit; // Exit if accessed directly. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Container of all Fetch related methods. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Ideally this should be a Trait. Since Bulk Delete still supports PHP 5.3, this is implemented as a class. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * Once the minimum requirement is increased to PHP 5.3, this will be changed into a Trait. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @since 6.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | abstract class Fetcher { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	 * Get the list of public post types registered in WordPress. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	 * @return \WP_Post_Type[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	protected function get_post_types() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 		return bd_get_post_types(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	 * Get the list of post statuses. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	 * This includes all custom post status, but excludes built-in private posts. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	 * @return array List of post status objects. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	protected function get_post_statuses() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 		return bd_get_post_statuses(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	 * Get the list of post types by post status and count. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	 * @return array Post types by post status. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	protected function get_post_types_by_status() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 		$post_types_by_status = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 		$post_types    = $this->get_post_types(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		$post_statuses = $this->get_post_statuses(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 		foreach ( $post_types as $post_type ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 			$post_type_name = $post_type->name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 			$count_posts    = wp_count_posts( $post_type_name ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 			foreach ( $post_statuses as $post_status ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 				$post_status_name = $post_status->name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 				if ( ! property_exists( $count_posts, $post_status_name ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 					continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 				if ( 0 === $count_posts->{$post_status_name} ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 					continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 				$post_types_by_status[ $post_type->labels->singular_name ][ "$post_type_name-$post_status_name" ] = $post_status->label . ' (' . $count_posts->{$post_status_name} . ' ' . __( 'Posts', 'bulk-delete' ) . ')'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		return $post_types_by_status; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	 * Get the list of sticky posts. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	 * @return array List of sticky posts. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	protected function get_sticky_posts() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 		$posts = get_posts( array( 'post__in' => get_option( 'sticky_posts' ) ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 		return $posts; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	 * Get the list of categories. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 	 * @return array List of categories. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	protected function get_categories() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		$enhanced_select_threshold = $this->get_enhanced_select_threshold(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 		$categories = get_categories( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 			array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 				'hide_empty' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 				'number'     => $enhanced_select_threshold, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 			) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		return $categories; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 	 * Are tags present in this WordPress installation? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 	 * Only one tag is retrieved to check if tags are present for performance reasons. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	 * @return bool True if tags are present, False otherwise. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 	protected function are_tags_present() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 		$tags = $this->get_tags( 1 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 		return ( count( $tags ) > 0 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 	 * Get the list of tags. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 	 * @param int $max_count The maximum number of tags to be returned (Optional). Default 0. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 	 *                       If 0 then the maximum number of tags specified in `get_enhanced_select_threshold` will be returned. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 	 * @return array List of tags. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 	protected function get_tags( $max_count = 0 ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 		if ( absint( $max_count ) === 0 ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 			$max_count = $this->get_enhanced_select_threshold(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 		$tags = get_tags( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 			array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 				'hide_empty' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 				'number'     => $max_count, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 			) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 		return $tags; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 	 * Are sticky post present in this WordPress installation? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 	 * @return bool True if sticky posts are present, False otherwise. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 	protected function are_sticky_posts_present() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 		$sticky_post_ids = get_option( 'sticky_posts' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 		if ( ! is_array( $sticky_post_ids ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 			return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 		return ( count( $sticky_post_ids ) > 0 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 	 * Check if private posts are present in a post type. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 	 * @param string $post_type Post type. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 	 * @return bool | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 155 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  | 	protected function are_private_posts_present( $post_type = 'any' ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 157 |  |  | 		$args  = array( | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  | 			'post_status' => array( 'private' ), | 
            
                                                                        
                            
            
                                    
            
            
                | 159 |  |  | 			'post_type'   => $post_type, | 
            
                                                                        
                            
            
                                    
            
            
                | 160 |  |  | 		); | 
            
                                                                        
                            
            
                                    
            
            
                | 161 |  |  | 		$query = new \WP_Query( $args ); | 
            
                                                                        
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 163 |  |  | 		return $query->have_posts(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 	 * Get the number of users present in a role. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | 	 * @param string $role Role slug. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 	 * @return int Number of users in that role. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 	protected function get_user_count_by_role( $role ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  | 		$users_count = count_users(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  | 		$roles = $users_count['avail_roles']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  | 		if ( ! array_key_exists( $role, $roles ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  | 			return 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  | 		return $roles[ $role ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 184 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 185 |  |  |  |