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

1st pass on conditionals #91

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
92 changes: 92 additions & 0 deletions assets/js/rules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@

console.log( 'TEST');

var addRuleTypeCategory = BBLogic.api.addRuleTypeCategory,
addRuleType = BBLogic.api.addRuleType,
getFormPreset = BBLogic.api.getFormPreset,
__ = BBLogic.i18n.__;


addRuleTypeCategory('pods', {
label: __('Pods')
});


addRuleType('pods/settings-field', {
label: __('Pods Settings Field'),
category: 'pods',
form: getFormPreset('key-value'), // string,date,number,string
});


addRuleType('pods/date-field', {
label: __('Pods Date Field'),
category: 'pods',
form: function ( props ) {
var rule = props.rule;
var post_type = rule.post_type;
var taxonomy = rule.taxonomy;

console.log( props );

return {
post_type: {
type: 'select',
route: 'bb-logic/v1/wordpress/post-types'
},
key: {
type: 'text',
placeholder: __('Key')
},
operator: {
type: 'operator',
operators: ['equals', 'does_not_equal']
},
taxonomy: {
type: 'select',
route: 'bb-logic/v1/wordpress/taxonomies'
},
term: {
type: 'select',
route: taxonomy ? 'bb-logic/v1/wordpress/terms?taxonomy=' + taxonomy : null,
visible: taxonomy
}
};
} // string,date,number,string
});


// Test / Example
addRuleType( 'pods/user-post-count', {
label: __( 'User Post Count' ),
category: 'user',
form: {
operator: {
type: 'operator',
operators: [
'equals',
'does_not_equal',
'is_less_than',
'is_greater_than',
],
},
count: { //compare
type: 'number',
defaultValue: '0',
},
my_select: {
type: 'select',
// route: 'bb-logic/v1/wordpress/taxonomies',
options: [
{
label: 'Option 1',
value: '1',
},
{
label: 'Option 2',
value: '2',
},
],
},
},
} )
8 changes: 6 additions & 2 deletions classes/class-pods-beaver-page-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -554,13 +554,13 @@ public static function pods_get_settings_fields( $field_options = array() ) {
),
'type' => array(
'type' => 'select',
'label' => __( 'User "type"', 'pods-beaver-builder-themer-add-on' ),
'label' => __( 'Select User "type"', 'pods-beaver-builder-themer-add-on' ),
'options' => array(
'author' => __( 'Author (post_author)', 'pods-beaver-builder-themer-add-on' ),
'modified' => __( 'Author (last modified) ', 'pods-beaver-builder-themer-add-on' ),
'logged_in' => __( 'Logged in User', 'pods-beaver-builder-themer-add-on' ),
),
'description' => __( 'Only affects user fields', 'pods-beaver-builder-themer-add-on' ),
'description' => __( 'Not needed for Settings Pod', 'pods-beaver-builder-themer-add-on' ),
),
);

Expand Down Expand Up @@ -617,6 +617,10 @@ private static function recurse_pod_fields( $pod_name, $field_options = array(),

$all_pod_fields = $pod->fields();

/* if ( isset( $pod->pod_data['object_fields'] ) ) {
$all_pod_fields = array_merge( $all_pod_fields, $pod->pod_data['object_fields'] );
}*/

/*if ( isset( $pod->pod_data['object_fields']['post_author'] ) ) {
$all_pod_fields['post_author'] = $pod->pod_data['object_fields']['post_author'];
}*/
Expand Down
125 changes: 125 additions & 0 deletions includes/rules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

/**
* Server side processing for ACF rules.
*
* @since 0.1
*/
final class BB_Logic_Rules_Pods {

/**
* Sets up callbacks for conditional logic rules.
*
* @since 0.1
* @return void
*/
static public function init() {
if ( ! class_exists( 'pods' ) ) {
return;
}

BB_Logic_Rules::register( array(
'pods/archive-field' => __CLASS__ . '::archive_field',
'pods/option-field' => __CLASS__ . '::option_field',
'pods/post-field' => __CLASS__ . '::post_field',
'pods/post-author-field' => __CLASS__ . '::post_author_field',
'pods/user-field' => __CLASS__ . '::user_field',
) );
}

/**
* Process an Pods rule based on the object ID of the
* field location such as archive, post or user.
*
* @since 0.1
* @param string $object_id
* @param object $rule
* @return bool
*/
static public function evaluate_rule( $object_id = false, $rule ) {
$value = get_field( $rule->key, $object_id );

if ( is_array( $value ) ) {
$value = empty( $value ) ? 0 : 1;
} elseif ( is_object( $value ) ) {
$value = 1;
}

return BB_Logic_Rules::evaluate_rule( array(
'value' => $value,
'operator' => $rule->operator,
'compare' => $rule->compare,
'isset' => $value,
) );
}

/**
* Archive field rule.
*
* @since 0.1
* @param object $rule
* @return bool
*/
static public function archive_field( $rule ) {
$object = get_queried_object();

if ( ! is_object( $object ) || ! isset( $object->taxonomy ) || ! isset( $object->term_id ) ) {
$id = 'archive';
} else {
$id = $object->taxonomy . '_' . $object->term_id;
}

return self::evaluate_rule( $id, $rule );
}

/**
* Option field rule.
*
* @since 0.1
* @param object $rule
* @return bool
*/
static public function option_field( $rule ) {
return self::evaluate_rule( 'option', $rule );
}

/**
* Post field rule.
*
* @since 0.1
* @param object $rule
* @return bool
*/
static public function post_field( $rule ) {
global $post;
$id = is_object( $post ) ? $post->ID : 0;
return self::evaluate_rule( $id, $rule );
}

/**
* Post author field rule.
*
* @since 0.1
* @param object $rule
* @return bool
*/
static public function post_author_field( $rule ) {
global $post;
$id = is_object( $post ) ? $post->post_author : 0;
return self::evaluate_rule( 'user_' . $id, $rule );
}

/**
* User field rule.
*
* @since 0.1
* @param object $rule
* @return bool
*/
static public function user_field( $rule ) {
$user = wp_get_current_user();
return self::evaluate_rule( 'user_' . $user->ID, $rule );
}
}

BB_Logic_Rules_Pods::init();
45 changes: 43 additions & 2 deletions pods-beaver-themer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: Pods Beaver Themer Add-On
* Plugin URI: http://pods.io/
* Description: Integration with Beaver Builder Themer (https://www.wpbeaverbuilder.com). Provides a UI for mapping Field Connections with Pods
* Version: 1.3.4
* Version: 1.4-a-1
* Author: Quasel, Pods Framework Team
* Author URI: http://pods.io/about/
* Text Domain: pods-beaver-builder-themer-add-on
Expand All @@ -30,7 +30,7 @@
* @package Pods\Beaver Themer
*/

define( 'PODS_BEAVER_VERSION', '1.3.4' );
define( 'PODS_BEAVER_VERSION', '1.4-a-1' );
define( 'PODS_BEAVER_FILE', __FILE__ );
define( 'PODS_BEAVER_DIR', plugin_dir_path( PODS_BEAVER_FILE ) );
define( 'PODS_BEAVER_URL', plugin_dir_url( PODS_BEAVER_FILE ) );
Expand Down Expand Up @@ -75,6 +75,19 @@ function pods_beaver_init() {

add_action( 'fl_page_data_add_properties', 'pods_beaver_init' );

/**
* Add support for the new bb_logic
* Server Side Rule Processing
* Frontend Rule Registration
*
* @since 1.4
*/
if ( defined( 'FL_THEME_BUILDER_VERSION' ) && version_compare( FL_THEME_BUILDER_VERSION, '1.1', '>' ) ) {
add_action( 'bb_logic_init', 'pods_beaver_bb_logic_init');
add_action( 'bb_logic_enqueue_scripts', 'pods_beaver_bb_logic_enqueue' );
}


/**
* Admin nag if Pods or Beaver Builder are not activated.
*
Expand Down Expand Up @@ -165,6 +178,34 @@ function pods_beaver_fake_loop_false() {

}

/**
* Server Side Rule Processing
*
* @return void
* @since 1.4
*/
function pods_beaver_bb_logic_init() {
require_once PODS_BEAVER_DIR . 'includes/rules.php';
}

/**
* Frontend Rule Registration
* Handles enqueuing css and js assets
*
* @return void
* @since 1.4
*/
function pods_beaver_bb_logic_enqueue() {
wp_enqueue_script(
'bb-logic-rules-pods',
PODS_BEAVER_URL . 'assets/js/rules.js',
array( 'bb-logic-core' ),
'',
true
);
}


/**
* Adds the custom code settings for custom post module layouts.
*
Expand Down
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Tags: pods, beaver builder, beaver themer
Requires at least: 4.4
Tested up to: 5.5
Requires PHP: 5.4
Stable tag: 1.3.4
Stable tag: 1.4-a-1
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Expand Down