Changeset 1703948
- Timestamp:
- 07/27/2017 05:59:23 PM (9 years ago)
- Location:
- exposify/trunk
- Files:
-
- 3 edited
-
README.txt (modified) (2 diffs)
-
handler.php (modified) (11 diffs)
-
public.php (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
exposify/trunk/README.txt
r1572466 r1703948 4 4 Requires at least: 4.5.0 5 5 Tested up to: 4.6.1 6 Stable tag: 1. 2.26 Stable tag: 1.3.0 7 7 License: GPLv2 or later 8 8 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 48 48 == Changelog == 49 49 50 = 1.3 = 51 * Switch to new API 52 * Style fixes 50 53 = 1.2 = 51 54 * Code refactor -
exposify/trunk/handler.php
r1572347 r1703948 1 1 <?php 2 2 3 /** 3 4 * Blueprint class to bundle API functionality. 4 5 */ 5 abstract class ApiBlueprint { 6 abstract class ApiBlueprint 7 { 6 8 /** 7 9 * API JSON result converted to an array. 8 * @var Array 10 * 11 * @var array 9 12 */ 10 13 protected $result = []; 14 11 15 /** 12 16 * API JSON error converted to an array. 13 * @var Array 17 * 18 * @var array 14 19 */ 15 20 protected $error = []; 21 16 22 /** 17 23 * The URL to connect with Exposify API. 24 * 18 25 * @var string 19 26 */ 20 27 protected $apiUrl = ''; 28 21 29 /** 22 30 * The secret key to connect with Exposify API. 31 * 23 32 * @var string 24 33 */ 25 34 protected $apiKey = ''; 35 26 36 /** 27 37 * Request and store data from a specific URL. 28 * @param String $url 29 * @return Void 38 * 39 * @param string $url 40 * @return void 30 41 */ 31 42 protected function requestData($url) … … 38 49 ]); 39 50 $json = json_decode(curl_exec($curl), true); 51 40 52 if (isset($json['error'])) { 41 53 $this->error = $json['error']; 54 } else if (isset($json['data'])) { 55 $this->result = $json['data']; 42 56 } else { 43 $this->result = $json; 44 } 57 $this->error = [ 58 'title' => 'Server Error', 59 'description' => 'The server did not return valid data.' 60 ]; 61 } 62 45 63 curl_close($curl); 46 64 } 65 47 66 /** 48 67 * Request all properties. 49 * @param String $searchQuery 50 * @return Void 68 * 69 * @param string $searchQuery 70 * @return void 51 71 */ 52 72 public function requestAllProperties($searchQuery) 53 73 { 54 $url = $this->apiUrl . '?api_token=' . $this->apiKey . '& query=' . $searchQuery;74 $url = $this->apiUrl . '?api_token=' . $this->apiKey . '&search=' . $searchQuery; 55 75 $this->requestData($url); 56 76 } 77 57 78 /** 58 79 * Request a single property. 59 * @param String $slug 60 * @return Void 80 * 81 * @param string $slug 82 * @return void 61 83 */ 62 84 public function requestSingleProperty($slug) … … 65 87 $this->requestData($url); 66 88 } 89 67 90 /** 68 91 * Return the result of the finished request. 69 * @return Array 92 * 93 * @return array 70 94 */ 71 95 public function getResult() … … 73 97 return $this->result; 74 98 } 99 75 100 /** 76 101 * Return the error of the finished request. 77 * @return Array 102 * 103 * @return array 78 104 */ 79 105 public function getError() … … 82 108 } 83 109 } 110 84 111 /** 85 112 * Class to allow access to the HTML API. 86 113 */ 87 class HtmlHandler extends ApiBlueprint { 114 class HtmlHandler extends ApiBlueprint 115 { 88 116 /** 89 117 * Construct the class. 90 * @param String $apiUrl 91 * @param String $apiKey 118 * 119 * @param string $apiUrl 120 * @param string $apiKey 121 * @return void 92 122 */ 93 123 public function __construct($apiUrl, $apiKey) … … 96 126 $this->apiKey = $apiKey; 97 127 } 128 98 129 /** 99 130 * Output the result of the HTML API request. 131 * 100 132 * @return void 101 133 */ … … 103 135 { 104 136 if (!empty($this->error)) { 105 http_response_code($this->error[' status'] ?: 404);106 echo htmlspecialchars_decode($this->error[' html']);137 http_response_code($this->error['id'] ?: 404); 138 echo htmlspecialchars_decode($this->error['attributes']['html']); 107 139 } else { 108 echo htmlspecialchars_decode($this->result['html']); 109 } 110 } 140 echo htmlspecialchars_decode($this->result['attributes']['html']); 141 } 142 } 143 111 144 /** 112 145 * Output the title of the requested property. 146 * 113 147 * @return void 114 148 */ 115 149 public function getTitle() 116 150 { 117 if (isset($this->result['title'])) { 118 echo $this->result['title']; 119 } 120 } 151 if (isset($this->result['attributes']['title'])) { 152 return $this->result['attributes']['title']; 153 } 154 } 155 121 156 /** 122 157 * Output the description of the requested property. 158 * 123 159 * @return void 124 160 */ 125 161 public function getDescription() 126 162 { 127 if (isset($this->result['description'])) { 128 echo $this->result['description']; 129 } 130 } 163 if (isset($this->result['attributes']['description'])) { 164 return $this->result['attributes']['description']; 165 } 166 } 167 131 168 /** 132 169 * Output all head tags needed for the requested ressources. 170 * 133 171 * @return void 134 172 */ … … 137 175 if (isset($this->error['css'])) { $css = $this->error['css']; } 138 176 if (isset($this->result['css'])) { $css = $this->result['css']; } 177 139 178 if (isset($css) && is_array($css)) { 140 179 foreach ($css as $css_src) { … … 143 182 } 144 183 } 184 145 185 /** 146 186 * Output all footer tags needed for the requested ressources. 187 * 147 188 * @return void 148 189 */ … … 151 192 if (isset($this->error['js'])) { $js = $this->error['js']; } 152 193 if (isset($this->result['js'])) { $js = $this->result['js']; } 194 153 195 if (isset($js) && is_array($js)) { 154 196 foreach ($js as $js_src) { … … 158 200 } 159 201 } 202 160 203 /** 161 204 * Class to handle the JSON API and allow access to the HTML API. 162 205 */ 163 class Exposify extends ApiBlueprint { 164 /** 165 * The HtmlHandler Instance 206 class Exposify extends ApiBlueprint 207 { 208 /** 209 * The HtmlHandler Instance. 210 * 166 211 * @var HtmlHandler 167 212 */ 168 213 public $html = null; 214 169 215 /** 170 216 * Construct the class and instantiate the HtmlHandler. 171 * @param String $apiKey 217 * 218 * @param string $apiKey 219 * @return void 172 220 */ 173 221 public function __construct($apiKey, $apiBaseUrl = 'https://app.exposify.de') 174 222 { 175 $this->apiUrl = $apiBaseUrl . '/api/ beta/';223 $this->apiUrl = $apiBaseUrl . '/api/v1/json'; 176 224 $this->apiKey = $apiKey; 177 $this->html = new HtmlHandler($apiBaseUrl . '/ html-api', $apiKey);225 $this->html = new HtmlHandler($apiBaseUrl . '/api/v1/html', $apiKey); 178 226 } 179 227 } -
exposify/trunk/public.php
r1572448 r1703948 5 5 /** 6 6 * An instance of the Exposify handler. 7 * 7 8 * @var Exposify 8 9 */ … … 11 12 /** 12 13 * Construct the class. 13 * @param String $apiKey 14 * @param String $baseUrl 14 * 15 * @param string $apiKey 16 * @param string $baseUrl 17 * @return void 15 18 */ 16 19 public function __construct($apiKey, $baseUrl = 'https://app.exposify.de') … … 25 28 /** 26 29 * Request the property/properties, if there isn't a result yet. 27 * @return Void 30 * 31 * @return void 28 32 */ 29 33 public function attemptRequest() … … 40 44 /** 41 45 * Change the page template to the specified one. 42 * @param String $oldTemplate 43 * @return String 46 * 47 * @param string $oldTemplate 48 * @return string 44 49 */ 45 50 public function changePageTemplate($oldTemplate) … … 59 64 /** 60 65 * Insert the properties into the page. 61 * @param String $oldContent 62 * @return String 66 * 67 * @param string $oldContent 68 * @return string 63 69 */ 64 70 public function changePageContent($oldContent) … … 74 80 /** 75 81 * Change the page title to the property name. 76 * @param String $oldTitle 77 * @return String 82 * 83 * @param string $oldTitle 84 * @return string 78 85 */ 79 86 public function changePageTitle($oldTitle) … … 93 100 /** 94 101 * Insert all external CSS and JS files in the page. 95 * @return Void 102 * 103 * @return void 96 104 */ 97 105 public function insertLinks() … … 103 111 $this->attemptRequest(); 104 112 105 if (isset($this->exposify->html->getError()[' css'])) {106 $css = $this->exposify->html->getError()[' css'];113 if (isset($this->exposify->html->getError()['attributes']['css'])) { 114 $css = $this->exposify->html->getError()['attributes']['css']; 107 115 } 108 if (isset($this->exposify->html->getResult()[' css'])) {109 $css = $this->exposify->html->getResult()[' css'];116 if (isset($this->exposify->html->getResult()['attributes']['css'])) { 117 $css = $this->exposify->html->getResult()['attributes']['css']; 110 118 } 111 119 if (isset($css) && is_array($css)) { … … 117 125 } 118 126 119 if (isset($this->exposify->html->getError()[' js'])) {120 $js = $this->exposify->html->getError()[' js'];127 if (isset($this->exposify->html->getError()['attributes']['js'])) { 128 $js = $this->exposify->html->getError()['attributes']['js']; 121 129 } 122 if (isset($this->exposify->html->getResult()[' js'])) {123 $js = $this->exposify->html->getResult()[' js'];130 if (isset($this->exposify->html->getResult()['attributes']['js'])) { 131 $js = $this->exposify->html->getResult()['attributes']['js']; 124 132 } 133 125 134 if (isset($js) && is_array($js)) { 126 135 $i = 1; 127 136 foreach ($js as $js_src) { 128 wp_enqueue_script('exposify-' . $i, $js_src, ['jquery'] );137 wp_enqueue_script('exposify-' . $i, $js_src, ['jquery'], false, true); 129 138 $i++; 130 139 }
Note: See TracChangeset
for help on using the changeset viewer.