Plugin Directory

Changeset 1703948


Ignore:
Timestamp:
07/27/2017 05:59:23 PM (9 years ago)
Author:
richartkeil
Message:

switch to new API

Location:
exposify/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • exposify/trunk/README.txt

    r1572466 r1703948  
    44Requires at least: 4.5.0
    55Tested up to: 4.6.1
    6 Stable tag: 1.2.2
     6Stable tag: 1.3.0
    77License: GPLv2 or later
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    4848== Changelog ==
    4949
     50= 1.3 =
     51* Switch to new API
     52* Style fixes
    5053= 1.2 =
    5154* Code refactor
  • exposify/trunk/handler.php

    r1572347 r1703948  
    11<?php
     2
    23/**
    34 * Blueprint class to bundle API functionality.
    45 */
    5 abstract class ApiBlueprint {
     6abstract class ApiBlueprint
     7{
    68    /**
    79     * API JSON result converted to an array.
    8      * @var Array
     10     *
     11     * @var array
    912     */
    1013    protected $result = [];
     14
    1115    /**
    1216     * API JSON error converted to an array.
    13      * @var Array
     17     *
     18     * @var array
    1419     */
    1520    protected $error = [];
     21
    1622    /**
    1723     * The URL to connect with Exposify API.
     24     *
    1825     * @var string
    1926     */
    2027    protected $apiUrl = '';
     28
    2129    /**
    2230     * The secret key to connect with Exposify API.
     31     *
    2332     * @var string
    2433     */
    2534    protected $apiKey = '';
     35
    2636    /**
    2737     * Request and store data from a specific URL.
    28      * @param  String $url
    29      * @return Void
     38     *
     39     * @param  string  $url
     40     * @return void
    3041     */
    3142    protected function requestData($url)
     
    3849        ]);
    3950        $json = json_decode(curl_exec($curl), true);
     51
    4052        if (isset($json['error'])) {
    4153            $this->error = $json['error'];
     54        } else if (isset($json['data'])) {
     55            $this->result = $json['data'];
    4256        } 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
    4563        curl_close($curl);
    4664    }
     65
    4766    /**
    4867     * Request all properties.
    49      * @param  String $searchQuery
    50      * @return Void
     68     *
     69     * @param  string  $searchQuery
     70     * @return void
    5171     */
    5272    public function requestAllProperties($searchQuery)
    5373    {
    54         $url = $this->apiUrl . '?api_token=' . $this->apiKey . '&query=' . $searchQuery;
     74        $url = $this->apiUrl . '?api_token=' . $this->apiKey . '&search=' . $searchQuery;
    5575        $this->requestData($url);
    5676    }
     77
    5778    /**
    5879     * Request a single property.
    59      * @param  String $slug
    60      * @return Void
     80     *
     81     * @param  string  $slug
     82     * @return void
    6183     */
    6284    public function requestSingleProperty($slug)
     
    6587        $this->requestData($url);
    6688    }
     89
    6790    /**
    6891     * Return the result of the finished request.
    69      * @return Array
     92     *
     93     * @return array
    7094     */
    7195    public function getResult()
     
    7397        return $this->result;
    7498    }
     99
    75100    /**
    76101     * Return the error of the finished request.
    77      * @return Array
     102     *
     103     * @return array
    78104     */
    79105    public function getError()
     
    82108    }
    83109}
     110
    84111/**
    85112 * Class to allow access to the HTML API.
    86113 */
    87 class HtmlHandler extends ApiBlueprint {
     114class HtmlHandler extends ApiBlueprint
     115{
    88116    /**
    89117     * Construct the class.
    90      * @param String $apiUrl
    91      * @param String $apiKey
     118     *
     119     * @param  string  $apiUrl
     120     * @param  string  $apiKey
     121     * @return void
    92122     */
    93123    public function __construct($apiUrl, $apiKey)
     
    96126        $this->apiKey = $apiKey;
    97127    }
     128
    98129    /**
    99130     * Output the result of the HTML API request.
     131     *
    100132     * @return void
    101133     */
     
    103135    {
    104136        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']);
    107139        } else {
    108             echo htmlspecialchars_decode($this->result['html']);
    109         }
    110     }
     140            echo htmlspecialchars_decode($this->result['attributes']['html']);
     141        }
     142    }
     143
    111144    /**
    112145     * Output the title of the requested property.
     146     *
    113147     * @return void
    114148     */
    115149    public function getTitle()
    116150    {
    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
    121156    /**
    122157     * Output the description of the requested property.
     158     *
    123159     * @return void
    124160     */
    125161    public function getDescription()
    126162    {
    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
    131168    /**
    132169     * Output all head tags needed for the requested ressources.
     170     *
    133171     * @return void
    134172     */
     
    137175        if (isset($this->error['css']))  { $css = $this->error['css']; }
    138176        if (isset($this->result['css'])) { $css = $this->result['css']; }
     177
    139178        if (isset($css) && is_array($css)) {
    140179            foreach ($css as $css_src) {
     
    143182        }
    144183    }
     184
    145185    /**
    146186     * Output all footer tags needed for the requested ressources.
     187     *
    147188     * @return void
    148189     */
     
    151192        if (isset($this->error['js']))  { $js = $this->error['js']; }
    152193        if (isset($this->result['js'])) { $js = $this->result['js']; }
     194
    153195        if (isset($js) && is_array($js)) {
    154196            foreach ($js as $js_src) {
     
    158200    }
    159201}
     202
    160203/**
    161204 * Class to handle the JSON API and allow access to the HTML API.
    162205 */
    163 class Exposify extends ApiBlueprint {
    164     /**
    165      * The HtmlHandler Instance
     206class Exposify extends ApiBlueprint
     207{
     208    /**
     209     * The HtmlHandler Instance.
     210     *
    166211     * @var HtmlHandler
    167212     */
    168213    public $html = null;
     214
    169215    /**
    170216     * Construct the class and instantiate the HtmlHandler.
    171      * @param String $apiKey
     217     *
     218     * @param  string  $apiKey
     219     * @return void
    172220     */
    173221    public function __construct($apiKey, $apiBaseUrl = 'https://app.exposify.de')
    174222    {
    175         $this->apiUrl = $apiBaseUrl . '/api/beta/';
     223        $this->apiUrl = $apiBaseUrl . '/api/v1/json';
    176224        $this->apiKey = $apiKey;
    177         $this->html   = new HtmlHandler($apiBaseUrl . '/html-api', $apiKey);
     225        $this->html   = new HtmlHandler($apiBaseUrl . '/api/v1/html', $apiKey);
    178226    }
    179227}
  • exposify/trunk/public.php

    r1572448 r1703948  
    55  /**
    66  * An instance of the Exposify handler.
     7  *
    78  * @var Exposify
    89  */
     
    1112  /**
    1213  * Construct the class.
    13   * @param String $apiKey
    14   * @param String $baseUrl
     14  *
     15  * @param  string  $apiKey
     16  * @param  string  $baseUrl
     17  * @return void
    1518  */
    1619  public function __construct($apiKey, $baseUrl = 'https://app.exposify.de')
     
    2528  /**
    2629  * Request the property/properties, if there isn't a result yet.
    27   * @return Void
     30  *
     31  * @return void
    2832  */
    2933  public function attemptRequest()
     
    4044  /**
    4145  * Change the page template to the specified one.
    42   * @param  String $oldTemplate
    43   * @return String
     46  *
     47  * @param  string  $oldTemplate
     48  * @return string
    4449  */
    4550  public function changePageTemplate($oldTemplate)
     
    5964  /**
    6065  * Insert the properties into the page.
    61   * @param  String $oldContent
    62   * @return String
     66  *
     67  * @param  string  $oldContent
     68  * @return string
    6369  */
    6470  public function changePageContent($oldContent)
     
    7480  /**
    7581  * Change the page title to the property name.
    76   * @param  String $oldTitle
    77   * @return String
     82  *
     83  * @param  string  $oldTitle
     84  * @return string
    7885  */
    7986  public function changePageTitle($oldTitle)
     
    93100  /**
    94101  * Insert all external CSS and JS files in the page.
    95   * @return Void
     102  *
     103  * @return void
    96104  */
    97105  public function insertLinks()
     
    103111    $this->attemptRequest();
    104112
    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'];
    107115    }
    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'];
    110118    }
    111119    if (isset($css) && is_array($css)) {
     
    117125    }
    118126
    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'];
    121129    }
    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'];
    124132    }
     133
    125134    if (isset($js) && is_array($js)) {
    126135      $i = 1;
    127136      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);
    129138        $i++;
    130139      }
Note: See TracChangeset for help on using the changeset viewer.