| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * JITM's REST API Endpoints | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @package automattic/jetpack-jitm | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | namespace Automattic\Jetpack\JITMS; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Automattic\Jetpack\Connection\REST_Connector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use WP_REST_Server; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * Register the JITM's REST API Endpoints and their callbacks. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class Rest_Api_Endpoints { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	 * Declare the JITM's REST API endpoints. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	public static function register_endpoints() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 		register_rest_route( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 			'jetpack/v4', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 			'/jitm', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 			array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 				'methods'             => WP_REST_Server::READABLE, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 				'callback'            => __CLASS__ . '::get_jitm_message', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 				'permission_callback' => '__return_true', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 			) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 		register_rest_route( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 			'jetpack/v4', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 			'/jitm', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 			array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 				'methods'             => WP_REST_Server::CREATABLE, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 				'callback'            => __CLASS__ . '::delete_jitm_message', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 				'permission_callback' => __CLASS__ . '::delete_jitm_message_permission_callback', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 			) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	 * Asks for a jitm, unless they've been disabled, in which case it returns an empty array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	 * @param WP_REST_Request $request The request object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	 * @return array An array of jitms | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 51 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  | 	public static function get_jitm_message( $request ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  | 		$jitm = JITM::get_instance(); | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  | 		if ( ! $jitm->register() ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  | 			return array(); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  | 		return $jitm->get_messages( $request['message_path'], urldecode_deep( $request['query'] ), 'true' === $request['full_jp_logo_exists'] ? true : false ); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	 * Dismisses a jitm. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	 * @param WP_REST_Request $request The request object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	 * @return bool Always True | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 	public static function delete_jitm_message( $request ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		$jitm = JITM::get_instance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 		if ( ! $jitm->register() ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 			return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 		return $jitm->dismiss( $request['id'], $request['feature_class'] ); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	 * Verify that the user can dismiss JITM messages. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 	 * @return bool|WP_Error True if user is able to dismiss JITM messages. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 84 |  | View Code Duplication | 	public static function delete_jitm_message_permission_callback() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 		if ( current_user_can( 'read' ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 			return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		return new \WP_Error( 'invalid_user_permission_jetpack_delete_jitm_message', REST_Connector::get_user_permissions_error_msg(), array( 'status' => rest_authorization_required_code() ) ); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 92 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |  | 
            
                        
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.