'Set Expire Date',
'id' => 'adsforwp_ad_expire_enable',
'type' => 'checkbox',
),
array(
'label' => 'From',
'id' => 'adsforwp_ad_expire_from',
'type' => 'text',
),
array(
'label' => 'To',
'id' => 'adsforwp_ad_expire_to',
'type' => 'text',
),
array(
'label' => 'Set Specific Days',
'id' => 'adsforwp_ad_expire_day_enable',
'type' => 'checkbox',
),
array(
'label' => 'Days',
'id' => 'adsforwp_ad_expire_days',
'type' => 'select',
'options' => array(
'0' => 'Monday',
'1' => 'Tuesday',
'2' => 'Wednesday',
'3' => 'Thursday',
'4' => 'Friday',
'5' => 'Saturday',
'6' => 'Sunday',
),
),
array(
'label' => 'Expire by Impression Limit',
'id' => 'adsforwp_ad_expire_impression_enable',
'type' => 'checkbox',
),
array(
'label' => 'Set Impression Limit',
'id' => 'adsforwp_ad_expire_impression_limit',
'type' => 'text',
),
);
public function __construct() {
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
add_action( 'save_post', array( $this, 'save_fields' ) );
}
public function add_meta_boxes() {
foreach ( $this->screen as $single_screen ) {
add_meta_box(
'setexpiredate',
esc_html__( 'Set Expire Date', 'ads-for-wp' ),
array( $this, 'meta_box_callback' ),
$single_screen,
'side',
'high'
);
}
}
public function meta_box_callback( $post ) {
wp_nonce_field( 'setexpiredate_data', 'setexpiredate_nonce' );
$this->field_generator( $post );
}
public function field_generator( $post ) {
$output = '';
foreach ( $this->meta_fields as $meta_field ) {
$label = '';
$meta_value = get_post_meta( $post->ID, $meta_field['id'], true );
if ( empty( $meta_value ) ) {
$meta_value = isset( $meta_field['default'] );
}
switch ( $meta_field['type'] ) {
case 'text':
$input = sprintf(
'',
$meta_field['type'] !== 'color' ? 'style="width: 100%; background:#fff"' : '',
esc_attr( $meta_field['id'] ),
esc_attr( $meta_field['id'] ),
esc_attr( $meta_field['type'] ),
esc_attr( $meta_value )
);
break;
case 'checkbox':
$input = sprintf(
'',
$meta_value === '1' ? 'checked' : '',
esc_attr( $meta_field['id'] ),
esc_attr( $meta_field['id'] )
);
break;
case 'select':
switch ( $meta_field['id'] ) {
case 'adsforwp_ad_expire_time':
$input = sprintf(
'';
break;
case 'adsforwp_ad_expire_days':
$input = sprintf(
'';
break;
default:
$input = sprintf(
'';
break;
}
break;
default:
$input = sprintf(
'',
$meta_field['type'] !== 'color' ? 'style="width: 100%; background:#fff"' : '',
esc_attr( $meta_field['id'] ),
esc_attr( $meta_field['id'] ),
esc_attr( $meta_field['type'] ),
esc_attr( $meta_value ),
esc_attr( $meta_field['id'] )
);
}
$output .= $this->format_rows( $label, $input );
}
$common_function_obj = new Adsforwp_Admin_Common_Functions();
$allowed_html = $common_function_obj->adsforwp_expanded_allowed_tags();
echo '';
}
public function format_rows( $label, $input ) {
return '| ' . $label . ' | ' . $input . ' |
';
}
public function save_fields( $post_id ) {
if ( ! isset( $_POST['setexpiredate_nonce'] ) ) {
return $post_id;
}
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason Validating nonce so sanitization not needed
if ( ! wp_verify_nonce( $_POST['setexpiredate_nonce'], 'setexpiredate_data' ) ) {
return $post_id;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
if ( current_user_can( 'manage_options' ) ) {
$post_meta = array();
$post_meta = $_POST; // Sanitized below before saving
$adsforwp_days_array = array();
if ( isset( $_POST['adsforwp_ad_expire_days'] ) && is_array( $_POST['adsforwp_ad_expire_days'] ) ) {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash
$adsforwp_days_array = array_map( 'sanitize_text_field', $_POST['adsforwp_ad_expire_days'] );
}
update_post_meta( $post_id, 'adsforwp_ad_expire_days', $adsforwp_days_array );
foreach ( $this->meta_fields as $meta_field ) {
if ( $meta_field['id'] != 'adsforwp_ad_expire_days' ) {
if ( isset( $post_meta[ $meta_field['id'] ] ) ) {
switch ( $meta_field['type'] ) {
case 'email':
$post_meta[ $meta_field['id'] ] = sanitize_email( $post_meta[ $meta_field['id'] ] );
break;
case 'text':
$post_meta[ $meta_field['id'] ] = sanitize_text_field( $post_meta[ $meta_field['id'] ] );
break;
default:
$post_meta[ $meta_field['id'] ] = sanitize_text_field( $post_meta[ $meta_field['id'] ] );
}
update_post_meta( $post_id, $meta_field['id'], $post_meta[ $meta_field['id'] ] );
} elseif ( $meta_field['type'] === 'checkbox' ) {
update_post_meta( $post_id, $meta_field['id'], '0' );
}
}
}
}
}
}
if ( class_exists( 'Adsforwp_View_Expiredate' ) ) {
new Adsforwp_View_Expiredate();
}