Skip to content

Commit

Permalink
Merge pull request #42 from humanmade/add-cs
Browse files Browse the repository at this point in the history
Add coding standards checks
  • Loading branch information
rmccue committed Jun 30, 2017
2 parents 60927a6 + 3e00c9a commit 9c5ca66
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 173 deletions.
7 changes: 7 additions & 0 deletions .travis.yml
@@ -0,0 +1,7 @@
language: php
php:
- '7.1'
install:
- composer install
script:
- vendor/bin/phpcs --standard=phpcs.ruleset.xml .
29 changes: 16 additions & 13 deletions composer.json
@@ -1,15 +1,18 @@
{
"name": "humanmade/cavalcade",
"description": "A better wp-cron. Horizontally scalable, works perfectly with multisite.",
"homepage": "https://github.com/humanmade/Cavalcade",
"type": "wordpress-muplugin",
"authors": [
{
"name": "Human Made",
"homepage": "https://hmn.md/"
}
],
"require": {
"composer/installers": "~1.0"
}
"name": "humanmade/cavalcade",
"description": "A better wp-cron. Horizontally scalable, works perfectly with multisite.",
"homepage": "https://github.com/humanmade/Cavalcade",
"type": "wordpress-muplugin",
"authors": [
{
"name": "Human Made",
"homepage": "https://hmn.md/"
}
],
"require": {
"composer/installers": "~1.0"
},
"require-dev": {
"humanmade/coding-standards": "dev-master"
}
}
32 changes: 16 additions & 16 deletions class-command.php → inc/class-command.php
Expand Up @@ -51,23 +51,23 @@ public function log( $args, $assoc_args ) {
$log_table = $wpdb->base_prefix . 'cavalcade_logs';
$job_table = $wpdb->base_prefix . 'cavalcade_jobs';

$assoc_args = wp_parse_args( $assoc_args, array(
$assoc_args = wp_parse_args( $assoc_args, [
'format' => 'table',
'fields' => 'job,hook,timestamp,status',
'hook' => null,
'job' => null,
));
]);

$where = array();
$data = array();
$where = [];
$data = [];

if ( $assoc_args['job'] ) {
$where[] = "job = %d";
$where[] = 'job = %d';
$data[] = $assoc_args['job'];
}

if ( $assoc_args['hook'] ) {
$where[] = "hook = %s";
$where[] = 'hook = %s';
$data[] = $assoc_args['hook'];
}

Expand All @@ -93,7 +93,7 @@ public function jobs( $args, $assoc_args ) {

global $wpdb;

$assoc_args = wp_parse_args( $assoc_args, array(
$assoc_args = wp_parse_args( $assoc_args, [
'format' => 'table',
'fields' => 'id,site,hook,start,nextrun,status',
'id' => null,
Expand All @@ -102,36 +102,36 @@ public function jobs( $args, $assoc_args ) {
'status' => null,
'limit' => 20,
'page' => 1,
));
]);

$where = array();
$data = array();
$where = [];
$data = [];

if ( $assoc_args['id'] ) {
$where[] = "id = %d";
$where[] = 'id = %d';
$data[] = $assoc_args['id'];
}

if ( $assoc_args['site'] ) {
$where[] = "site = %d";
$where[] = 'site = %d';
$data[] = $assoc_args['site'];
}

if ( $assoc_args['hook'] ) {
$where[] = "hook = %s";
$where[] = 'hook = %s';
$data[] = $assoc_args['hook'];
}

if ( $assoc_args['status'] ) {
$where[] = "status = %s";
$where[] = 'status = %s';
$data[] = $assoc_args['status'];
}

$where = $where ? 'WHERE ' . implode( ' AND ', $where ) : '';

$limit = "LIMIT %d";
$limit = 'LIMIT %d';
$data[] = absint( $assoc_args['limit'] );
$offset = "OFFSET %d";
$offset = 'OFFSET %d';
$data[] = absint( ( $assoc_args['page'] - 1 ) * $assoc_args['limit'] );

$query = "SELECT * FROM {$wpdb->base_prefix}cavalcade_jobs $where $limit $offset";
Expand Down
38 changes: 18 additions & 20 deletions class-job.php → inc/class-job.php
Expand Up @@ -39,13 +39,13 @@ public function is_recurring() {
public function save() {
global $wpdb;

$data = array(
$data = [
'hook' => $this->hook,
'site' => $this->site,
'start' => gmdate( MYSQL_DATE_FORMAT, $this->start ),
'nextrun' => gmdate( MYSQL_DATE_FORMAT, $this->nextrun ),
'args' => serialize( $this->args ),
);
];

if ( $this->is_recurring() ) {
$data['interval'] = $this->interval;
Expand All @@ -54,33 +54,32 @@ public function save() {
wp_cache_delete( 'jobs', 'cavalcade-jobs' );

if ( $this->is_created() ) {
$where = array(
$where = [
'id' => $this->id,
);
];
$result = $wpdb->update( $this->get_table(), $data, $where, $this->row_format( $data ), $this->row_format( $where ) );
}
else {
} else {
$result = $wpdb->insert( $this->get_table(), $data, $this->row_format( $data ) );
$this->id = $wpdb->insert_id;
}
}

public function delete( $options = array() ) {
public function delete( $options = [] ) {
global $wpdb;
$wpdb->show_errors();

$defaults = array(
$defaults = [
'delete_running' => false,
);
];
$options = wp_parse_args( $options, $defaults );

if ( $this->status === 'running' && ! $options['delete_running'] ) {
return new WP_Error( 'cavalcade.job.delete.still_running', __( 'Cannot delete running jobs', 'cavalcade' ) );
}

$where = array(
$where = [
'id' => $this->id,
);
];
$result = $wpdb->delete( $this->get_table(), $where, $this->row_format( $where ) );

wp_cache_delete( 'jobs', 'cavalcade-jobs' );
Expand Down Expand Up @@ -122,7 +121,7 @@ protected static function to_instance( $row ) {
* @return Job[]
*/
protected static function to_instances( $rows ) {
return array_map( array( get_called_class(), 'to_instance' ), $rows );
return array_map( [ get_called_class(), 'to_instance' ], $rows );
}

/**
Expand Down Expand Up @@ -176,7 +175,7 @@ public static function get_by_site( $site, $include_completed = false, $include_
}

if ( isset( $results ) && ! $results ) {
$statuses = array( 'waiting', 'running' );
$statuses = [ 'waiting', 'running' ];
if ( $include_completed ) {
$statuses[] = 'completed';
}
Expand All @@ -188,18 +187,17 @@ public static function get_by_site( $site, $include_completed = false, $include_
$table = static::get_table();

$sql = "SELECT * FROM `{$table}` WHERE site = %d";
$sql .= " AND status IN(" . implode( ',', array_fill( 0, count( $statuses ), '%s' ) ) . ")";
$query = $wpdb->prepare( $sql, array_merge( array( $site ), $statuses ) );
$sql .= ' AND status IN(' . implode( ',', array_fill( 0, count( $statuses ), '%s' ) ) . ')';
$query = $wpdb->prepare( $sql, array_merge( [ $site ], $statuses ) );
$results = $wpdb->get_results( $query );

if ( ! $include_completed && ! $include_failed ) {
wp_cache_set( 'jobs', $results, 'cavalcade-jobs' );
}

}

if ( empty( $results ) ) {
return array();
return [];
}

return static::to_instances( $results );
Expand All @@ -212,7 +210,7 @@ public static function get_by_site( $site, $include_completed = false, $include_
* @return string Format specifier. Defaults to '%s'
*/
protected static function column_format( $column ) {
$columns = array(
$columns = [
'id' => '%d',
'site' => '%d',
'hook' => '%s',
Expand All @@ -221,7 +219,7 @@ protected static function column_format( $column ) {
'nextrun' => '%s',
'interval' => '%d',
'status' => '%s',
);
];

if ( isset( $columns[ $column ] ) ) {
return $columns[ $column ];
Expand All @@ -237,7 +235,7 @@ protected static function column_format( $column ) {
* @return array List of formats for fields in the row. Order matches the input order.
*/
protected static function row_format( $row ) {
$format = array();
$format = [];
foreach ( $row as $field => $value ) {
$format[] = static::column_format( $field );
}
Expand Down
35 changes: 21 additions & 14 deletions connector.php → inc/connector/namespace.php
@@ -1,9 +1,16 @@
<?php

namespace HM\Cavalcade\Plugin;
namespace HM\Cavalcade\Plugin\Connector;

add_filter( 'pre_update_option_cron', __NAMESPACE__ . '\\update_cron_array', 10, 2 );
add_filter( 'pre_option_cron', __NAMESPACE__ . '\\get_cron_array' );
use HM\Cavalcade\Job;

/**
* Register hooks for WordPress.
*/
function bootstrap() {
add_filter( 'pre_update_option_cron', __NAMESPACE__ . '\\update_cron_array', 10, 2 );
add_filter( 'pre_option_cron', __NAMESPACE__ . '\\get_cron_array' );
}

/**
* Schedule an event with Cavalcade
Expand Down Expand Up @@ -71,7 +78,7 @@ function update_cron_array( $value, $old_value ) {

// Massage so we can compare
$massager = function ( $crons ) {
$new = array();
$new = [];

foreach ( $crons as $timestamp => $hooks ) {
foreach ( $hooks as $hook => $groups ) {
Expand All @@ -83,12 +90,12 @@ function update_cron_array( $value, $old_value ) {
}

$real_key = sha1( $timestamp . $hook . $key );
$new[ $real_key ] = array(
$new[ $real_key ] = [
'timestamp' => $timestamp,
'hook' => $hook,
'key' => $key,
'value' => $item
);
'value' => $item,
];
}
}
}
Expand All @@ -109,11 +116,11 @@ function update_cron_array( $value, $old_value ) {
}

// Added new event
$event = (object) array(
$event = (object) [
'hook' => $item['hook'],
'timestamp' => $item['timestamp'],
'args' => $item['value']['args'],
);
];
if ( ! empty( $item['value']['schedule'] ) ) {
$event->schedule = $item['value']['schedule'];
$event->interval = $item['value']['interval'];
Expand Down Expand Up @@ -160,28 +167,28 @@ function get_cron_array( $value ) {
}

// Massage into the correct format
$crons = array();
$crons = [];
$results = get_jobs();
foreach ( $results as $result ) {
$timestamp = $result->nextrun;
$hook = $result->hook;
$key = md5( serialize( $result->args ) );
$value = array(
$value = [
'schedule' => '__fake_schedule',
'args' => $result->args,
'_job' => $result,
);
];

if ( isset( $result->interval ) ) {
$value['interval'] = $result->interval;
}

// Build the array up, urgh
if ( ! isset( $crons[ $timestamp ] ) ) {
$crons[ $timestamp ] = array();
$crons[ $timestamp ] = [];
}
if ( ! isset( $crons[ $timestamp ][ $hook ] ) ) {
$crons[ $timestamp ][ $hook ] = array();
$crons[ $timestamp ][ $hook ] = [];
}
$crons[ $timestamp ][ $hook ][ $key ] = $value;
}
Expand Down

0 comments on commit 9c5ca66

Please sign in to comment.