nirjharlo /
wp-plugin-framework
| 1 | <?php |
||||||
| 2 | namespace NirjharLo\WP_Plugin_Framework\Src; |
||||||
| 3 | |||||||
| 4 | use WP_REST_Controller as WP_REST_Controller; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 5 | |||||||
| 6 | if ( ! defined( 'ABSPATH' ) ) exit; |
||||||
| 7 | |||||||
| 8 | /** |
||||||
| 9 | * Extending REST API framework of WordPress |
||||||
| 10 | * |
||||||
| 11 | * @author Nirjhar Lo |
||||||
| 12 | * @package wp-plugin-framework |
||||||
| 13 | */ |
||||||
| 14 | if ( ! class_exists( 'RestApi' ) ) { |
||||||
| 15 | |||||||
| 16 | class RestApi extends WP_REST_Controller { |
||||||
| 17 | |||||||
| 18 | |||||||
| 19 | /** |
||||||
| 20 | * REST API routes |
||||||
| 21 | * |
||||||
| 22 | * @return Object |
||||||
| 23 | */ |
||||||
| 24 | public function register_routes() { |
||||||
| 25 | |||||||
| 26 | $version = '1'; |
||||||
| 27 | $namespace = 'vendor/v' . $version; |
||||||
| 28 | $base = 'route'; |
||||||
| 29 | |||||||
| 30 | //Available options for methods are CREATABLE, READABLE, EDITABLE, DELETABLE |
||||||
| 31 | register_rest_route( $namespace, '/' . $base, array( |
||||||
|
0 ignored issues
–
show
The function
register_rest_route was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 32 | 'methods' => WP_REST_Server::READABLE, |
||||||
|
0 ignored issues
–
show
The type
NirjharLo\WP_Plugin_Framework\Src\WP_REST_Server was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||||
| 33 | 'callback' => array( $this, 'callback' ), |
||||||
| 34 | 'permission_callback' => array( $this, 'permission' ), |
||||||
| 35 | 'args' => array('sample', 'list', 'of', 'args') |
||||||
| 36 | )); |
||||||
| 37 | } |
||||||
| 38 | |||||||
| 39 | |||||||
| 40 | /** |
||||||
| 41 | * The request handler |
||||||
| 42 | * |
||||||
| 43 | * @return Object |
||||||
| 44 | */ |
||||||
| 45 | public function callback() { |
||||||
| 46 | |||||||
| 47 | $params = $request->get_params(); |
||||||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||||
| 48 | $items = array(); |
||||||
| 49 | $data = $this->prepare_item_for_response( $items, $request ); |
||||||
| 50 | |||||||
| 51 | if ( $data ) { |
||||||
|
0 ignored issues
–
show
The expression
$data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using Loading history...
|
|||||||
| 52 | return new WP_REST_Response( $data, 200 ); |
||||||
|
0 ignored issues
–
show
The type
NirjharLo\WP_Plugin_Framework\Src\WP_REST_Response was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||||
| 53 | } else { |
||||||
| 54 | return new WP_Error( 'status_code', __( 'message', 'text-domain' ) ); |
||||||
|
0 ignored issues
–
show
The function
__ was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
The type
NirjharLo\WP_Plugin_Framework\Src\WP_Error was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||||
| 55 | } |
||||||
| 56 | } |
||||||
| 57 | |||||||
| 58 | |||||||
| 59 | /** |
||||||
| 60 | * Prevent unauthorized access here |
||||||
| 61 | * |
||||||
| 62 | * @return Bool |
||||||
| 63 | */ |
||||||
| 64 | public function permission() { |
||||||
| 65 | |||||||
| 66 | return current_user_can( 'manage_options' ); |
||||||
|
0 ignored issues
–
show
The function
current_user_can was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 67 | } |
||||||
| 68 | |||||||
| 69 | |||||||
| 70 | /** |
||||||
| 71 | * Processing of request takes place here |
||||||
| 72 | * |
||||||
| 73 | * @param Array |
||||||
| 74 | * @param Object |
||||||
| 75 | * |
||||||
| 76 | * @return Array |
||||||
| 77 | */ |
||||||
| 78 | public function prepare_item_for_response($items, $request) { |
||||||
| 79 | |||||||
| 80 | //Process the data in any way you like |
||||||
| 81 | $data = compact($items, $request); |
||||||
| 82 | return $data; |
||||||
| 83 | } |
||||||
| 84 | } |
||||||
| 85 | } |
||||||
| 86 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths