| @@ -1,122 +1,122 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -/** | |
| 4 | - * Log manager class | |
| 5 | - * | |
| 6 | - * @since 1.4.2 | |
| 7 | - */ | |
| 8 | -class Weforms_Log_Controller extends Weforms_REST_Controller { | |
| 9 | - | |
| 10 | - /** | |
| 11 | - * Endpoint namespace | |
| 12 | - * | |
| 13 | - * @var string | |
| 14 | - */ | |
| 15 | - protected $namespace = 'weforms/v1'; | |
| 16 | - | |
| 17 | - /** | |
| 18 | - * Route name | |
| 19 | - * | |
| 20 | - * @var string | |
| 21 | - */ | |
| 22 | - protected $rest_base = 'logs'; | |
| 23 | - | |
| 24 | - /** | |
| 25 | - * Register all routes releated with forms | |
| 26 | - * | |
| 27 | - * @return void | |
| 28 | - */ | |
| 29 | - public function register_routes() { | |
| 30 | - register_rest_route( $this->namespace, '/' . $this->rest_base, [ | |
| 31 | - [ | |
| 32 | - 'methods' => WP_REST_Server::READABLE, | |
| 33 | - 'callback' => [ $this, 'get_items' ], | |
| 34 | - 'permission_callback' => [ $this, 'get_items_permissions_check' ], | |
| 35 | - 'args' => [ | |
| 36 | - 'context' => $this->get_context_param( [ 'default' => 'view' ] ), | |
| 37 | - ], | |
| 38 | - ], | |
| 39 | - [ | |
| 40 | - 'methods' => WP_REST_Server::DELETABLE, | |
| 41 | - 'callback' => [ $this, 'delete_item' ], | |
| 42 | - 'permission_callback' => [ $this, 'delete_item_permissions_check' ], | |
| 43 | - ], | |
| 44 | - ] | |
| 45 | - ); | |
| 46 | - } | |
| 47 | - | |
| 48 | - /** | |
| 49 | - * Retrieves a collection of log. | |
| 50 | - * | |
| 51 | - * @since 1.3.9 | |
| 52 | - * | |
| 53 | - * @param WP_REST_Request $request full details about the request | |
| 54 | - * | |
| 55 | - * @return WP_REST_Response|WP_Error response object on success, or WP_Error object on failure | |
| 56 | - **/ | |
| 57 | - public function get_items( $request ) { | |
| 58 | - $file = weforms_log_file_path(); | |
| 59 | - | |
| 60 | - if ( !file_exists( $file ) ) { | |
| 61 | - return new WP_Error( 'rest_weforms_invalid_data', __( 'file not exist', 'weforms' ), [ 'status' => 404 ] ); | |
| 62 | - } | |
| 63 | - | |
| 64 | - $data = file_get_contents( $file ); | |
| 65 | - $data = explode( "\n", $data ); | |
| 66 | - $data = array_reverse( $data ); | |
| 67 | - $data = array_filter( $data ); | |
| 68 | - | |
| 69 | - if ( empty( $data ) ) { | |
| 70 | - return new WP_Error( 'rest_weforms_invalid_data', __( 'data not exist', 'weforms' ), [ 'status' => 404 ] ); | |
| 71 | - } | |
| 72 | - | |
| 73 | - $logs = []; | |
| 74 | - | |
| 75 | - foreach ( $data as $key => $row ) { | |
| 76 | - preg_match( '/\[(?<time>.+?)\]\[(?<type>.+?)\](?<message>.+)/im', $row, $log ); | |
| 77 | - | |
| 78 | - if ( empty( $log['message'] ) ) { | |
| 79 | - $log = []; | |
| 80 | - $log['message'] = !empty( $log['message'] ) ? $log['message'] : $row; | |
| 81 | - } | |
| 82 | - | |
| 83 | - if ( !empty( $log['time'] ) ) { | |
| 84 | - $human_time = human_time_diff( strtotime( $log['time'] ), current_time( 'timestamp' ) ); | |
| 85 | - $log['time'] = $human_time ? $human_time . ' ' . __( 'ago', 'weforms' ) : $log['time']; | |
| 86 | - } | |
| 87 | - | |
| 88 | - $logs[] = $log; | |
| 89 | - } | |
| 90 | - | |
| 91 | - $data = []; | |
| 92 | - $response = $this->prepare_response_for_collection( $logs, $request ); | |
| 93 | - $response = rest_ensure_response( $response ); | |
| 94 | - | |
| 95 | - return $response; | |
| 96 | - } | |
| 97 | - | |
| 98 | - /** | |
| 99 | - * Delete Log file | |
| 100 | - * | |
| 101 | - * @since 1.4.2 | |
| 102 | - * | |
| 103 | - * @param WP_REST_Request $request | |
| 104 | - * | |
| 105 | - * @return json | |
| 106 | - **/ | |
| 107 | - public function delete_item( $request ) { | |
| 108 | - $file = weforms_log_file_path(); | |
| 109 | - | |
| 110 | - if ( !file_exists( $file ) ) { | |
| 111 | - return new WP_Error( 'rest_weforms_invalid_data', __( 'file not exist', 'weforms' ), [ 'status' => 404 ] ); | |
| 112 | - } | |
| 113 | - | |
| 114 | - @unlink( $file ); | |
| 115 | - | |
| 116 | - $data = [ 'message' => 'log file deleted' ]; | |
| 117 | - $response = $this->prepare_response_for_collection( $data, $request ); | |
| 118 | - $response = rest_ensure_response( $response ); | |
| 119 | - | |
| 120 | - return $response; | |
| 121 | - } | |
| 122 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * Log manager class | |
| 5 | + * | |
| 6 | + * @since 1.4.2 | |
| 7 | + */ | |
| 8 | +class Weforms_Log_Controller extends Weforms_REST_Controller { | |
| 9 | + | |
| 10 | + /** | |
| 11 | + * Endpoint namespace | |
| 12 | + * | |
| 13 | + * @var string | |
| 14 | + */ | |
| 15 | + protected $namespace = 'weforms/v1'; | |
| 16 | + | |
| 17 | + /** | |
| 18 | + * Route name | |
| 19 | + * | |
| 20 | + * @var string | |
| 21 | + */ | |
| 22 | + protected $rest_base = 'logs'; | |
| 23 | + | |
| 24 | + /** | |
| 25 | + * Register all routes releated with forms | |
| 26 | + * | |
| 27 | + * @return void | |
| 28 | + */ | |
| 29 | + public function register_routes() { | |
| 30 | + register_rest_route( $this->namespace, '/' . $this->rest_base, [ | |
| 31 | + [ | |
| 32 | + 'methods' => WP_REST_Server::READABLE, | |
| 33 | + 'callback' => [ $this, 'get_items' ], | |
| 34 | + 'permission_callback' => [ $this, 'get_items_permissions_check' ], | |
| 35 | + 'args' => [ | |
| 36 | + 'context' => $this->get_context_param( [ 'default' => 'view' ] ), | |
| 37 | + ], | |
| 38 | + ], | |
| 39 | + [ | |
| 40 | + 'methods' => WP_REST_Server::DELETABLE, | |
| 41 | + 'callback' => [ $this, 'delete_item' ], | |
| 42 | + 'permission_callback' => [ $this, 'delete_item_permissions_check' ], | |
| 43 | + ], | |
| 44 | + ] | |
| 45 | + ); | |
| 46 | + } | |
| 47 | + | |
| 48 | + /** | |
| 49 | + * Retrieves a collection of log. | |
| 50 | + * | |
| 51 | + * @since 1.3.9 | |
| 52 | + * | |
| 53 | + * @param WP_REST_Request $request full details about the request | |
| 54 | + * | |
| 55 | + * @return WP_REST_Response|WP_Error response object on success, or WP_Error object on failure | |
| 56 | + **/ | |
| 57 | + public function get_items( $request ) { | |
| 58 | + $file = weforms_log_file_path(); | |
| 59 | + | |
| 60 | + if ( !file_exists( $file ) ) { | |
| 61 | + return new WP_Error( 'rest_weforms_invalid_data', __( 'file not exist', 'weforms' ), [ 'status' => 404 ] ); | |
| 62 | + } | |
| 63 | + | |
| 64 | + $data = file_get_contents( $file ); | |
| 65 | + $data = explode( "\n", $data ); | |
| 66 | + $data = array_reverse( $data ); | |
| 67 | + $data = array_filter( $data ); | |
| 68 | + | |
| 69 | + if ( empty( $data ) ) { | |
| 70 | + return new WP_Error( 'rest_weforms_invalid_data', __( 'data not exist', 'weforms' ), [ 'status' => 404 ] ); | |
| 71 | + } | |
| 72 | + | |
| 73 | + $logs = []; | |
| 74 | + | |
| 75 | + foreach ( $data as $key => $row ) { | |
| 76 | + preg_match( '/\[(?<time>.+?)\]\[(?<type>.+?)\](?<message>.+)/im', $row, $log ); | |
| 77 | + | |
| 78 | + if ( empty( $log['message'] ) ) { | |
| 79 | + $log = []; | |
| 80 | + $log['message'] = !empty( $log['message'] ) ? $log['message'] : $row; | |
| 81 | + } | |
| 82 | + | |
| 83 | + if ( !empty( $log['time'] ) ) { | |
| 84 | + $human_time = human_time_diff( strtotime( $log['time'] ), current_time( 'timestamp' ) ); | |
| 85 | + $log['time'] = $human_time ? $human_time . ' ' . __( 'ago', 'weforms' ) : $log['time']; | |
| 86 | + } | |
| 87 | + | |
| 88 | + $logs[] = $log; | |
| 89 | + } | |
| 90 | + | |
| 91 | + $data = []; | |
| 92 | + $response = $this->prepare_response_for_collection( $logs, $request ); | |
| 93 | + $response = rest_ensure_response( $response ); | |
| 94 | + | |
| 95 | + return $response; | |
| 96 | + } | |
| 97 | + | |
| 98 | + /** | |
| 99 | + * Delete Log file | |
| 100 | + * | |
| 101 | + * @since 1.4.2 | |
| 102 | + * | |
| 103 | + * @param WP_REST_Request $request | |
| 104 | + * | |
| 105 | + * @return json | |
| 106 | + **/ | |
| 107 | + public function delete_item( $request ) { | |
| 108 | + $file = weforms_log_file_path(); | |
| 109 | + | |
| 110 | + if ( !file_exists( $file ) ) { | |
| 111 | + return new WP_Error( 'rest_weforms_invalid_data', __( 'file not exist', 'weforms' ), [ 'status' => 404 ] ); | |
| 112 | + } | |
| 113 | + | |
| 114 | + @unlink( $file ); | |
| 115 | + | |
| 116 | + $data = [ 'message' => 'log file deleted' ]; | |
| 117 | + $response = $this->prepare_response_for_collection( $data, $request ); | |
| 118 | + $response = rest_ensure_response( $response ); | |
| 119 | + | |
| 120 | + return $response; | |
| 121 | + } | |
| 122 | +} | |