class-base.php
1 year ago
class-dashboard-widget.php
1 year ago
class-rest-request.php
1 year ago
index.php
3 years ago
class-rest-request.php
222 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WBCR\Factory_Adverts_159; |
| 4 | |
| 5 | // Exit if accessed directly |
| 6 | if( !defined('ABSPATH') ) { |
| 7 | exit; |
| 8 | } |
| 9 | |
| 10 | /** |
| 11 | * Factory request class. |
| 12 | * |
| 13 | * Performs a server request, retrieves banner data and stores it in the cache. |
| 14 | * |
| 15 | * @author Alexander Kovalev <alex.kovalevv@gmail.com>, Github: https://github.com/alexkovalevv |
| 16 | * @author Alexander Vitkalov <nechin.va@gmail.com> |
| 17 | * |
| 18 | * @since 1.0.1 Изменил имя класса и доработал его. |
| 19 | * @since 1.0.0 Added |
| 20 | * |
| 21 | * @package factory-adverts |
| 22 | * @copyright (c) 2019 Webcraftic Ltd |
| 23 | */ |
| 24 | class Creative_Motion_API { |
| 25 | |
| 26 | /** |
| 27 | * Rest request url. |
| 28 | * |
| 29 | * Define rest request url for rest request to remote server. |
| 30 | * |
| 31 | * @since 1.2.1 |
| 32 | */ |
| 33 | const SERVER_URL = 'https://api.cm-wp.com'; |
| 34 | |
| 35 | /** |
| 36 | * Rest route path. |
| 37 | * |
| 38 | * Define rest route path for rest request. |
| 39 | * |
| 40 | * @since 1.0.0 |
| 41 | */ |
| 42 | const REST_ROUTE = '/adverds/v1/advt'; |
| 43 | |
| 44 | /** |
| 45 | * Интервал между запросами по умолчанию |
| 46 | * |
| 47 | * Значение в часа� |
| 48 | . |
| 49 | * |
| 50 | * @since 1.0.1 |
| 51 | */ |
| 52 | const DEFAULT_REQUESTS_INTERVAL = 24; |
| 53 | |
| 54 | /** |
| 55 | * Интервал между запросами, если сервер недоступен |
| 56 | * |
| 57 | * Значение в часа� |
| 58 | . |
| 59 | * |
| 60 | * @since 1.0.1 |
| 61 | */ |
| 62 | const SERVER_UNAVAILABLE_INTERVAL = 4; |
| 63 | |
| 64 | |
| 65 | /** |
| 66 | * Экзепляр плагина с которым взаимодействует этот модуль |
| 67 | * |
| 68 | * @author Alexander Kovalev <alex.kovalevv@gmail.com> |
| 69 | * @since 1.0.1 |
| 70 | * @var \Wbcr_Factory480_Plugin |
| 71 | */ |
| 72 | private $plugin; |
| 73 | |
| 74 | |
| 75 | /** |
| 76 | * Request constructor. |
| 77 | * |
| 78 | * Variable initialization. |
| 79 | * |
| 80 | * @param \Wbcr_Factory480_Plugin $plugin_name |
| 81 | * @since 1.0.0 Added |
| 82 | * |
| 83 | */ |
| 84 | public function __construct(\Wbcr_Factory480_Plugin $plugin) |
| 85 | { |
| 86 | $this->plugin = $plugin; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Get adverts content. |
| 91 | * |
| 92 | * @param $position |
| 93 | * |
| 94 | * @return string|\WP_Error |
| 95 | * @author Alexander Kovalev <alex.kovalevv@gmail.com> |
| 96 | * @since 1.0.1 |
| 97 | * |
| 98 | */ |
| 99 | public function get_content($position) |
| 100 | { |
| 101 | $data = $this->get_cache($position); |
| 102 | |
| 103 | if( is_wp_error($data) ) { |
| 104 | return $data; |
| 105 | } |
| 106 | |
| 107 | return strip_tags($data['content'], '<b>,<a>,<i>,<strong>,<img>,<ul>,<ol>,<li>'); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Get data from cache. |
| 112 | * |
| 113 | * If data in the cache, not empty and not expired, then get data from cache. Or get data from server. |
| 114 | * |
| 115 | * @return mixed array( |
| 116 | * 'plugin' => 'wbcr_insert_php', |
| 117 | * 'content' => '<p></p>', |
| 118 | * 'expires' => 1563542199, |
| 119 | * ); |
| 120 | * @since 1.0.1 Полностью переписан, с пере� |
| 121 | ватом api ошибок |
| 122 | * @since 1.0.0 Added |
| 123 | * |
| 124 | * @author Alexander Kovalev <alex.kovalevv@gmail.com> |
| 125 | * |
| 126 | */ |
| 127 | private function get_cache($position) |
| 128 | { |
| 129 | |
| 130 | if( defined('FACTORY_ADVERTS_DEBUG') && FACTORY_ADVERTS_DEBUG ) { |
| 131 | return $this->do_api_request($position); |
| 132 | } |
| 133 | |
| 134 | $key = $this->plugin->getPrefix() . md5($position . 'adverts_transient_'); |
| 135 | |
| 136 | if( 'ru_RU' === get_locale() ) { |
| 137 | $key .= 'ru_'; |
| 138 | } |
| 139 | |
| 140 | $cached = get_transient($key); |
| 141 | |
| 142 | if( $cached !== false ) { |
| 143 | if( isset($cached['error_code']) && isset($cached['error']) ) { |
| 144 | return new \WP_Error($cached['error_code'], $cached['error']); |
| 145 | } |
| 146 | |
| 147 | return $cached; |
| 148 | } |
| 149 | |
| 150 | $data = $this->do_api_request($position); |
| 151 | |
| 152 | if( is_wp_error($data) ) { |
| 153 | set_transient($key, [ |
| 154 | 'error' => $data->get_error_message(), |
| 155 | 'error_code' => $data->get_error_code() |
| 156 | ], self::SERVER_UNAVAILABLE_INTERVAL * HOUR_IN_SECONDS); |
| 157 | |
| 158 | return $data; |
| 159 | } |
| 160 | |
| 161 | set_transient($key, $data, self::DEFAULT_REQUESTS_INTERVAL * HOUR_IN_SECONDS); |
| 162 | |
| 163 | return $data; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Performs rest api request. |
| 168 | * |
| 169 | * In some case on the server (Apache) in the .htaccess must be set |
| 170 | * RewriteRule ^wp-json/(.*)[?](.*) /?rest_route=/$1&$2 [L] |
| 171 | * |
| 172 | * @return mixed array( |
| 173 | * 'plugin' => 'wbcr_insert_php', |
| 174 | * 'content' => '<p></p>', |
| 175 | * 'expires' => 1563542199, |
| 176 | * ); |
| 177 | * @since 1.0.0 Added |
| 178 | * |
| 179 | * @since 1.0.1 Добавлен пере� |
| 180 | ват ошибок, рефакторинг кода. |
| 181 | */ |
| 182 | private function do_api_request($position) |
| 183 | { |
| 184 | $default_result = [ |
| 185 | 'content' => '', |
| 186 | 'expires' => self::DEFAULT_REQUESTS_INTERVAL * HOUR_IN_SECONDS, |
| 187 | ]; |
| 188 | |
| 189 | $url = untrailingslashit(self::SERVER_URL) . '/wp-json' . self::REST_ROUTE; |
| 190 | |
| 191 | $ads_ID = $this->plugin->getPluginName(); |
| 192 | |
| 193 | if( 'ru_RU' === get_locale() ) { |
| 194 | $ads_ID .= '-ru'; |
| 195 | } |
| 196 | |
| 197 | $url = add_query_arg([ |
| 198 | 'plugin' => $ads_ID, |
| 199 | 'position' => $position, |
| 200 | 'plugin_title' => $this->plugin->getPluginTitle(), |
| 201 | 'lang' => get_locale() |
| 202 | ], $url); |
| 203 | |
| 204 | $response = wp_remote_get($url); |
| 205 | |
| 206 | $code = wp_remote_retrieve_response_code($response); |
| 207 | $body = wp_remote_retrieve_body($response); |
| 208 | |
| 209 | $data = @json_decode($body, true); |
| 210 | |
| 211 | if( is_wp_error($response) ) { |
| 212 | return $response; |
| 213 | } |
| 214 | |
| 215 | if( 200 !== $code ) { |
| 216 | return new \WP_Error('http_request_error', 'Failed request to the remote server. Code: ' . $code); |
| 217 | } |
| 218 | |
| 219 | return wp_parse_args($data, $default_result); |
| 220 | } |
| 221 | } |
| 222 |