Skip to content
Permalink
Browse files

Clean up code for further fixing

  • Loading branch information...
szepeviktor committed Apr 14, 2019
1 parent 1a36bd3 commit 783388fbb29e1c17f2b3d3674b771317735d593a
Showing with 182 additions and 128 deletions.
  1. +64 −58 classes/model.php
  2. +17 −15 classes/query.php
  3. +15 −4 composer.json
  4. +56 −0 composer.lock
  5. +24 −45 dustpress.php
  6. +2 −2 helpers/contains.php
  7. +1 −1 helpers/pagination.php
  8. +3 −3 models/user-activate.php
@@ -23,7 +23,7 @@ class Model {
protected $class_name;
// Arguments of this instance
private $args;
private $args = array();
// Instances of all submodels initiated from this class
private $submodels;
@@ -40,15 +40,21 @@ class Model {
// Is execution terminated
private $terminated;
private $ttl;
private $called_subs;
private $api;
/**
* Constructor for DustPress model class.
*
* @type function
* @date 10/8/2015
* @since 0.2.0
*
* @param load (boolean)
* @return N/A
* @param array $args
* @param mixed $parent
*/
public function __construct( $args = [], $parent = null ) {
$this->fix_deprecated();
@@ -69,7 +75,7 @@ public function __construct( $args = [], $parent = null ) {
* @date 3/6/2016
* @since 0.4.0
*
* @return void
* @return array
*/
public function get_args() {
return $this->args;
@@ -109,7 +115,7 @@ public function get_data() {
* @date 3/6/2016
* @since 0.4.0
*
* @return Dustpress\Model
* @return \Dustpress\Model
*/
public function get_submodel( $name ) {
return $this->submodels->{$name};
@@ -135,7 +141,7 @@ public function get_submodels() {
* @date 3/6/2016
* @since 0.4.0
*
* @return Dustpress\Model
* @return \Dustpress\Model
*/
public function get_ancestor( $model = null ) {
if ( ! isset( $model ) ) {
@@ -157,9 +163,6 @@ public function get_ancestor( $model = null ) {
* @type function
* @date 16/02/2017
* @since 1.5.5
*
* @param N/A
* @return N/A
*/
public function fix_deprecated() {
// Reassign deprecated "allowed_functions" to "api".
@@ -176,14 +179,16 @@ public function fix_deprecated() {
* @date 15/10/2015
* @since 0.2.0
*
* @param N/A
* @return N/A
* @param string $functions
* @param bool $tidy
*
* @return mixed
*/
public function fetch_data( $functions = null, $tidy = false ) {
$this->class_name = get_class( $this );
// Create a place to store the wanted data in the global data structure.
if ( ! isset( $this->data[ $this->class_name ] ) ) $this->data[ $this->class_name ] = new \StdClass();
if ( ! isset( $this->data[ $this->class_name ] ) ) $this->data[ $this->class_name ] = new \stdClass();
// Fetch all methods from given class and in its parents.
$methods = $this->get_class_methods( $this->class_name );
@@ -213,7 +218,7 @@ public function fetch_data( $functions = null, $tidy = false ) {
}
else {
if ( ! $this->is_function_allowed( $method_item ) ) {
die( json_encode( [ "error" => "Method '". $function ."' is not allowed to be run via AJAX or does not exist." ] ) );
die( json_encode( [ "error" => "Method '". $method_item ."' is not allowed to be run via AJAX or does not exist." ] ) );
}
else if ( $reflection->isProtected() || $reflection->isPrivate() ) {
$private_methods[] = $method_item;
@@ -353,8 +358,8 @@ public function fetch_data( $functions = null, $tidy = false ) {
* @date 19/3/2015
* @since 0.0.1
*
* @param $class_name (string)
* @param $methods (array)
* @param string $class_name
* @param array $methods
* @return $methods (array)
*/
private function get_class_methods( $class_name, $methods = array() ) {
@@ -448,7 +453,7 @@ public function bind_sub( $name, $args = null, $cache_sub = true ) {
}
if ( $model->terminated == true ) {
$this-terminate();
$this->terminate();
}
}
@@ -460,9 +465,9 @@ public function bind_sub( $name, $args = null, $cache_sub = true ) {
* @date 17/3/2015
* @since 0.0.1
*
* @param $data (N/A)
* @param $key (string)
* @param $model (string)
* @param N/A $data
* @param string $key
* @param string $model
* @return true/false (boolean)
*/
public function bind( $data, $key = null, $model = null ) {
@@ -476,7 +481,7 @@ public function bind( $data, $key = null, $model = null ) {
if ( $model ) {
// Create a place to store the wanted data in the global data structure.
if ( ! isset( $this->data[ $model ] ) ) $this->data[ $model ] = new \StdClass();
if ( ! isset( $this->data[ $model ] ) ) $this->data[ $model ] = new \stdClass();
if ( ! isset( $this->data[ $model ] ) ) {
$this->data[ $model ] = (object)[];
@@ -485,7 +490,7 @@ public function bind( $data, $key = null, $model = null ) {
}
else {
// Create a place to store the wanted data in the global data structure.
if ( ! isset( $this->data[ $this->class_name ] ) ) $this->data[ $this->class_name ] = new \StdClass();
if ( ! isset( $this->data[ $this->class_name ] ) ) $this->data[ $this->class_name ] = new \stdClass();
if ( ! $this->parent ) {
if ( is_array( $data ) ) {
@@ -534,8 +539,7 @@ public function get_template() {
* @date 15/10/2015
* @since 0.2.0
*
* @param $template (string)
* @return N/A
* @param string $template
*/
public function set_template( $template ) {
$ancestor = $this->get_ancestor();
@@ -561,8 +565,9 @@ public function set_template( $template ) {
* @date 29/01/2016
* @since 0.3.1
*
* @param $m (string)
* @return N/A
* @param string $m
*
* @return mixed
*/
private function run_function( $m, $class = null ) {
$cached = $this->get_cached( $m );
@@ -607,8 +612,9 @@ private function run_function( $m, $class = null ) {
* @date 29/01/2016
* @since 0.3.1
*
* @param $m (string)
* @return N/A
* @param string $m
*
* @return mixed|bool
*/
private function get_cached( $m ) {
@@ -644,42 +650,42 @@ private function get_cached( $m ) {
* @date 29/01/2016
* @since 0.3.1
*
* @param $m (string), $data (any), $subs (array)
* @return N/A
* @param string $m
* @param mixed $data
* @param array $subs
*/
private function maybe_cache( $m, $data, $subs ) {
// Check whether cache is enabled and model has ttl-settings.
if ( dustpress()->get_setting('cache') && $this->is_cacheable_function( $m ) ) {
if ( ! dustpress()->get_setting('cache') || ! $this->is_cacheable_function( $m ) ) {
return;
}
// Extend data with submodels
$to_cache = (object)[ 'data' => $data, 'subs' => $subs ];
// If no hash key exists, bail out
if ( empty( $this->hash ) ) {
return;
}
set_transient( $this->hash, $to_cache, $this->ttl[ $m ] );
// Extend data with submodels
$to_cache = (object)[ 'data' => $data, 'subs' => $subs ];
// If no hash key exists, bail
if ( ! isset( $this->hash ) ) {
return;
}
set_transient( $this->hash, $to_cache, $this->ttl[ $m ] );
// Index key for cache clearing
$index = $this->generate_cache_key( $this->class_name, $m );
$hash_index = get_transient( $index );
// Index key for cache clearing
$index = $this->generate_cache_key( $this->class_name, $m );
$hash_index = get_transient( $index );
if ( ! is_array( $hash_index ) ) {
$hash_index = [];
}
// Set the data hash key to the index array of this model function
if ( ! in_array( $this->hash, $hash_index ) ) {
$hash_index[] = $this->hash;
}
if ( ! is_array( $hash_index ) ) {
$hash_index = [];
}
// Store transient for 30 days
set_transient( $index, $hash_index, 30 * DAY_IN_SECONDS );
// Set the data hash key to the index array of this model function
if ( ! in_array( $this->hash, $hash_index ) ) {
$hash_index[] = $this->hash;
}
return false;
// Store transient for 30 days
set_transient( $index, $hash_index, 30 * DAY_IN_SECONDS );
}
/**
@@ -712,7 +718,7 @@ private function is_cacheable_function( $m ) {
* @date 17/12/2015
* @since 0.3.0
*
* @param $function (string)
* @param string $function
* @return $allowed (boolean)
*/
private function is_function_allowed( $function ) {
@@ -740,7 +746,7 @@ private function is_function_allowed( $function ) {
* @date 17/12/2015
* @since 0.3.0
*
* @param $args (ellipsis)
* @param ellipsis $args
* @return $key (string)
*/
private function generate_cache_key() {
@@ -765,8 +771,8 @@ private function generate_cache_key() {
* @since 0.3.0
*
*
* @param $function (string)
* @param $args (array)
* @param string $function
* @param array $args
* @return mixed
*/
public function run_restricted( $function ) {
@@ -786,8 +792,8 @@ public function run_restricted( $function ) {
* @since 1.2.0
*
*
* @param $function (string)
* @param $args (array)
* @param string $function
* @param array $args
* @return mixed
*/
protected function rename_model( $name ) {
@@ -811,7 +817,7 @@ protected function rename_model( $name ) {
/**
* A recursive array search.
*
* @param any $needle
* @param mixed $needle
* @param array $haystack
* @param boolean $strict
* @return boolean
@@ -29,8 +29,8 @@ class Query {
* @date 20/3/2015
* @since 0.0.1
*
* @param $id (int)
* @param $args (array)
* @param int $id
* @param array $args
*
* @return array|object|null Type corresponding to output type on success or null on failure.
*/
@@ -87,8 +87,8 @@ public static function get_post( $id = null, $args = array() ) {
* @date 20/3/2015
* @since 0.0.1
*
* @param $id (int)
* @param $args (array)
* @param int $id
* @param array $args
*
* @return array|object|null Type corresponding to output type on success or null on failure.
*/
@@ -107,6 +107,7 @@ public static function get_acf_post( $id = null, $args = array() ) {
$options = array_merge( $defaults, $args );
// FIXME -> WP func.
extract( $options );
// Throw error if we have no post to fetch
@@ -175,10 +176,10 @@ public static function get_acf_post( $id = null, $args = array() ) {
* @date 16/8/2016
* @since 1.1.5
*
* @param array|object $field The current ACF field object.
* @param object $options Recurion options.
* @param array|object $field The current ACF field object.
* @param object $options Recurion options.
*
* @return any $field Returns the same type it is given, possibly extended.
* @return mixed Returns the same type it is given, possibly extended.
*/
private static function handle_field( $field, $options ) {
// No recursion for these post types
@@ -236,6 +237,7 @@ public static function get_posts( $args ) {
$options = array_merge( $defaults, $args );
// FIXME -> WP function
extract( $options );
self::$query = new WP_Query( $options );
@@ -262,9 +264,9 @@ public static function get_posts( $args ) {
// Return in the desired format.
return self::query_return_value_format( self::$query , $query_object, $no_found_rows );
} else {
return false;
}
}
return false;
}
/**
@@ -277,7 +279,7 @@ public static function get_posts( $args ) {
*
* @param array $args Arguments to override the defaults defined in get_wp_query_defaults.
*
* @return array/boolean Array of posts as an associative array with acf fields and meta data
* @return array|boolean Array of posts as an associative array with acf fields and meta data
*/
public static function get_acf_posts( $args ) {
@@ -347,11 +349,11 @@ public static function get_acf_posts( $args ) {
/**
* A wrapper for posts query function return value formatting.
*
* @param objecy $query The WP_Query object.
* @param boolean $query_object Do we want the whole query object?
* @param boolean $no_found_rows Was the query paginated?
* @param \WP_Query $query The WP_Query object.
* @param boolean $query_object Do we want the whole query object?
* @param boolean $no_found_rows Was the query paginated?
*
* @return object|array
* @return \WP_Query|array
*/
private static function query_return_value_format( $query, $query_object, $no_found_rows ) {
// Maybe return the whole query object
Oops, something went wrong.

0 comments on commit 783388f

Please sign in to comment.
You can’t perform that action at this time.