Skip to content

Commit

Permalink
Merge pull request #34 from CodeProKid/feature/32
Browse files Browse the repository at this point in the history
phpcs cleanup and some documentation fixes #32
  • Loading branch information
keg authored Jun 8, 2017
2 parents 936cc19 + c42cac8 commit f85b275
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 31 deletions.
4 changes: 2 additions & 2 deletions includes/class-dfm-async-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ class DFM_Async_Handler {
function __construct( $transient, $modifier, $lock_key = '' ) {

$this->transient_name = $transient;
$this->modifier = $modifier;
$this->lock_key = $lock_key;
$this->modifier = $modifier;
$this->lock_key = $lock_key;
// Spawn the event on shutdown so we are less likely to run into timeouts, or block other processes
add_action( 'shutdown', array( $this, 'spawn_event' ) );

Expand Down
2 changes: 1 addition & 1 deletion includes/class-dfm-async-nonce.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class DFM_Async_Nonce {
*/
function __construct( $transient ) {
$this->transient = $transient;
$this->action = 'dfm_' . $this->transient;
$this->action = 'dfm_' . $this->transient;
}

/**
Expand Down
13 changes: 7 additions & 6 deletions includes/class-dfm-transient-scheduler.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

if ( ! class_exists( 'DFM_Transient_Scheduler' ) ) {
if ( ! class_exists( 'DFM_Transient_Scheduler' ) ) :

/**
* Class DFM_Transient_Scheduler
* Attaches hooks to run incoming post requests.
Expand Down Expand Up @@ -87,10 +88,10 @@ private function post_processing_hooks() {
*/
public function run_update() {

$transient_name = empty( $_POST['transient_name'] ) ? false : $_POST['transient_name'];
$modifier = empty( $_POST['modifier'] ) ? '' : $_POST['modifier'];
$nonce = empty( $_POST['_nonce'] ) ? '' : $_POST['_nonce'];
$lock_key = empty( $_POST['lock_key'] ) ? '' : $_POST['lock_key'];
$transient_name = empty( $_POST['transient_name'] ) ? false : sanitize_text_field( $_POST['transient_name'] );
$modifier = empty( $_POST['modifier'] ) ? '' : sanitize_text_field( $_POST['modifier'] );
$nonce = empty( $_POST['_nonce'] ) ? '' : sanitize_text_field( $_POST['_nonce'] );
$lock_key = empty( $_POST['lock_key'] ) ? '' : sanitize_text_field( $_POST['lock_key'] );

// Bail if a transient name wasn't passed for some reason
if ( empty( $transient_name ) ) {
Expand Down Expand Up @@ -126,6 +127,6 @@ public function run_update() {

}

}
endif;

new DFM_Transient_Scheduler();
50 changes: 28 additions & 22 deletions includes/class-dfm-transients.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,18 @@ class DFM_Transients {
/**
* DFM_Transients constructor.
*
* @param string $transient
* @param string|int $modifier
* @param string $transient Name of the transient
* @param string|int $modifier Unique modifier for the transient
*/
function __construct( $transient, $modifier ) {

global $dfm_transients;
$this->transient = $transient;
$this->modifier = $modifier;
$this->transient = $transient;
$this->modifier = $modifier;
$this->transient_object = $dfm_transients[ $this->transient ];
$this->key = $this->cache_key();
$this->lock_key = uniqid( 'dfm_lt_' );
$this->prefix = apply_filters( 'dfm_transient_prefix', 'dfm_transient_' );
$this->key = $this->cache_key();
$this->lock_key = uniqid( 'dfm_lt_' );
$this->prefix = apply_filters( 'dfm_transient_prefix', 'dfm_transient_' );

}

Expand Down Expand Up @@ -122,14 +122,14 @@ public function get() {
/**
* This method handles storing transient data to the database or object cache
*
* @param string|array $data
* @param string|array $data The data to add to the transient
* @access public
* @return WP_Error|void
*/
public function set( $data ) {

if ( ! isset( $this->transient_object ) ) {
new WP_Error( 'invalid-transient', __( 'You are trying to set data to a transient that doesn\'t exist', 'dfm-transients' ) );
return;
return new WP_Error( 'invalid-transient', __( 'You are trying to set data to a transient that doesn\'t exist', 'dfm-transients' ) );
}

if ( false === $data || is_wp_error( $data ) ) {
Expand Down Expand Up @@ -225,14 +225,17 @@ public function is_locked() {
* @access public
*/
public function owns_lock( $lock_key ) {

if ( empty( $this->lock ) ) {
$this->lock = get_transient( 'dfm_lt_' . $this->key );
}

if ( $this->lock === $lock_key ) {
return true;
} else {
return false;
}

}

/**
Expand Down Expand Up @@ -274,16 +277,16 @@ private function get_from_transient() {
/**
* Handles retrieving data from post_meta or term_meta
*
* @param string $type
* @param string $type The object type the meta is attached to
* @return mixed string|array
* @access private
*/
private function get_from_meta( $type ) {

$data = get_metadata( $type, $this->modifier, $this->key, true );

$data_exists = true;

if ( empty( $data ) ) {
$data_exists = metadata_exists( $type, $this->modifier, $this->key );
}
Expand Down Expand Up @@ -317,7 +320,7 @@ private function get_from_meta( $type ) {
/**
* Handles saving of data for a transient storage type
*
* @param string|array $data
* @param string|array $data The data to save to the transient
* @return void
* @access private
*/
Expand All @@ -331,8 +334,8 @@ private function save_to_transient( $data ) {
// Set expiration to a year if we aren't using an object cache, so the transient
// isn't autoloaded from the database
$expiration = ( true === wp_using_ext_object_cache() ) ? 0 : YEAR_IN_SECONDS;
$data = array(
'data' => $data,
$data = array(
'data' => $data,
'expiration' => time() + (int) $this->transient_object->expiration,
);
}
Expand All @@ -345,14 +348,17 @@ private function save_to_transient( $data ) {
/**
* Handles saving data for meta storage engine
*
* @param string|array $data
* @param string $type
* @param string|array $data The data to save
* @param string $type The object type the meta is connected to
*
* @access private
* @return void
*/
private function save_to_metadata( $data, $type ) {

if ( $this->should_expire() ) {
$data = array(
'data' => $data,
'data' => $data,
'expiration' => time() + (int) $this->transient_object->expiration,
);
}
Expand Down Expand Up @@ -440,7 +446,7 @@ private function facilitate_retry() {
/**
* Hashes storage key
*
* @param string $key
* @param string $key Name of the key to hash
* @return string
* @access private
*/
Expand Down Expand Up @@ -470,7 +476,7 @@ private function cache_key() {
$key = $this->hash_key( $key );
}

switch( $this->transient_object->cache_type ) {
switch ( $this->transient_object->cache_type ) {
case 'post_meta':
case 'term_meta':
case 'user_meta':
Expand Down Expand Up @@ -520,7 +526,7 @@ private function should_soft_expire() {
/**
* Whether or not the transient data has expired
*
* @param array|string $data
* @param array|string $data The data to check if it's expired
* @return bool
*/
private function is_expired( $data ) {
Expand Down

0 comments on commit f85b275

Please sign in to comment.