| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace WPDFI; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * This class handle all ajax actions of this plugin | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @author Duc Bui Quang <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @since 1.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use WPDFI\Traits\Singleton; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  | final class Ajax | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  | 	use Singleton; | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  | 	 * @traitDoc | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	public function initializes() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 		$this->hooks(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 24 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  | 	 * All ajax action of this plugin come here | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  | 	 * | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  | 	 * @since 1.0.0 | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  | 	 * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	public function hooks() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 		$actions = ['get_post_types', 'get_terms', 'get_image_size_names_and_dimensions',  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 					'get_default_layout', 'get_related_layout', 'generate_feature_image']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 		foreach($actions as $action) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 			\add_action('wp_ajax_wpdfi_'. $action, [$this, $action]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 			\add_action('wp_ajax_nopriv_wpdfi'. $action, [$this, $action]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 		} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 40 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  | 	 * Get related layout with post type value, related layout include taxonomies, image upload and image size | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  | 	 * | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  | 	 * @since 1.0.0 | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  | 	 * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	public function get_related_layout() { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 		echo json_encode(\wpdfi()->layout->get_related_layout($_POST['section_index'], $_POST['post_type'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		exit; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 51 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  | 	 * Get default layout of single section in admin | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  | 	 * | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  | 	 * @since 1.0.0 | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  | 	 * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	public function get_default_layout() { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 		echo json_encode(\wpdfi()->layout->get_default_layout($_POST['index'], $_POST['include_delete'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		exit; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 62 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  | 	 * Ajax action to get all post types which support thumbnail feature | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  | 	 * | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  | 	 * @since 1.0.0 | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  | 	 * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	public function get_post_types() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		echo json_encode(\wpdfi()->post_type->get_id_and_text()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 		exit; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 73 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  | 	 * Ajax action to get all terms by given taxonomy | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  | 	 *  | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  | 	 * @since 1.0.0 | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  | 	 * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	public function get_terms() { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 		echo json_encode(\wpdfi()->term->get($_POST['taxonomy'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 		exit; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 84 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	public function generate_feature_image() { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 		/* security check. */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		\check_ajax_referer( 'wpdfi-ajax-nonce', 'security' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 		$update_fimage = \wpdfi()->post_type->update_fimage($_POST['post_id']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		$post_type = \get_post_type($_POST['post_id']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 		$post_type_name = \wpdfi()->post_type->get_singular_name($post_type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		echo json_encode(['status' => $update_fimage, 'namePT' => $post_type_name, 'postId' => $_POST['post_id']]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 		exit; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 95 |  |  | 	} | 
            
                                                        
            
                                    
            
            
                | 96 |  |  | } | 
            
                        
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: