type = isset( $attr['type'] ) ? ( $attr['type'] ? $attr['type'] : 'text' ) : 'text';
$this->multiple = isset( $attr['multiple'] ) ? ( $attr['multiple'] ? $attr['multiple'] : false ) : false;
$this->name = ! empty( $key ) ? $key : null;
$id = isset( $attr['id'] ) ? $attr['id'] : null;
$this->id = ! empty( $id ) ? $id : $this->name;
$this->default = isset( $attr['default'] ) ? $attr['default'] : null;
$this->value = isset( $attr['value'] ) ? ( $attr['value'] ? $attr['value'] : null ) : null;
if ( ! $this->value ) {
$post_id = get_the_ID();
if ( ! Fns::meta_exist( $this->name, $post_id ) && $pagenow == 'post-new.php' ) {
$this->value = $this->default;
} else {
if ( $this->multiple ) {
if ( metadata_exists( 'post', $post_id, $this->name ) ) {
$this->value = get_post_meta( $post_id, $this->name );
} else {
$this->value = $this->default;
}
} else {
if ( metadata_exists( 'post', $post_id, $this->name ) ) {
$this->value = get_post_meta( $post_id, $this->name, true );
} else {
$this->value = $this->default;
}
}
}
}
$this->label = isset( $attr['label'] ) ? ( $attr['label'] ? $attr['label'] : null ) : null;
$this->class = isset( $attr['class'] ) ? ( $attr['class'] ? $attr['class'] : null ) : null;
$this->holderClass = isset( $attr['holderClass'] ) ? ( $attr['holderClass'] ? $attr['holderClass'] : null ) : null;
$this->placeholder = isset( $attr['placeholder'] ) ? ( $attr['placeholder'] ? $attr['placeholder'] : null ) : null;
$this->description = isset( $attr['description'] ) ? ( $attr['description'] ? $attr['description'] : null ) : null;
$this->descriptionAdv = isset( $attr['description_adv'] ) ? ( $attr['description_adv'] ? $attr['description_adv'] : null ) : null;
$this->options = isset( $attr['options'] ) ? ( $attr['options'] ? $attr['options'] : [] ) : [];
$this->option = isset( $attr['option'] ) ? ( $attr['option'] ? $attr['option'] : null ) : null;
$this->optionLabel = isset( $attr['optionLabel'] ) ? ( $attr['optionLabel'] ? $attr['optionLabel'] : null ) : null;
$this->attr = isset( $attr['attr'] ) ? ( $attr['attr'] ? $attr['attr'] : null ) : null;
$this->alignment = isset( $attr['alignment'] ) ? ( $attr['alignment'] ? $attr['alignment'] : null ) : null;
$this->blank = ! empty( $attr['blank'] ) ? $attr['blank'] : null;
}
public function Field( $key, $attr = [] ) {
$this->setArgument( $key, $attr );
$holderId = $this->name . '_holder';
if ( ! rtTPG()->hasPro() ) {
$class = $this->holderClass;
} else {
$class = str_replace( 'pro-field', '', $this->holderClass );
}
$html = null;
$html .= '
';
$holderClass = explode( ' ', $this->holderClass );
if ( $this->label ) {
$html .= "
";
$html .= '';
if ( in_array( 'pro-field', $holderClass, true ) && ! rtTPG()->hasPro() ) {
$html .= '[Pro]' . esc_html__( 'Premium Option', 'the-post-grid' ) . '';
}
$html .= '
';
}
$html .= "
";
switch ( $this->type ) {
case 'text':
$html .= $this->text();
break;
case 'url':
$html .= $this->url();
break;
case 'number':
$html .= $this->number();
break;
case 'select':
$html .= $this->select();
break;
case 'textarea':
$html .= $this->textArea();
break;
case 'checkbox':
$html .= $this->checkbox();
break;
case 'switch':
$html .= $this->switchField();
break;
case 'checkboxFilter':
$html .= $this->checkboxFilter();
break;
case 'radio':
$html .= $this->radioField();
break;
case 'radio-image':
$html .= $this->radioImage();
break;
case 'date_range':
$html .= $this->dateRange();
break;
case 'script':
$html .= $this->script();
break;
case 'image':
$html .= $this->image();
break;
case 'image_size':
$html .= $this->imageSize();
break;
}
if ( $this->description ) {
$html .= '
' . Fns::htmlKses( $this->description, 'basic' ) . '
';
}
if ( $this->descriptionAdv ) {
$html .= '
' . Fns::htmlKses( $this->descriptionAdv, 'advanced' ) . '
';
}
$html .= '
'; // field.
$html .= '
'; // field holder.
return $html;
}
private function text() {
$holderClass = explode( ' ', $this->holderClass );
$h = null;
$h .= 'attr, 'basic' ) . '
/>';
return $h;
}
private function script() {
$type = 'script';
if ( $this->id == 'custom-css' ) {
$type = 'css';
}
$h = null;
$h .= '';
$h .= '
';
$h .= "
";
$h .= '
';
$h .= '
';
$h .= '
';
$h .= '
';
return $h;
}
private function url() {
$h = null;
$h .= 'attr, 'basic' ) . '
/>';
return $h;
}
private function number() {
$holderClass = explode( ' ', $this->holderClass );
$h = null;
$h .= 'attr, 'basic' ) . '
/>';
return $h;
}
private function select() {
$holderClass = explode( ' ', $this->holderClass );
$atts = ( in_array( 'pro-field', $holderClass, true ) ) && ! rtTPG()->hasPro() ? 'disabled="true"' : '';
$h = null;
if ( $this->multiple ) {
$this->attr = " style='min-width:160px;'";
$this->name = $this->name . '[]';
$this->attr = $this->attr . " multiple='multiple'";
$this->value = ( is_array( $this->value ) && ! empty( $this->value ) ? $this->value : [] );
} else {
$this->value = [ $this->value ];
}
$h .= '';
return $h;
}
private function textArea() {
$holderClass = explode( ' ', $this->holderClass );
$h = null;
$h .= '';
return $h;
}
private function image() {
$holderClass = explode( ' ', $this->holderClass );
$h = null;
$img = null;
$h .= "";
$h .= '
';
$c = 'hidden';
if ( $id = absint( $this->value ) ) {
$aImg = wp_get_attachment_image_src( $id, 'thumbnail' );
$img = '
 . ')
';
$c = null;
}
$h .= '
' . Fns::htmlKses( $img, 'image' ) . '
';
$h .= '
';
return $h;
}
private function imageSize() {
$width = ( ! empty( $this->value[0] ) ? $this->value[0] : null );
$height = ( ! empty( $this->value[1] ) ? $this->value[1] : null );
$cropV = ( ! empty( $this->value[2] ) ? $this->value[2] : 'soft' );
$h = null;
$h .= "";
$h .= "
";
$h .= '';
$h .= '';
$h .= '
';
$h .= "
";
$h .= '';
$h .= '';
$h .= '
';
$h .= "
";
$h .= '';
$h .= '';
$h .= '
';
$h .= '
';
return $h;
}
private function checkbox() {
$holderClass = explode( ' ', $this->holderClass );
$this->alignment .= ( in_array( 'pro-field', $holderClass, true ) ) && ! rtTPG()->hasPro() ? ' disabled' : '';
$h = null;
if ( $this->multiple ) {
$this->name = $this->name . '[]';
$this->value = ( is_array( $this->value ) && ! empty( $this->value ) ? $this->value : [] );
}
if ( $this->multiple ) {
$h .= '';
if ( is_array( $this->options ) && ! empty( $this->options ) ) {
foreach ( $this->options as $key => $value ) {
$checked = ( in_array( $key, $this->value ) ? 'checked' : null );
$h .= '';
}
}
$h .= '
';
} else {
$checked = ( $this->value ? 'checked' : null );
$h .= '';
}
return $h;
}
private function switchField() {
$h = null;
$checked = $this->value ? 'checked' : null;
$h .= '';
return $h;
}
private function checkboxFilter() {
global $post;
$pt = get_post_meta( $post->ID, 'tpg_post_type', true );
$advFilters = Options::rtTPAdvanceFilters();
$holderClass = explode( ' ', $this->holderClass );
$h = null;
if ( $this->multiple ) {
$this->name = $this->name . '[]';
$this->value = ( is_array( $this->value ) && ! empty( $this->value ) ? $this->value : [] );
}
if ( $this->multiple ) {
$h .= '';
if ( is_array( $this->options ) && ! empty( $this->options ) ) {
foreach ( $this->options as $key => $value ) {
$checked = ( in_array( $key, $this->value ) ? 'checked' : null );
$h .= '
';
$h .= '
';
if ( $key == 'tpg_taxonomy' ) {
$h .= "
";
if ( isset( $pt ) && $pt ) {
$taxonomies = Fns::rt_get_all_taxonomy_by_post_type( $pt );
$taxA = get_post_meta( $post->ID, 'tpg_taxonomy' );
$post_filter = get_post_meta( $post->ID, 'post_filter' );
$h .= "
";
if ( is_array( $post_filter ) && ! empty( $post_filter ) && in_array( 'tpg_taxonomy', $post_filter ) && ! empty( $taxonomies ) ) {
$h .= Fns::rtFieldGenerator(
[
'tpg_taxonomy' => [
'type' => 'checkbox',
'label' => '',
'id' => 'post-taxonomy',
'multiple' => true,
'options' => $taxonomies,
],
]
);
} else {
$h .= '
' . esc_html__( 'No Taxonomy found', 'the-post-grid' ) . '
';
}
$h .= '
';
$h .= "
";
$h .= '
';
$h .= '
Terms
';
$h .= '
';
if ( is_array( $taxA ) && ! empty( $taxA ) ) {
foreach ( $taxA as $tax ) {
$h .= '
';
$h .= Fns::rtFieldGenerator(
[
'term_' . $tax => [
'type' => 'select',
'label' => ucfirst( str_replace( '_', ' ', $tax ) ),
'class' => 'rt-select2 full',
'holderClass' => "term-filter-item {$tax}",
'value' => get_post_meta( $post->ID, 'term_' . $tax ),
'multiple' => true,
'options' => Fns::rt_get_all_term_by_taxonomy( $tax ),
],
]
);
$h .= Fns::rtFieldGenerator(
[
'term_operator_' . $tax => [
'type' => 'select',
'label' => esc_html__( 'Operator', 'the-post-grid' ),
'class' => 'rt-select2 full',
'holderClass' => "term-filter-item-operator {$tax}",
'value' => get_post_meta( $post->ID, 'term_operator_' . $tax, true ),
'options' => Options::rtTermOperators(),
],
]
);
$h .= '
';
}
}
$h .= '
';
$h .= '
';
$h .= Fns::rtFieldGenerator(
[
'taxonomy_relation' => [
'type' => 'select',
'label' => esc_html__( 'Relation', 'the-post-grid' ),
'class' => 'rt-select2',
'holderClass' => 'term-filter-item-relation ' . ( count( $taxA ) > 1 ? null : 'hidden' ),
'value' => get_post_meta( $post->ID, 'taxonomy_relation', true ),
'options' => Options::rtTermRelations(),
],
]
);
$h .= '
';
} else {
$h .= "
";
$h .= '
';
$h .= "
";
$h .= '
';
$h .= '
Terms
';
$h .= '
';
$h .= '
';
$h .= '
';
$h .= '
';
$h .= Fns::rtFieldGenerator(
[
'taxonomy_relation' => [
'type' => 'select',
'label' => esc_html__( 'Relation', 'the-post-grid' ),
'class' => 'rt-select2',
'holderClass' => 'term-filter-item-relation tpg-hidden',
'default' => 'OR',
'options' => Options::rtTermRelations(),
],
]
);
}
$h .= '
';
} elseif ( $key == 'order' ) {
$h .= '
';
$h .= "
";
$h .= "
";
$h .= "
";
$h .= Fns::rtFieldGenerator(
[
'order_by' => [
'type' => 'select',
'label' => esc_html__( 'Order by', 'the-post-grid' ),
'class' => 'rt-select2 filter-item',
'value' => get_post_meta( $post->ID, 'order_by', true ),
'options' => Options::rtPostOrderBy( false, true ),
'description' => esc_html__( 'If "Meta value", "Meta value Number" or "Meta value datetime" is chosen then meta key is required.', 'the-post-grid' ),
],
]
);
$h .= Fns::rtFieldGenerator(
[
'tpg_meta_key' => [
'type' => 'text',
'label' => esc_html__( 'Meta key', 'the-post-grid' ),
'class' => 'rt-select2 filter-item',
'holderClass' => 'tpg-hidden',
'value' => get_post_meta( $post->ID, 'tpg_meta_key', true ),
],
]
);
$h .= Fns::rtFieldGenerator(
[
'order' => [
'type' => 'radio',
'label' => esc_html__( 'Order', 'the-post-grid' ),
'class' => 'rt-select2 filter-item',
'alignment' => 'vertical',
'default' => 'DESC',
'value' => get_post_meta( $post->ID, 'order', true ),
'options' => Options::rtPostOrders(),
],
]
);
$h .= '
';
$h .= '
';
$h .= '
';
$h .= '
';
} elseif ( $key == 'author' ) {
$h .= '
';
$h .= "
";
$h .= Fns::rtFieldGenerator(
[
$key => [
'type' => 'select',
'label' => '',
'class' => 'rt-select2 filter-item full',
'value' => get_post_meta( $post->ID, $key ),
'multiple' => true,
'options' => Fns::rt_get_users(),
],
]
);
$h .= '
';
$h .= '
';
} elseif ( $key == 'tpg_post_status' ) {
$h .= '
';
$h .= "
";
$h .= Fns::rtFieldGenerator(
[
$key => [
'type' => 'select',
'label' => '',
'class' => 'rt-select2 filter-item full',
'default' => [ 'publish' ],
'value' => get_post_meta( $post->ID, $key ),
'multiple' => true,
'options' => Options::rtTPGPostStatus(),
],
]
);
$h .= '
';
$h .= '
';
} elseif ( $key == 's' ) {
$h .= '
';
$h .= "
";
$h .= Fns::rtFieldGenerator(
[
$key => [
'type' => 'text',
'label' => esc_html__( 'Keyword', 'the-post-grid' ),
'class' => 'filter-item full',
'value' => get_post_meta( $post->ID, $key, true ),
],
]
);
$h .= '
';
$h .= '
';
} elseif ( $key == 'date_range' ) {
$range_start = get_post_meta( $post->ID, 'date_range_start', true );
$range_end = get_post_meta( $post->ID, 'date_range_end', true );
$range_value = [
'start' => $range_start,
'end' => $range_end,
];
$h .= '
';
$h .= "
";
$h .= Fns::rtFieldGenerator(
[
$key => [
'type' => 'date_range',
'label' => '',
'class' => 'filter-item full rt-date-range',
'value' => $range_value,
'description' => "Date format should be 'yyyy-mm-dd'",
],
]
);
$h .= '
';
$h .= '
';
}
// }
$h .= '
';
}
}
$h .= '
';
} else {
$checked = ( $this->value ? 'checked' : null );
$h .= '';
}
return $h;
}
private function radioField() {
$holderClass = explode( ' ', $this->holderClass );
$this->alignment .= ( in_array( 'pro-field', $holderClass, true ) ) && ! rtTPG()->hasPro() ? ' disabled' : '';
$h = null;
$h .= '';
if ( is_array( $this->options ) && ! empty( $this->options ) ) {
foreach ( $this->options as $key => $value ) {
$checked = ( $key == $this->value ? 'checked' : null );
$h .= '';
}
}
$h .= '
';
return $h;
}
/**
* Radio Image
*
* @return String
*/
private function radioImage() {
$h = null;
$id = 'rttpg-' . $this->name;
$h .= sprintf( "", esc_attr( $this->alignment ), esc_attr( $id ) );
$selected_value = $this->value;
if ( is_array( $this->options ) && ! empty( $this->options ) ) {
foreach ( $this->options as $key => $value ) {
$checked = ( $key == $selected_value ? 'checked' : null );
$title = isset( $value['title'] ) && $value['title'] ? $value['title'] : '';
$link = isset( $value['layout_link'] ) && $value['layout_link'] ? $value['layout_link'] : '';
$linkHtml = empty( $link ) ? esc_html( $title ) : '
' . esc_html( $title ) . '';
$layout = isset( $value['layout'] ) ? $value['layout'] : '';
$taghtml = isset( $value['tag'] ) ? '
' . $value['tag'] . '
' : '';
$h .= sprintf(
'
%8$s
',
'',
esc_attr( $key ),
esc_attr( $checked ),
esc_attr( $this->name ),
esc_url( $value['img'] ),
esc_attr( $title ),
esc_attr( $layout ),
Fns::htmlKses( $linkHtml, 'basic' ),
Fns::htmlKses( $taghtml, 'basic' )
);
}
}
$h .= '
';
return $h;
}
private function dateRange() {
$h = null;
$this->name = ( $this->name ? $this->name : 'date-range-' . rand( 0, 1000 ) );
$h .= '';
return $h;
}
}