@@ -16,145 +16,145 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class Wordlift_Http_Api { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * A {@link Wordlift_Log_Service} instance. |
|
| 21 | - * |
|
| 22 | - * @since 3.15.3 |
|
| 23 | - * |
|
| 24 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 25 | - */ |
|
| 26 | - private $log; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Create a {@link Wordlift_End_Point} instance. |
|
| 30 | - * |
|
| 31 | - * @since 3.15.3 |
|
| 32 | - */ |
|
| 33 | - public function __construct() { |
|
| 34 | - |
|
| 35 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 36 | - |
|
| 37 | - add_action( 'init', array( $this, 'add_rewrite_endpoint' ) ); |
|
| 38 | - add_action( 'template_redirect', array( $this, 'template_redirect' ) ); |
|
| 39 | - add_action( 'admin_post_wl_hello_world', array( |
|
| 40 | - $this, |
|
| 41 | - 'hello_world', |
|
| 42 | - ) ); |
|
| 43 | - add_action( 'admin_post_nopriv_wl_hello_world', array( |
|
| 44 | - $this, |
|
| 45 | - 'nopriv_hello_world', |
|
| 46 | - ) ); |
|
| 47 | - |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Add the `wl-api` rewrite end-point. |
|
| 52 | - * |
|
| 53 | - * @since 3.15.3 |
|
| 54 | - */ |
|
| 55 | - public function add_rewrite_endpoint() { |
|
| 56 | - |
|
| 57 | - add_rewrite_endpoint( 'wl-api', EP_ROOT ); |
|
| 58 | - $this->ensure_rewrite_rules_are_flushed(); |
|
| 59 | - |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Handle `template_redirect` hooks. |
|
| 64 | - * |
|
| 65 | - * @since 3.15.3 |
|
| 66 | - */ |
|
| 67 | - public function template_redirect() { |
|
| 68 | - |
|
| 69 | - global $wp_query; |
|
| 70 | - |
|
| 71 | - if ( ! isset( $wp_query->query_vars['wl-api'] ) ) { |
|
| 72 | - $this->log->trace( 'Skipping, not a `wl-api` call.' ); |
|
| 73 | - |
|
| 74 | - return; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - $this->do_action( $_REQUEST['action'] ); |
|
| 78 | - |
|
| 79 | - exit; |
|
| 80 | - |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Do the requested action. |
|
| 85 | - * |
|
| 86 | - * @since 3.15.3 |
|
| 87 | - * |
|
| 88 | - * @param string $action The action to execute. |
|
| 89 | - */ |
|
| 90 | - private function do_action( $action ) { |
|
| 91 | - |
|
| 92 | - if ( empty( $action ) ) { |
|
| 93 | - return; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - if ( ! wp_validate_auth_cookie( '', 'logged_in' ) ) { |
|
| 97 | - /** |
|
| 98 | - * Fires on a non-authenticated admin post request for the given action. |
|
| 99 | - * |
|
| 100 | - * The dynamic portion of the hook name, `$action`, refers to the given |
|
| 101 | - * request action. |
|
| 102 | - * |
|
| 103 | - * @since 2.6.0 |
|
| 104 | - */ |
|
| 105 | - do_action( "admin_post_nopriv_{$action}" ); |
|
| 106 | - } else { |
|
| 107 | - /** |
|
| 108 | - * Fires on an authenticated admin post request for the given action. |
|
| 109 | - * |
|
| 110 | - * The dynamic portion of the hook name, `$action`, refers to the given |
|
| 111 | - * request action. |
|
| 112 | - * |
|
| 113 | - * @since 2.6.0 |
|
| 114 | - */ |
|
| 115 | - do_action( "admin_post_{$action}" ); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * Test function, anonymous. |
|
| 122 | - * |
|
| 123 | - * @since 3.15.3 |
|
| 124 | - */ |
|
| 125 | - public function nopriv_hello_world() { |
|
| 126 | - |
|
| 127 | - wp_die( 'Hello World! (from anonymous)' ); |
|
| 128 | - |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Test function, authenticated. |
|
| 133 | - * |
|
| 134 | - * @since 3.15.3 |
|
| 135 | - */ |
|
| 136 | - public function hello_world() { |
|
| 137 | - |
|
| 138 | - wp_die( 'Hello World! (from authenticated)' ); |
|
| 139 | - |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * Ensure that the rewrite rules are flushed the first time. |
|
| 144 | - * |
|
| 145 | - * @since 3.16.0 changed the value from 1 to `yes` to avoid type juggling issues. |
|
| 146 | - * @since 3.15.3 |
|
| 147 | - */ |
|
| 148 | - public static function ensure_rewrite_rules_are_flushed() { |
|
| 149 | - |
|
| 150 | - // See https://github.com/insideout10/wordlift-plugin/issues/698. |
|
| 151 | - if ( 'yes' !== get_option( 'wl_http_api' ) ) { |
|
| 152 | - add_option( 'wl_http_api', 'yes' ); |
|
| 153 | - add_action( 'wp_loaded', function () { |
|
| 154 | - flush_rewrite_rules(); |
|
| 155 | - } ); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - } |
|
| 19 | + /** |
|
| 20 | + * A {@link Wordlift_Log_Service} instance. |
|
| 21 | + * |
|
| 22 | + * @since 3.15.3 |
|
| 23 | + * |
|
| 24 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 25 | + */ |
|
| 26 | + private $log; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Create a {@link Wordlift_End_Point} instance. |
|
| 30 | + * |
|
| 31 | + * @since 3.15.3 |
|
| 32 | + */ |
|
| 33 | + public function __construct() { |
|
| 34 | + |
|
| 35 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 36 | + |
|
| 37 | + add_action( 'init', array( $this, 'add_rewrite_endpoint' ) ); |
|
| 38 | + add_action( 'template_redirect', array( $this, 'template_redirect' ) ); |
|
| 39 | + add_action( 'admin_post_wl_hello_world', array( |
|
| 40 | + $this, |
|
| 41 | + 'hello_world', |
|
| 42 | + ) ); |
|
| 43 | + add_action( 'admin_post_nopriv_wl_hello_world', array( |
|
| 44 | + $this, |
|
| 45 | + 'nopriv_hello_world', |
|
| 46 | + ) ); |
|
| 47 | + |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Add the `wl-api` rewrite end-point. |
|
| 52 | + * |
|
| 53 | + * @since 3.15.3 |
|
| 54 | + */ |
|
| 55 | + public function add_rewrite_endpoint() { |
|
| 56 | + |
|
| 57 | + add_rewrite_endpoint( 'wl-api', EP_ROOT ); |
|
| 58 | + $this->ensure_rewrite_rules_are_flushed(); |
|
| 59 | + |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Handle `template_redirect` hooks. |
|
| 64 | + * |
|
| 65 | + * @since 3.15.3 |
|
| 66 | + */ |
|
| 67 | + public function template_redirect() { |
|
| 68 | + |
|
| 69 | + global $wp_query; |
|
| 70 | + |
|
| 71 | + if ( ! isset( $wp_query->query_vars['wl-api'] ) ) { |
|
| 72 | + $this->log->trace( 'Skipping, not a `wl-api` call.' ); |
|
| 73 | + |
|
| 74 | + return; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + $this->do_action( $_REQUEST['action'] ); |
|
| 78 | + |
|
| 79 | + exit; |
|
| 80 | + |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Do the requested action. |
|
| 85 | + * |
|
| 86 | + * @since 3.15.3 |
|
| 87 | + * |
|
| 88 | + * @param string $action The action to execute. |
|
| 89 | + */ |
|
| 90 | + private function do_action( $action ) { |
|
| 91 | + |
|
| 92 | + if ( empty( $action ) ) { |
|
| 93 | + return; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + if ( ! wp_validate_auth_cookie( '', 'logged_in' ) ) { |
|
| 97 | + /** |
|
| 98 | + * Fires on a non-authenticated admin post request for the given action. |
|
| 99 | + * |
|
| 100 | + * The dynamic portion of the hook name, `$action`, refers to the given |
|
| 101 | + * request action. |
|
| 102 | + * |
|
| 103 | + * @since 2.6.0 |
|
| 104 | + */ |
|
| 105 | + do_action( "admin_post_nopriv_{$action}" ); |
|
| 106 | + } else { |
|
| 107 | + /** |
|
| 108 | + * Fires on an authenticated admin post request for the given action. |
|
| 109 | + * |
|
| 110 | + * The dynamic portion of the hook name, `$action`, refers to the given |
|
| 111 | + * request action. |
|
| 112 | + * |
|
| 113 | + * @since 2.6.0 |
|
| 114 | + */ |
|
| 115 | + do_action( "admin_post_{$action}" ); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * Test function, anonymous. |
|
| 122 | + * |
|
| 123 | + * @since 3.15.3 |
|
| 124 | + */ |
|
| 125 | + public function nopriv_hello_world() { |
|
| 126 | + |
|
| 127 | + wp_die( 'Hello World! (from anonymous)' ); |
|
| 128 | + |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Test function, authenticated. |
|
| 133 | + * |
|
| 134 | + * @since 3.15.3 |
|
| 135 | + */ |
|
| 136 | + public function hello_world() { |
|
| 137 | + |
|
| 138 | + wp_die( 'Hello World! (from authenticated)' ); |
|
| 139 | + |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * Ensure that the rewrite rules are flushed the first time. |
|
| 144 | + * |
|
| 145 | + * @since 3.16.0 changed the value from 1 to `yes` to avoid type juggling issues. |
|
| 146 | + * @since 3.15.3 |
|
| 147 | + */ |
|
| 148 | + public static function ensure_rewrite_rules_are_flushed() { |
|
| 149 | + |
|
| 150 | + // See https://github.com/insideout10/wordlift-plugin/issues/698. |
|
| 151 | + if ( 'yes' !== get_option( 'wl_http_api' ) ) { |
|
| 152 | + add_option( 'wl_http_api', 'yes' ); |
|
| 153 | + add_action( 'wp_loaded', function () { |
|
| 154 | + flush_rewrite_rules(); |
|
| 155 | + } ); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + } |
|
| 159 | 159 | |
| 160 | 160 | } |
@@ -32,18 +32,18 @@ discard block |
||
| 32 | 32 | */ |
| 33 | 33 | public function __construct() { |
| 34 | 34 | |
| 35 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 35 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
| 36 | 36 | |
| 37 | - add_action( 'init', array( $this, 'add_rewrite_endpoint' ) ); |
|
| 38 | - add_action( 'template_redirect', array( $this, 'template_redirect' ) ); |
|
| 39 | - add_action( 'admin_post_wl_hello_world', array( |
|
| 37 | + add_action('init', array($this, 'add_rewrite_endpoint')); |
|
| 38 | + add_action('template_redirect', array($this, 'template_redirect')); |
|
| 39 | + add_action('admin_post_wl_hello_world', array( |
|
| 40 | 40 | $this, |
| 41 | 41 | 'hello_world', |
| 42 | - ) ); |
|
| 43 | - add_action( 'admin_post_nopriv_wl_hello_world', array( |
|
| 42 | + )); |
|
| 43 | + add_action('admin_post_nopriv_wl_hello_world', array( |
|
| 44 | 44 | $this, |
| 45 | 45 | 'nopriv_hello_world', |
| 46 | - ) ); |
|
| 46 | + )); |
|
| 47 | 47 | |
| 48 | 48 | } |
| 49 | 49 | |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | */ |
| 55 | 55 | public function add_rewrite_endpoint() { |
| 56 | 56 | |
| 57 | - add_rewrite_endpoint( 'wl-api', EP_ROOT ); |
|
| 57 | + add_rewrite_endpoint('wl-api', EP_ROOT); |
|
| 58 | 58 | $this->ensure_rewrite_rules_are_flushed(); |
| 59 | 59 | |
| 60 | 60 | } |
@@ -68,13 +68,13 @@ discard block |
||
| 68 | 68 | |
| 69 | 69 | global $wp_query; |
| 70 | 70 | |
| 71 | - if ( ! isset( $wp_query->query_vars['wl-api'] ) ) { |
|
| 72 | - $this->log->trace( 'Skipping, not a `wl-api` call.' ); |
|
| 71 | + if ( ! isset($wp_query->query_vars['wl-api'])) { |
|
| 72 | + $this->log->trace('Skipping, not a `wl-api` call.'); |
|
| 73 | 73 | |
| 74 | 74 | return; |
| 75 | 75 | } |
| 76 | 76 | |
| 77 | - $this->do_action( $_REQUEST['action'] ); |
|
| 77 | + $this->do_action($_REQUEST['action']); |
|
| 78 | 78 | |
| 79 | 79 | exit; |
| 80 | 80 | |
@@ -87,13 +87,13 @@ discard block |
||
| 87 | 87 | * |
| 88 | 88 | * @param string $action The action to execute. |
| 89 | 89 | */ |
| 90 | - private function do_action( $action ) { |
|
| 90 | + private function do_action($action) { |
|
| 91 | 91 | |
| 92 | - if ( empty( $action ) ) { |
|
| 92 | + if (empty($action)) { |
|
| 93 | 93 | return; |
| 94 | 94 | } |
| 95 | 95 | |
| 96 | - if ( ! wp_validate_auth_cookie( '', 'logged_in' ) ) { |
|
| 96 | + if ( ! wp_validate_auth_cookie('', 'logged_in')) { |
|
| 97 | 97 | /** |
| 98 | 98 | * Fires on a non-authenticated admin post request for the given action. |
| 99 | 99 | * |
@@ -102,7 +102,7 @@ discard block |
||
| 102 | 102 | * |
| 103 | 103 | * @since 2.6.0 |
| 104 | 104 | */ |
| 105 | - do_action( "admin_post_nopriv_{$action}" ); |
|
| 105 | + do_action("admin_post_nopriv_{$action}"); |
|
| 106 | 106 | } else { |
| 107 | 107 | /** |
| 108 | 108 | * Fires on an authenticated admin post request for the given action. |
@@ -112,7 +112,7 @@ discard block |
||
| 112 | 112 | * |
| 113 | 113 | * @since 2.6.0 |
| 114 | 114 | */ |
| 115 | - do_action( "admin_post_{$action}" ); |
|
| 115 | + do_action("admin_post_{$action}"); |
|
| 116 | 116 | } |
| 117 | 117 | |
| 118 | 118 | } |
@@ -124,7 +124,7 @@ discard block |
||
| 124 | 124 | */ |
| 125 | 125 | public function nopriv_hello_world() { |
| 126 | 126 | |
| 127 | - wp_die( 'Hello World! (from anonymous)' ); |
|
| 127 | + wp_die('Hello World! (from anonymous)'); |
|
| 128 | 128 | |
| 129 | 129 | } |
| 130 | 130 | |
@@ -135,7 +135,7 @@ discard block |
||
| 135 | 135 | */ |
| 136 | 136 | public function hello_world() { |
| 137 | 137 | |
| 138 | - wp_die( 'Hello World! (from authenticated)' ); |
|
| 138 | + wp_die('Hello World! (from authenticated)'); |
|
| 139 | 139 | |
| 140 | 140 | } |
| 141 | 141 | |
@@ -148,9 +148,9 @@ discard block |
||
| 148 | 148 | public static function ensure_rewrite_rules_are_flushed() { |
| 149 | 149 | |
| 150 | 150 | // See https://github.com/insideout10/wordlift-plugin/issues/698. |
| 151 | - if ( 'yes' !== get_option( 'wl_http_api' ) ) { |
|
| 152 | - add_option( 'wl_http_api', 'yes' ); |
|
| 153 | - add_action( 'wp_loaded', function () { |
|
| 151 | + if ('yes' !== get_option('wl_http_api')) { |
|
| 152 | + add_option('wl_http_api', 'yes'); |
|
| 153 | + add_action('wp_loaded', function() { |
|
| 154 | 154 | flush_rewrite_rules(); |
| 155 | 155 | } ); |
| 156 | 156 | } |