Skip to content

Commit

Permalink
Initial plugin implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoJunkie committed Jan 21, 2025
1 parent 833b219 commit f2268f3
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 1 deletion.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
# automatewoo-workflow-rule-last-run-date
| :exclamation: This is a public repository |
|--------------------------------------------|

# AutomateWoo Workflow Rule - Last Run Date

Extends the functionality of AutomateWoo with a custom "Workflow" rule which checks the last time a workflow ran.

## Usage

This works similarly to the [AutomateWoo "Workflow – Last Run Date For Customer" rule](https://woocommerce.com/document/automatewoo/rules/list/#section-4), except it checks for when the workflow ran at all, not just for a particular customer. Input your the timeframe to check against. If the workflow ran within the defined timeframe, it will return true.

Tips:
- Is available for any trigger, since it doesn't use a specific order or subscription object

![Example usage](screenshot.png)

## Support

This plugin is provided without any support or guarantees of functionality. If you'd like to contribute, feel free to open a PR on this repo. If you have a request, please open an issue.

> [!WARNING]
> Please test thoroughly before deploying to a production site.
27 changes: 27 additions & 0 deletions automatewoo-workflow-rule-last-run-date.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/*
* Plugin Name: AutomateWoo Workflow Rule - Last Run Date
* Plugin URI: https://github.com/a8cteam51/automatewoo-workflow-rule-last-run-date
* Description: Extends the functionality of AutomateWoo with a custom rule which checks when a workflow last ran
* Version: 1.0.0
* Author: WP Special Projects
* Author URI: https://wpspecialprojects.wordpress.com/
* License: GPL v2 or later
*/

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}

error_log( 'automatewoo-workflow-rule-last-run-date.php loaded' );
add_filter( 'automatewoo/rules/includes', 'to51_automatewoo_workflow_last_run_rules' );

/**
* @param array $rules
* @return array
*/
function to51_automatewoo_workflow_last_run_rules( $rules ) {
error_log( 'to51_automatewoo_workflow_last_run_rules' );
$rules['workflow_last_run_date'] = dirname( __FILE__ ) . '/includes/class-workflow-last-run-date.php';
return $rules;
}
66 changes: 66 additions & 0 deletions includes/class-workflow-last-run-date.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace AutomateWoo\Rules;

defined( 'ABSPATH' ) || exit;

/**
* Rule to check when workflow last ran.
*/
class Workflow_Last_Run_Date extends Abstract_Date {

/**
* What data we're using to validate.
*
* @var string
*/
public $data_item = 'shop';

/**
* Constructor.
*/
public function __construct() {
$this->has_is_past_comparision = true;
parent::__construct();
}

/**
* Init.
*/
public function init() {
$this->title = __( 'Workflow - Last Run Date', 'automatewoo' );
}

/**
* Validates rule.
*
* @param mixed $data_item The data item passed by the trigger (unused).
* @param string $compare The type of comparison.
* @param mixed $value The values we have to compare. Null is allowed when $compare is is_not_set.
*
* @return bool
*/
public function validate( $data_item, $compare, $value = null ) {
$workflow = $this->get_workflow();

if ( ! $workflow ) {
return false;
}

// Query the most recent log entry
$query = new \AutomateWoo\Log_Query();
$query->where_workflow( $workflow->get_id() );
$query->set_limit( 1 );
$query->set_ordering( 'date', 'DESC' );

$logs = $query->get_results();

if ( empty( $logs ) ) {
return $compare === 'is_not_set';
}

return $this->validate_date( $compare, $value, $logs[0]->get_date() );
}
}

return new Workflow_Last_Run_Date();
Binary file added screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f2268f3

Please sign in to comment.