| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPFunnels rest api controllers |
| 4 |
* |
| 5 |
* @package WPFunnels\Rest\Controller |
| 6 |
*/ |
| 7 |
namespace WPFunnels\Rest\Controllers; |
| 8 |
|
| 9 |
use WP_Error; |
| 10 |
use WP_REST_Controller; |
| 11 |
|
| 12 |
abstract class Wpfnl_REST_Controller extends WP_REST_Controller { |
| 13 |
/** |
| 14 |
* Endpoint namespace. |
| 15 |
* |
| 16 |
* @var string |
| 17 |
*/ |
| 18 |
protected $namespace = 'wpfunnels/v1'; |
| 19 |
|
| 20 |
/** |
| 21 |
* Route base. |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
protected $rest_base = ''; |
| 26 |
|
| 27 |
|
| 28 |
/** |
| 29 |
* Prepare links for the request. |
| 30 |
* |
| 31 |
* @param string $setting_id Setting ID. |
| 32 |
* @param string $group_id Group ID. |
| 33 |
* |
| 34 |
* @return array Links for the given setting. |
| 35 |
* @since 3.0.0 |
| 36 |
*/ |
| 37 |
protected function prepare_links( $setting_id ) { |
| 38 |
$base = str_replace( '(?P<settings_id>[\w-]+)', $setting_id, $this->rest_base ); |
| 39 |
$links = array( |
| 40 |
'self' => array( |
| 41 |
'href' => get_rest_url( sprintf( '/%s/%s/%s', $this->namespace, $base, $setting_id ) ), |
| 42 |
), |
| 43 |
'collection' => array( |
| 44 |
'href' => get_rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ), |
| 45 |
), |
| 46 |
); |
| 47 |
return $links; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Create a WP_Error object with the specified parameters. |
| 52 |
* |
| 53 |
* @param string $error_code The error code. |
| 54 |
* @param string $error_message The error message. |
| 55 |
* @param int $status The HTTP status code. |
| 56 |
* |
| 57 |
* @return WP_Error The created WP_Error object. |
| 58 |
* |
| 59 |
* @since 2.7.9 |
| 60 |
*/ |
| 61 |
protected function prepare_wp_error_response( $error_code, $error_message, $data ) { |
| 62 |
return new WP_Error( |
| 63 |
$error_code, |
| 64 |
$error_message, |
| 65 |
$data |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Create a Success response with the specified parameters. |
| 71 |
* |
| 72 |
* @param string $message Success message. |
| 73 |
* @param int $code Success coed. |
| 74 |
* |
| 75 |
* @return WP_Rest_Response The created WP_Rest_response object. |
| 76 |
* |
| 77 |
* @since 2.7.9 |
| 78 |
*/ |
| 79 |
protected function prepare_wp_success_response( $message, $code ) { |
| 80 |
$response = array( |
| 81 |
'success' => true, |
| 82 |
'message' => $message, |
| 83 |
'status' => $code |
| 84 |
); |
| 85 |
return rest_ensure_response( $response ); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
/** |
| 90 |
* Returns default 'before' parameter for the reports. |
| 91 |
* |
| 92 |
* @return \WC_DateTime |
| 93 |
* @throws \Exception |
| 94 |
* @since 3.1.7 |
| 95 |
*/ |
| 96 |
public function default_before() { |
| 97 |
$datetime = new \DateTime(); |
| 98 |
$datetime->setTimezone( new \DateTimeZone( wpf_timezone_string() ) ); |
| 99 |
return $datetime; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
/** |
| 104 |
* Returns default 'after' parameter for the reports. |
| 105 |
* |
| 106 |
* @return \Wpfnl_DateTime |
| 107 |
* @throws \Exception |
| 108 |
* @since 3.1.7 |
| 109 |
*/ |
| 110 |
public function default_after() { |
| 111 |
$now = time(); |
| 112 |
$week_back = $now - YEAR_IN_SECONDS; |
| 113 |
|
| 114 |
$datetime = new \DateTime(); |
| 115 |
$datetime->setTimestamp( $week_back ); |
| 116 |
$datetime->setTimezone( new \DateTimeZone( wpf_timezone_string() ) ); |
| 117 |
return $datetime; |
| 118 |
} |
| 119 |
} |
| 120 |
|