rule = $rule;
$this->group_id = $group_id;
$this->rule_id = $rule_id;
$this->value = $value;
}
/*
* Get field html markup
* @param $type string Field type
* @return string
*/
public function get_field( $type ) {
$this->field_name = "awl_label_params[conditions][group_{$this->group_id}][rule_{$this->rule_id}][{$type}]";
return call_user_func( array( $this, 'get_rule_' . $type ) );
}
/*
* Rule html
* @return string
*/
public function get_rule() {
$params_html = $this->get_field( 'param' );
$operators_html = $this->get_field( 'operator' );
$values_html = $this->get_field( 'value' );
$suboptions_html = $this->get_field( 'suboption' );
$class = $suboptions_html ? ' adv' : '';
$rule_html = '';
$rule_html .= '
';
$rule_html .= '| ';
$rule_html .= $params_html . $suboptions_html;
$rule_html .= ' | ';
$rule_html .= '';
$rule_html .= $operators_html;
$rule_html .= ' | ';
$rule_html .= '';
$rule_html .= $values_html;
$rule_html .= ' | ';
$rule_html .= '';
$rule_html .= '' . __( 'and', 'advanced-woo-labels' ) . '';
$rule_html .= ' | ';
$rule_html .= '';
$rule_html .= '';
$rule_html .= ' | ';
$rule_html .= '
';
return $rule_html;
}
/*
* Rules params html
* @param $rule array Rule
* @return string
*/
private function get_rule_param() {
$rules = AWL_Admin_Options::include_rules();
$val = ( $this->value && is_array( $this->value ) && isset( $this->value['param'] ) ) ? $this->value['param'] : '';
$rules_html = '';
return $rules_html;
}
/*
* Rules suboptions html
* @return string
*/
private function get_rule_suboption() {
$val = ( $this->value && is_array( $this->value ) && isset( $this->value['suboption'] ) ) ? $this->value['suboption'] : '';
$rules_html = '';
if ( isset( $this->rule['suboption'] ) ) {
if ( isset( $this->rule['suboption']['ajax'] ) && isset( $this->rule['suboption']['ajax']['action'] )
&& ! ( is_ajax() && isset( $_REQUEST['action'] ) && ( $_REQUEST['action'] === 'awl-getRuleGroup' || $_REQUEST['action'] === 'awl-getSuboptionValues' ) && isset( $this->rule['choices']['ajax']['input'] ) && $this->rule['choices']['ajax']['input'] === 0 )
) {
$ajax_callback = $this->rule['suboption']['callback'];
$action = $this->rule['suboption']['ajax']['action'];
$value_callback = isset( $this->rule['suboption']['ajax']['value_callback'] ) ? $this->rule['suboption']['ajax']['value_callback'] : '';
$min_ajax_input = isset( $this->rule['suboption']['ajax']['input'] ) ? intval( $this->rule['suboption']['ajax']['input'] ) : 0;
$placeholder = isset( $this->rule['suboption']['ajax']['placeholder'] ) ? $this->rule['suboption']['ajax']['placeholder'] : '';
$displayed_val = $val;
if ( $val && $value_callback ) {
$displayed_val = call_user_func_array( $value_callback, array( $val ) );
}
$rules_html = '';
} elseif ( isset( $this->rule['suboption']['callback'] ) && isset( $this->rule['suboption']['params'] ) ) {
$values_callback = self::get_rule_callback_options( $this->rule['suboption']['callback'], $this->rule['suboption']['params'], $val );
if ( $values_callback ) {
$options_number = substr_count( $values_callback, '' );
$val_class = $options_number > 15 ? ' awl-select2' : '';
$rules_html = '';
}
} else {
$rules_html = '';
}
}
return $rules_html;
}
/*
* Rules values html markup
* @return string
*/
private function get_rule_value() {
$val = ( $this->value && is_array( $this->value ) && isset( $this->value['value'] ) ) ? $this->value['value'] : '';
$sub_val = ( $this->value && is_array( $this->value ) && isset( $this->value['suboption'] ) ) ? $this->value['suboption'] : '';
$is_multiple = isset( $this->rule['multiple'] ) && $this->rule['multiple'];
$multiple_attr = $is_multiple ? ' multiple="multiple" ' : '';
$field_name = $is_multiple ? $this->field_name . '[]' : $this->field_name;
$values = '';
switch( $this->rule['type'] ) {
case 'callback';
$callback_function = $this->rule['choices']['callback'];
$callback_params = $sub_val ? array( $sub_val ) : $this->rule['choices']['params'];
if ( isset( $this->rule['suboption'] ) && empty( $callback_params ) ) {
$values_suboption = call_user_func_array( $this->rule['suboption']['callback'], $this->rule['suboption']['params'] );
if ( $values_suboption && is_array( $values_suboption ) && ! empty( $values_suboption ) ) {
foreach ( $values_suboption as $values_val => $values_name ) {
if ( is_array( $values_name ) && isset( $values_name['id'] ) ) {
$values_val = $values_name['id'];
}
$callback_params = array( $values_val );
break;
}
}
}
if ( isset( $this->rule['choices']['ajax'] ) && isset( $this->rule['choices']['ajax']['action'] )
&& ! ( is_ajax() && isset( $_REQUEST['action'] ) && ( $_REQUEST['action'] === 'awl-getRuleGroup' || $_REQUEST['action'] === 'awl-getSuboptionValues' ) && isset( $this->rule['choices']['ajax']['input'] ) && $this->rule['choices']['ajax']['input'] === 0 )
) {
$action = $this->rule['choices']['ajax']['action'];
$value_callback = isset( $this->rule['choices']['ajax']['value_callback'] ) ? $this->rule['choices']['ajax']['value_callback'] : '';
$min_ajax_input = isset( $this->rule['choices']['ajax']['input'] ) ? intval( $this->rule['choices']['ajax']['input'] ) : 0;
$placeholder = isset( $this->rule['choices']['ajax']['placeholder'] ) ? $this->rule['choices']['ajax']['placeholder'] : '';
$displayed_val = $val;
if ( $val && $value_callback ) {
$displayed_val = $this->get_ajax_displayed_options( $value_callback, $callback_params, $val );
}
$values = '';
} else {
$values_callback = $this->get_rule_callback_options( $callback_function, $callback_params, $val );
if ( $values_callback ) {
$options_number = substr_count( $values_callback, '' );
$val_class = $options_number > 15 || $is_multiple ? ' awl-select2' : '';
$placeholder = isset( $this->rule['placeholder'] ) && $this->rule['placeholder'] ? $this->rule['placeholder'] : '';
if ( ! $placeholder && $is_multiple ) {
$placeholder = __( 'Select...', 'advanced-woo-labels' );
}
if ( isset( $this->rule['suboption'] ) ) {
$values_callback = '' . $values_callback;
}
$values = '';
} else {
$values = '';
}
}
break;
case 'bool';
$values .= '';
break;
case 'number';
$step = isset( $this->rule['step'] ) ? 'step="' . esc_attr( $this->rule['step'] ) . '"' : '';
$values .= '';
break;
case 'text';
$placeholder = $this->rule['placeholder'] ? $this->rule['placeholder'] : '';
$val = $val === 'awl_any' ? '' : $val;
$values .= '';
break;
}
return $values;
}
/*
* Rules operators html markup
* @return string
*/
private function get_rule_operator() {
$val = ( $this->value && is_array( $this->value ) && isset( $this->value['operator'] ) ) ? $this->value['operator'] : '';
$operators = AWL_Admin_Options::get_rule_operators( $this->rule['operators'] );
$operators_html = '';
return $operators_html;
}
/*
* Rules callback options
* @param $callback string Function name
* @param $params array Function parameters
* @param $value mixed Current option value
* @return string
*/
private function get_rule_callback_options( $callback, $params, $value = false ) {
$values = '';
$values_arr = call_user_func_array( $callback, $params );
$selected_values = is_array( $value ) ? array_map( 'strval', $value ) : array( (string) $value );
if ( $values_arr && is_array( $values_arr ) && ! empty( $values_arr ) ) {
foreach ( $values_arr as $values_val => $values_name ) {
if ( is_array( $values_name ) && isset( $values_name['text'] ) && isset( $values_name['id'] ) ) {
$values_val = $values_name['id'];
$values_name = $values_name['text'];
}
// depricated
if ( is_array( $values_name ) && isset( $values_name['name'] ) && isset( $values_name['value'] ) ) {
$values_val = $values_name['value'];
$values_name = $values_name['name'];
}
$selected = in_array( (string) $values_val, $selected_values, true ) ? ' selected="selected"' : '';
$values .= '';
}
}
return $values;
}
/*
* Rules ajax callback options
* @param string $callback Callback function name
* @param array $params Callback params
* @param string|array $value Current option value
* @return string
*/
private function get_ajax_displayed_options( $callback, $params, $value ) {
$values = is_array( $value ) ? $value : array( $value );
$options = '';
foreach ( $values as $current_value ) {
if ( '' === $current_value || null === $current_value ) {
continue;
}
$callback_params = $params;
$callback_params[] = $current_value;
$displayed_val = call_user_func_array( $callback, $callback_params );
if ( $displayed_val === 'awl_any' ) {
$displayed_val = __( 'Any', 'advanced-woo-labels' );
}
$options .= '';
}
return $options;
}
}
endif;