Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the Easy Digital Downloads control #87

Merged
merged 5 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion build/block-visibility-editor.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('lodash', 'moment', 'react', 'react-dom', 'wp-a11y', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-core-data', 'wp-data', 'wp-date', 'wp-edit-post', 'wp-element', 'wp-hooks', 'wp-html-entities', 'wp-i18n', 'wp-notices', 'wp-plugins', 'wp-primitives', 'wp-url'), 'version' => '56471e69cc13e20ed08d');
<?php return array('dependencies' => array('lodash', 'moment', 'react', 'react-dom', 'wp-a11y', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-core-data', 'wp-data', 'wp-date', 'wp-edit-post', 'wp-element', 'wp-hooks', 'wp-html-entities', 'wp-i18n', 'wp-notices', 'wp-plugins', 'wp-primitives', 'wp-url'), 'version' => 'ed9e8c95527cf228d949');
52 changes: 27 additions & 25 deletions build/block-visibility-editor.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/block-visibility-settings.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('lodash', 'react', 'react-dom', 'wp-api-fetch', 'wp-block-editor', 'wp-block-library', 'wp-components', 'wp-core-data', 'wp-data', 'wp-date', 'wp-element', 'wp-hooks', 'wp-html-entities', 'wp-i18n', 'wp-notices', 'wp-primitives', 'wp-url'), 'version' => 'c5c4b6573da1f2e60cee');
<?php return array('dependencies' => array('lodash', 'react', 'react-dom', 'wp-api-fetch', 'wp-block-editor', 'wp-block-library', 'wp-components', 'wp-core-data', 'wp-data', 'wp-date', 'wp-element', 'wp-hooks', 'wp-html-entities', 'wp-i18n', 'wp-notices', 'wp-primitives', 'wp-url'), 'version' => '3c9316ef1608f873b851');
18 changes: 9 additions & 9 deletions build/block-visibility-settings.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion includes/frontend/render-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,11 +320,12 @@ function render_block_widget_with_visibility( $instance ) {
require_once BLOCK_VISIBILITY_ABSPATH . 'includes/frontend/visibility-tests/url-path.php';
require_once BLOCK_VISIBILITY_ABSPATH . 'includes/frontend/visibility-tests/user-role.php';
require_once BLOCK_VISIBILITY_ABSPATH . 'includes/frontend/visibility-tests/visibility-presets.php';
require_once BLOCK_VISIBILITY_ABSPATH . 'includes/frontend/visibility-tests/woocommerce/woocommerce.php';

// Run our integration tests.
require_once BLOCK_VISIBILITY_ABSPATH . 'includes/frontend/visibility-tests/acf.php';
require_once BLOCK_VISIBILITY_ABSPATH . 'includes/frontend/visibility-tests/edd/edd.php';
require_once BLOCK_VISIBILITY_ABSPATH . 'includes/frontend/visibility-tests/wp-fusion.php';
require_once BLOCK_VISIBILITY_ABSPATH . 'includes/frontend/visibility-tests/woocommerce/woocommerce.php';

// Require utlity functions for tests.
require_once BLOCK_VISIBILITY_ABSPATH . 'includes/frontend/visibility-tests/utils/value-compare-helpers.php';
Expand Down
220 changes: 220 additions & 0 deletions includes/frontend/visibility-tests/edd/edd.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
<?php
/**
* Adds a filter to the visibility test for the Easy Digital Download control.
*
* @package block-visibility
* @since 3.1.0
*/

namespace BlockVisibility\Frontend\VisibilityTests\EDD;

defined( 'ABSPATH' ) || exit;

/**
* Internal dependencies
*/
use function BlockVisibility\Utils\is_control_enabled;
require_once BLOCK_VISIBILITY_ABSPATH . 'includes/frontend/visibility-tests/edd/rule-tests.php';
require_once BLOCK_VISIBILITY_ABSPATH . 'includes/frontend/visibility-tests/edd/helper-functions.php';

/**
* Run test to see if block visibility should be restricted by Easy Digital Downloads rules.
*
* @since 3.1.0
*
* @param boolean $is_visible The current value of the visibility test.
* @param array $settings The core plugin settings.
* @param array $controls The control set controls.
* @return boolean Return true if the block should be visible, false if not.
*/
function edd_test( $is_visible, $settings, $controls ) {

// If the test is already false, or WooCommerce is not active, skip this test.
if ( ! $is_visible || ! class_exists( 'Easy_Digital_Downloads' ) ) {
return $is_visible;
}

// If this control has been disabled, skip test.
if ( ! is_control_enabled( $settings, 'edd' ) ) {
return true;
}

$control_atts = isset( $controls['edd'] ) ? $controls['edd'] : null;

// There are no control settings, so skip tests.
if ( ! $control_atts ) {
return true;
}

$rule_sets = isset( $control_atts['ruleSets'] )
? $control_atts['ruleSets']
: array();

$hide_on_rule_sets = isset( $control_atts['hideOnRuleSets'] )
? $control_atts['hideOnRuleSets']
: false;

// There are no rule sets, skip tests.
if ( ! is_array( $rule_sets ) || 0 === count( $rule_sets ) ) {
return true;
}

// Array of results for each rule set.
$rule_sets_test_results = array();

foreach ( $rule_sets as $rule_set ) {
$enable = isset( $rule_set['enable'] ) ? $rule_set['enable'] : true;
$rules =
isset( $rule_set['rules'] ) ? $rule_set['rules'] : array();

if ( $enable && 0 < count( $rules ) ) {

// Array of results for each rule within the current rule set.
$rule_set_test_results = array();

foreach ( $rules as $rule ) {

$test_result = run_rule_tests( $rule );

// If there is an error, default to showing the block.
$test_result =
'error' === $test_result ? 'visible' : $test_result;

$rule_set_test_results[] = $test_result;
}

// Within a rule set, all tests have to pass.
$rule_set_result = in_array( 'hidden', $rule_set_test_results, true )
? 'hidden'
: 'visible';

// Reverse the rule set result if hide_on_rules setting is active.
if ( $hide_on_rule_sets ) {
$rule_set_result =
'visible' === $rule_set_result ? 'hidden' : 'visible';
}

// Pass the rule set result to the rule *sets* test results array.
$rule_sets_test_results[] = $rule_set_result;
}
}

// If there are no enabled rule sets, or if the rule sets have no set rules,
// there will be no results. Default to showing the block.
if ( empty( $rule_sets_test_results ) ) {
return true;
}

// Under normal circumstances, need no "visible" results to hide the block.
// When hide_on_rule_sets is enabled, we need at least one "hidden" to hide.
if (
! $hide_on_rule_sets &&
! in_array( 'visible', $rule_sets_test_results, true )
) {
return false;
} elseif (
$hide_on_rule_sets &&
in_array( 'hidden', $rule_sets_test_results, true )
) {
return false;
} else {
return true;
}
}

// Run all integration tests at "15" priority, which is after the main controls,
// but before the final "hide block" tests.
add_filter( 'block_visibility_control_set_is_block_visible', __NAMESPACE__ . '\edd_test', 15, 3 );

/**
* Run the individual rule tests.
*
* @since 3.1.0
*
* @param array $rule All rule settings.
* @return string Returns 'visible', 'hidden', or 'error'.
*/
function run_rule_tests( $rule ) {

$field = isset( $rule['field'] ) ? $rule['field'] : null;

// No field is set, so return an error.
if ( ! $field ) {
return 'error';
}

switch ( $field ) {

// Cart rule tests.
case 'cartContents':
$test_result = run_cart_contents_test( $rule );
break;

case 'cartTotalQuantity':
$test_result = run_cart_total_quantity_test( $rule );
break;

case 'cartTotalValue':
$test_result = run_cart_total_value_test( $rule );
break;

case 'cartProductQuantity':
$test_result = run_cart_product_quantity_test( $rule, 'quantity' );
break;

case 'cartCategoryQuantity':
$test_result = run_cart_category_quantity_test( $rule, 'quantity' );
break;

// Customer rule tests.
case 'customerTotalSpent':
$test_result = run_customer_total_spent_test( $rule );
break;

case 'customerTotalOrders':
$test_result = run_customer_total_orders_test( $rule );
break;

case 'customerAverageOrderValue':
$test_result = run_customer_average_order_value_test( $rule );
break;

case 'customerQuantityProductOrdered':
$test_result = run_customer_quantity_product_ordered_test( $rule, 'quantity' );
break;

case 'customerQuantityCategoryOrdered':
$test_result = run_customer_quantity_category_ordered_test( $rule, 'quantity' );
break;

case 'customerTimeSinceOrder':
$test_result = run_customer_time_since_order_test( $rule );
break;

case 'customerTimeSinceProductOrdered':
$test_result = run_customer_time_since_product_ordered_test( $rule );
break;

case 'customerTimeSinceCategoryOrdered':
$test_result = run_customer_time_since_category_ordered_test( $rule );
break;

case 'customerDateOfOrder':
$test_result = run_customer_date_of_order_test( $rule );
break;

case 'customerDateOfProductOrdered':
$test_result = run_customer_date_of_product_ordered_test( $rule );
break;

case 'customerDateOfCategoryOrdered':
$test_result = run_customer_date_of_category_ordered_test( $rule );
break;

default:
$test_result = 'error';
break;
}

return $test_result;
}