PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.4.1
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.4.1
1.6.7 1.6.8 1.6.9 1.6.12 1.6.13 1.6.14 1.6.15 1.6.16 1.6.17 1.6.18 1.6.19 1.6.2 1.6.20 1.6.21 1.6.22 1.6.23 1.6.24 1.6.25 1.6.26 1.6.27 1.6.28 1.6.3 1.6.4 1.6.5 1.6.6 All 74 releases
weforms / includes / api / class-weforms-log-controller.php

class-weforms-log-controller.php in weForms – Easy Drag & Drop Contact Form Builder For WordPress 1.4.1, at includes/api/class-weforms-log-controller.php

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