wpaf_fields = WPAF_Meta_Boxes_Fields_Pro::getFields();
}
/**
* Hooks into WordPress' add_meta_boxes function.
*
* @since 1.0
*/
public function add_meta_boxes() {
foreach ( $this->post_types as $post_type ) {
add_meta_box(
'coupon-details',
__( 'Coupon Settings & Details', 'affcoupon' ),
array( $this, 'add_meta_box_callback' ),
$post_type,
'normal',
'core'
);
}
}
/**
* Generating the HTML for the meta box.
*
* @param object $post WordPress post object.
*
* @since 1.0
*/
public function add_meta_box_callback( $post ) {
wp_nonce_field( 'coupon_details_data', 'coupon_details_nonce' );
$this->generate_wpaf_fields( $post );
}
/**
* Generating the field's HTML for the meta box.
*
* @param array $post post data.
*
* @since 1.0
*/
public function generate_wpaf_fields( $post ) {
$expire_date_format = get_option( 'wpaf_expiry-date-format' );
if ( empty( $expire_date_format ) ) {
$expire_date_format = 'dd-mm-yy';
}
$expireDateFormatFun = wpaf_getExpireDateFormatFun( $expire_date_format );
$output = '';
$help = '';
foreach ( $this->wpaf_fields as $wpaf_field ) {
$tr_class = '';
if ( ! empty( $wpaf_field['tr_class'] ) ) {
$tr_class = $wpaf_field['tr_class'];
}
$type = $wpaf_field['type'];
$label = '
';
$wpaf_coupon_meta_key = 'coupon_details_' . $wpaf_field['id'];
if( $wpaf_field['id'] == 'wpaf_description' ) $wpaf_coupon_meta_key = 'coupon_details_description';
$db_value = get_post_meta( $post->ID, $wpaf_coupon_meta_key, true );
if ( $wpaf_field['type'] == 'expiredate' || $wpaf_field['type'] == 'temp4-expiredate' ) {
if ( ! empty( $db_value ) && ( (string)(int)$db_value ) == $db_value ) {
$db_value = gmdate( $expireDateFormatFun, $db_value );
} elseif ( ! empty( $db_value ) ) {
$db_value = gmdate( $expireDateFormatFun, strtotime( $db_value ) );
}
}
switch ( $wpaf_field['type'] ) {
case 'dealtext':
$input = sprintf(
'',
esc_attr( $wpaf_field['id'] ),
esc_attr( $wpaf_field['id'] ),
esc_attr( $db_value )
);
break;
case 'temp4-dealtext':
$input = sprintf(
'',
esc_attr( $wpaf_field['id'] ),
esc_attr( $wpaf_field['id'] ),
esc_attr( $db_value )
);
break;
case 'buttontext':
$input = sprintf(
'',
esc_attr( $wpaf_field['id'] ),
esc_attr( $wpaf_field['id'] ),
esc_attr( $db_value )
);
break;
case 'temp4-buttontext':
$input = sprintf(
'',
esc_attr( $wpaf_field['id'] ),
esc_attr( $wpaf_field['id'] ),
esc_attr( $db_value )
);
break;
case 'temp4-text':
$input = sprintf(
'',
$wpaf_field['type'] !== 'color' ? 'class="regular-text"' : '',
esc_attr( $wpaf_field['id'] ),
esc_attr( $wpaf_field['id'] ),
'text',
esc_attr( $db_value )
);
break;
case 'date':
$input = sprintf(
'',
esc_attr( $wpaf_field['type'] ),
esc_attr( $wpaf_field['id'] ),
esc_attr( $wpaf_field['id'] ),
esc_attr( $db_value )
);
break;
case 'select':
$input = sprintf(
'';
break;
case 'textarea':
if ( $wpaf_field['id'] == 'wpaf_description' ):
ob_start();
/**
* Add Editor to description field
*
* @since 1.0.2
*/
$settings = array(
'wpautop' => false,
'media_buttons' => false,
'tinymce' => true,
'editor_height' => 150,
'teeny' => false,
'quicktags' => false,
'textarea_name' => "wpaf_description"
);
wp_editor( $db_value, 'wpaf_description' ,$settings);
$input = ob_get_clean();
else:
$input = sprintf(
'',
esc_attr( $wpaf_field['id'] ),
esc_attr( $wpaf_field['id'] ),
esc_textarea( $db_value )
);
endif;
break;
case 'expirationcheck':
$input = sprintf(
'',
esc_attr( $wpaf_field['id'] ),
esc_attr( $wpaf_field['id'] ),
esc_attr( $db_value )
);
break;
case 'neverexpire-checkbox':
$input = sprintf(
'',
esc_attr( $wpaf_field['id'] ),
esc_attr( $wpaf_field['id'] )
);
break;
case 'expiredate':
$input = sprintf(
'',
esc_attr( $expire_date_format ),
esc_attr( $wpaf_field['id'] ),
esc_attr( $wpaf_field['id'] ),
esc_attr( $expire_date_format ),
esc_attr( $db_value )
);
break;
case 'temp4-expiredate':
$input = sprintf(
'',
esc_attr( $expire_date_format ),
esc_attr( $wpaf_field['id'] ),
esc_attr( $wpaf_field['id'] ),
esc_attr( $expire_date_format ),
esc_attr( $db_value )
);
break;
case 'expiretime':
$input = sprintf(
'',
esc_attr( $wpaf_field['id'] ),
esc_attr( $wpaf_field['id'] ),
esc_attr( $db_value )
);
break;
case 'coupon-image-row' :
$id = $wpaf_field['id'];
$id_attr = esc_attr( $id );
$input = '';
//Get upload url
$upload_link = esc_url( get_upload_iframe_src( 'image', $db_value ) );
// Get the image src
$img_src = wp_get_attachment_image_src( $db_value, 'full' );
$you_have_img = is_array( $img_src );
//img container
$input .= '
';
if ( $you_have_img ) {
$input .= '
 . ')
';
}
$input .= '
';
//add image or remove
$input .= '';
//hidden input
$input .= '
';
break;
case 'colorpicker':
$value = empty( $db_value ) ? $wpaf_field['default'] : $db_value;
$value = sanitize_hex_color( $value ) ? sanitize_hex_color( $value ) : '';
$input = '';
break;
default:
$input = sprintf(
'',
$wpaf_field['type'] !== 'color' ? 'class="regular-text"' : '',
esc_attr( $wpaf_field['id'] ),
esc_attr( $wpaf_field['id'] ),
esc_attr( $wpaf_field['type'] ),
esc_attr( $db_value ),
( empty( $wpaf_field['placeholder'] ) ? '' : esc_attr( $wpaf_field['placeholder'] ) )
);
}
$output .= $this->row_format( $type, $label, $input, $tr_class );
}
echo '';
WPAF_Assets::wpaf_print_inline_script(
"jQuery('#expire-time').timepicker({\n"
. " controlType: 'select',\n"
. " oneLine: true,\n"
. " timeFormat: 'h:m tt',\n"
. " showButtonPanel: false\n"
. "});"
);
}
/**
* Generates the HTML for table rows.
*
* @since 1.0
*/
public function row_format( $type, $label, $input, $tr_class = '' ) {
return sprintf(
'| %s | %s |
',
esc_attr( $type ),
esc_attr( $tr_class ),
$label,
$input
);
}
/**
* Sanitize coupon meta values before storage.
*
* @param mixed $value Submitted value.
* @param array $field Field definition.
*
* @return mixed
*/
private function sanitize_meta_field_value( $value, $field ) {
if ( is_array( $value ) ) {
return array_map( 'sanitize_text_field', $value );
}
if ( in_array( $field['id'], array( 'expire-date', 'second-expire-date', 'third-expire-date' ), true ) ) {
return strtotime( sanitize_text_field( $value ) );
}
if ( 'wpaf_description' === $field['id'] ) {
return wp_kses_post( $value );
}
switch ( $field['type'] ) {
case 'email':
return sanitize_email( $value );
case 'text':
if ( 'link' === $field['id'] ) {
return esc_url_raw( $value );
}
return sanitize_text_field( $value );
case 'textarea':
return sanitize_textarea_field( $value );
case 'color':
case 'colorpicker':
return WPAF_Assets::wpaf_sanitize_css_color( $value );
case 'coupon-image-row':
return absint( $value );
default:
return sanitize_text_field( $value );
}
}
/**
* Hooks into WordPress' save_post function.
*
* @since 1.0
*/
public function save_post( $post_id ) {
if ( ! in_array( get_post_type( $post_id ), $this->post_types, true ) ) {
return $post_id;
}
if ( wp_is_post_revision( $post_id ) ) {
return $post_id;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
if ( ! isset( $_POST['coupon_details_nonce'] ) ) {
return $post_id;
}
$nonce = sanitize_text_field( wp_unslash( $_POST['coupon_details_nonce'] ) );
if ( ! wp_verify_nonce( $nonce, 'coupon_details_data' ) ) {
return $post_id;
}
foreach ( $this->wpaf_fields as $wpaf_field ) {
if ( isset( $_POST[ $wpaf_field['id'] ] ) ) {
// Wrap each $_POST read directly in a context-appropriate core sanitizer
// so static analysis sees sanitization in the same statement;
// sanitize_meta_field_value() then applies the precise per-field rule.
// map_deep( ..., 'sanitize_text_field' ) covers both scalar and array
// (multi-value) fields.
if ( 'wpaf_description' === $wpaf_field['id'] ) {
$wpaf_raw_value = wp_kses_post( wp_unslash( $_POST[ $wpaf_field['id'] ] ) );
} elseif ( 'textarea' === $wpaf_field['type'] ) {
$wpaf_raw_value = sanitize_textarea_field( wp_unslash( $_POST[ $wpaf_field['id'] ] ) );
} else {
$wpaf_raw_value = map_deep( wp_unslash( $_POST[ $wpaf_field['id'] ] ), 'sanitize_text_field' );
}
$field_value = $this->sanitize_meta_field_value( $wpaf_raw_value, $wpaf_field );
$wpaf_coupon_meta_key = 'coupon_details_' . $wpaf_field['id'];
if( $wpaf_field['id'] == 'wpaf_description' ) $wpaf_coupon_meta_key = 'coupon_details_description';
update_post_meta( $post_id, $wpaf_coupon_meta_key, $field_value );
} else if ( $wpaf_field['type'] === 'checkbox' ||
strpos($wpaf_field['type'],'checkbox')) {
update_post_meta( $post_id, 'coupon_details_' . $wpaf_field['id'], '0' );
}
}
}
}