Skip to content

Commit

Permalink
Enforce PSR-2 PHP formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmacarthur committed Feb 11, 2019
1 parent 9090d40 commit 303646d
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 102 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
yarn-error.log
.php_cs.cache
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ add_filter('ramsey-batch-table-columns', function ($columns) {
$columns[] = [
'id' => 'columnHeaderID',
'name' => 'Custom Column Name',
'contentKey' => 'jobArrayKeyFromWhereToPullContent',
'contentKey' => 'myColumnNameOrSlug',
];
return $columns;
});
Expand All @@ -225,7 +225,7 @@ The content of that newly registered column can be accessed by adding a new key

```php
add_filter('ramsey-batch-jobs', function ($jobs, $instance) {
$jobs['MyAppNamespace\Batch\YourJob']['jobArrayKeyFromWhereToPullContent'] = '
$jobs['MyAppNamespace\Batch\YourJob']['myColumnNameOrSlug'] = '
<input type="file" name="fileUpload">
';

Expand Down
40 changes: 25 additions & 15 deletions src/BatchJob.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
namespace RamseySolutions\RamseyBatch;

abstract class BatchJob {

abstract class BatchJob
{
public $items;
public $progress;
protected $lastRunTimes;
Expand Down Expand Up @@ -35,7 +35,8 @@ abstract public function registerBatchJob(array $jobs);
/**
* Constructor
*/
public function __construct() {
public function __construct()
{
$this->items = [];
$this->lastRunTimes = get_option(RB_PLUGIN_SLUG . '_batch-run-timestamps', []);
}
Expand All @@ -44,23 +45,28 @@ public function __construct() {
* Get batch items
* @return obj|array Iterable object or array
*/
public function getItems() {
public function getItems()
{
return $this->items;
}

/**
* Check if batch items are available
*/
public function hasItems() {
return !empty( $this->getItems() );
public function hasItems()
{
return !empty($this->getItems());
}

/**
* Check if the batch name is allowed to run based on the instantiated class.
* @return boolean
*/
public function isAllowedBatch() {
if( empty($_REQUEST) || !array_key_exists('batchName', $_REQUEST) ) return false;
public function isAllowedBatch()
{
if (empty($_REQUEST) || !array_key_exists('batchName', $_REQUEST)) {
return false;
}

return stripslashes($_REQUEST['batchName']) == get_class($this);
}
Expand All @@ -69,8 +75,9 @@ public function isAllowedBatch() {
* Get the timestamp corresponding to the time this particular batch was last run.
* @return float Timestamp
*/
public function getLastRunTime() {
if( empty($this->lastRunTimes) || !array_key_exists(get_called_class(), $this->lastRunTimes) ) {
public function getLastRunTime()
{
if (empty($this->lastRunTimes) || !array_key_exists(get_called_class(), $this->lastRunTimes)) {
return '';
}

Expand All @@ -83,8 +90,11 @@ public function getLastRunTime() {
* @param string $default Default string if batch has never been run
* @return string
*/
public function getLastRunDate($format = 'M j, Y H:i:s', $default = '--') {
if( !$this->getLastRunTime() ) return $default;
public function getLastRunDate($format = 'M j, Y H:i:s', $default = '--')
{
if (!$this->getLastRunTime()) {
return $default;
}
return date($format, $this->getLastRunTime());
}

Expand All @@ -93,12 +103,12 @@ public function getLastRunDate($format = 'M j, Y H:i:s', $default = '--') {
* @param float $timestamp A PHP timestamp
* @return bool True if option value has changed, false if not or if update failed.
*/
protected function updateLastRunDate($timestamp = null) {
if( !$timestamp ) {
protected function updateLastRunDate($timestamp = null)
{
if (!$timestamp) {
$timestamp = current_time('timestamp');
}
$this->lastRunTimes[get_called_class()] = $timestamp;
return update_option(RB_PLUGIN_SLUG . '_batch-run-timestamps', $this->lastRunTimes, false);
}

}
58 changes: 35 additions & 23 deletions src/Controllers/BatchController.php
Original file line number Diff line number Diff line change
@@ -1,43 +1,51 @@
<?php
namespace RamseySolutions\RamseyBatch\Controllers;

class BatchController {
class BatchController
{

/**
* Registered batch jobs
* @var array
* @type string "name"
* @type string "description"
* @type string "lastRunDate"
*/
protected $jobs;
/**
* Registered batch jobs
* @var array
* @type string "name"
* @type string "description"
* @type string "lastRunDate"
*/
protected $jobs;

/**
* Constructor
*/
public function __construct() {
public function __construct()
{
$this->jobs = apply_filters(RB_PLUGIN_SLUG . '-jobs', [], $this);
}

public static function runJob() {
if( !wp_doing_ajax() || $_REQUEST['action'] != RB_PLUGIN_SLUG ) return;
public static function runJob()
{
if (!wp_doing_ajax() || $_REQUEST['action'] != RB_PLUGIN_SLUG) {
return;
}

$currentBatchName = stripslashes($_REQUEST['batchName']);
$batchObj = $GLOBALS[RB_PLUGIN_SLUG][$currentBatchName];

if( !$batchObj ) {
if (!$batchObj) {
wp_send_json_error(['reason' => "Attempted to run job but global object not found - {$currentBatchName}."]);
}

$batchObj->run();
}

public static function runJobItem() {
if( !wp_doing_ajax() || $_REQUEST['action'] != 'ramsey-batch-item' ) return;
public static function runJobItem()
{
if (!wp_doing_ajax() || $_REQUEST['action'] != 'ramsey-batch-item') {
return;
}

$currentBatchName = stripslashes($_REQUEST['batchName']);
$batchObj = $GLOBALS[RB_PLUGIN_SLUG][$currentBatchName];
if( !$batchObj ) {
if (!$batchObj) {
wp_send_json_error(['reason' => "Attempted to run job item but global object not found - {$currentBatchName}."]);
}

Expand All @@ -48,26 +56,30 @@ public static function runJobItem() {
* Enqueue JS scripts
* @return void
*/
public static function enqueueScripts() {
public static function enqueueScripts()
{
wp_register_script(RB_PLUGIN_SLUG, RB_PLUGIN_URL . '/js/dist/ramsey-batch.min.js', ['jquery'], get_plugin_data(RB_PLUGIN_ROOT . '/wp-ramsey-batch.php', false)['Version']);

$currentScreen = get_current_screen();
if( $currentScreen->id == 'tools_page_' . RB_PLUGIN_SLUG) {
if ($currentScreen->id == 'tools_page_' . RB_PLUGIN_SLUG) {
wp_enqueue_script(RB_PLUGIN_SLUG);
}
}

public static function register(string $name) {
if( !class_exists($name) ) return;
public static function register(string $name)
{
if (!class_exists($name)) {
return;
}

$obj = new $name;
add_filter('ramsey-batch-jobs', [$obj, 'registerBatchJob']);

$GLOBALS[RB_PLUGIN_SLUG][$name] = $obj;
}

public function getJobs() {
return $this->jobs;
public function getJobs()
{
return $this->jobs;
}

}
67 changes: 35 additions & 32 deletions src/Views/AdminPage.php
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
<?php
namespace RamseySolutions\RamseyBatch\Views;

abstract class AdminPage {

protected $controller;
protected $slug;
protected $title;

abstract public function display();

public function __construct(object $controller, string $slug, string $title) {
$this->controller = $controller;
$this->slug = $slug;
$this->title = $title;
}

protected function open() {
return '<div id="' . $this->slug . '" class="wrap"><div id="icon-options-general" class="icon32"><br></div><h1 class="wp-heading-inline">' . $this->title . '</h1>';
}

protected function close() {
return '</div>';//.wrap
}

public function debug() {
dump(__CLASS__);
dump(dirname(__FILE__));
dump(get_current_screen());
dump($this->controller);
dump(RB_PLUGIN_ROOT);
dump(RB_PLUGIN_URL);
}

}
abstract class AdminPage
{
protected $controller;
protected $slug;
protected $title;

abstract public function display();

public function __construct(object $controller, string $slug, string $title)
{
$this->controller = $controller;
$this->slug = $slug;
$this->title = $title;
}

protected function open()
{
return '<div id="' . $this->slug . '" class="wrap"><div id="icon-options-general" class="icon32"><br></div><h1 class="wp-heading-inline">' . $this->title . '</h1>';
}

protected function close()
{
return '</div>';//.wrap
}

public function debug()
{
dump(__CLASS__);
dump(dirname(__FILE__));
dump(get_current_screen());
dump($this->controller);
dump(RB_PLUGIN_ROOT);
dump(RB_PLUGIN_URL);
}
}
35 changes: 18 additions & 17 deletions src/Views/BatchView.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php
namespace RamseySolutions\RamseyBatch\Views;

class BatchView extends AdminPage {

public function __construct(object $controller, string $slug, string $title) {
class BatchView extends AdminPage
{
public function __construct(object $controller, string $slug, string $title)
{
parent::__construct($controller, $slug, $title);

$this->tableColumns = apply_filters(RB_PLUGIN_SLUG . '-table-columns',
$this->tableColumns = apply_filters(
RB_PLUGIN_SLUG . '-table-columns',
[
[
'id' => 'name',
Expand All @@ -25,19 +27,18 @@ public function __construct(object $controller, string $slug, string $title) {
]
]
);
}

public function display() {
echo $this->open();
}

?>
public function display()
{
echo $this->open(); ?>
<div class="notice notice-warning">
<p>The batch jobs here are powerful <strong>and must be run with caution</strong>. It's likely that they will irreversibly change your data. Carefully read and understand each job's description before proceeding.</p>
</div>

<table class="wp-list-table widefat fixed striped">
<thead>
<?php foreach($this->tableColumns as $column): ?>
<?php foreach ($this->tableColumns as $column): ?>
<th id="<?php echo $column['id']; ?>" class="manage-column">
<?php echo $column['name']; ?>
</th>
Expand All @@ -51,26 +52,27 @@ public function display() {
</tbody>
</table>
<?php echo $this->close();
}
}

/**
/**
* List batch jobs
* @return string
*/
protected function listBatchJobs() {
protected function listBatchJobs()
{
$jobs = $this->controller->getJobs();

ob_start();

if( empty($jobs) ) {
if (empty($jobs)) {
echo '<tr><td colspan="4">There are no registered batch jobs.</td></tr>';
return ob_get_clean();
}

foreach( $jobs as $key => $job ): ?>
foreach ($jobs as $key => $job): ?>

<tr>
<?php foreach($this->tableColumns as $column): ?>
<?php foreach ($this->tableColumns as $column): ?>
<td>
<?php echo $job[$column['contentKey']]; ?>
</td>
Expand All @@ -92,5 +94,4 @@ protected function listBatchJobs() {
<?php endforeach;
return ob_get_clean();
}

}
Loading

0 comments on commit 303646d

Please sign in to comment.