Skip to content

Commit

Permalink
Add static html REST controller
Browse files Browse the repository at this point in the history
  • Loading branch information
jtsternberg committed Apr 18, 2016
1 parent 6199ce0 commit d08e618
Show file tree
Hide file tree
Showing 3 changed files with 382 additions and 7 deletions.
35 changes: 28 additions & 7 deletions apppresser-offline-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,10 @@ public function plugin_classes() {
* @return void
*/
public function hooks() {
add_action( 'init', array( $this, 'init' ) );
$this->controllers->hooks();
if ( $this->check_requirements() ) {
add_action( 'init', array( $this, 'init' ) );
$this->controllers->hooks();
}
}

/**
Expand Down Expand Up @@ -192,9 +194,7 @@ public function _deactivate() {}
* @return void
*/
public function init() {
if ( $this->check_requirements() ) {
load_plugin_textdomain( 'apppresser-offline-cache', false, dirname( $this->basename ) . '/languages/' );
}
load_plugin_textdomain( 'apppresser-offline-cache', false, dirname( $this->basename ) . '/languages/' );
}

/**
Expand Down Expand Up @@ -239,7 +239,7 @@ public static function meets_requirements() {
// Do checks for required classes / functions
// function_exists('') & class_exists('').
// We have met all requirements.
return true;
return function_exists( 'rest_api_init' ) && class_exists( 'WP_REST_Posts_Controller' );
}

/**
Expand All @@ -251,10 +251,31 @@ public static function meets_requirements() {
public function requirements_not_met_notice() {
// Output our error.
echo '<div id="message" class="error">';
echo '<p>' . sprintf( __( 'AppPresser Offline Cache is missing requirements and has been <a href="%s">deactivated</a>. Please make sure all requirements are available.', 'apppresser-offline-cache' ), admin_url( 'plugins.php' ) ) . '</p>';
echo '<p>' . sprintf( __( 'AppPresser Offline Cache is missing requirements and has been <a href="%s">deactivated</a>. Please make sure all requirements are available. <em>Requirements include: At least WordPress 4.4, and the WordPress REST API plugin.</em>', 'apppresser-offline-cache' ), admin_url( 'plugins.php' ) ) . '</p>';
echo '</div>';
}

public function get_option( $key = false, $fallback = false ) {
if ( function_exists( 'appp_get_setting' ) ) {
return appp_get_setting( $key, $fallback );
}

// Otherwise, duplicate appp_get_setting functionality.
$settings = get_option( 'appp_settings' );
$value = $settings;

if ( $key ) {
$setting = isset( $settings[ $key ] ) ? $settings[ $key ] : false;
// Override value or supply fallback
$value = apply_filters( 'apppresser_setting_default', $setting, $key, $settings, $fallback );
if ( ! $value ) {
$value = $fallback;
}
}

return $value;
}

/**
* Magic getter for our object.
*
Expand Down
66 changes: 66 additions & 0 deletions includes/class-rest-controllers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* AppPresser Offline Cache Endpoints
* @version 0.0.0
* @package AppPresser Offline Cache
*/

class APOC_REST_Controllers {

/**
* Parent plugin class
*
* @var class
* @since NEXT
*/
protected $plugin = null;

/**
* Instance of APOC_Static_HTML_Controller
*
* @since NEXT
* @var APOC_Static_HTML_Controller
*/
protected $static_html;

/**
* Constructor
*
* @since NEXT
* @param object $plugin Main plugin object.
* @return void
*/
public function __construct( $plugin ) {
$this->plugin = $plugin;
}

/**
* Initiate our hooks
*
* @since NEXT
* @return void
*/
public function hooks() {
// Waited to initiate until WP_REST_Posts_Controller is ready.
$this->static_html = new APOC_Static_HTML_Controller( $this->plugin );

add_action( 'rest_api_init', array( $this->static_html, 'register_routes' ) );
}

/**
* Magic getter for our object.
*
* @since NEXT
* @param string $field Field to get.
* @throws Exception Throws an exception if the field is invalid.
* @return mixed
*/
public function __get( $field ) {
switch ( $field ) {
case 'static_html':
return $this->$field;
default:
throw new Exception( 'Invalid '. __CLASS__ .' property: ' . $field );
}
}
}
Loading

0 comments on commit d08e618

Please sign in to comment.