options = affcoups_get_options();
if ( is_numeric( $post ) )
$post = get_post( $post );
// Setup coupon
if ( $post instanceof WP_Post ) {
$this->post = $post;
$this->id = $post->ID;
}
// Setup vendor
$vendor_id = get_post_meta( $post->ID, AFFCOUPS_PREFIX . 'coupon_vendor', true );
if ( ! empty( $vendor_id ) ) {
$this->vendor_id = $vendor_id;
$this->vendor = new Affcoups_Vendor( $vendor_id );
}
}
/**
* Get coupon id
*
* @return int
*/
public function get_id() {
return $this->id;
}
/**
* Output coupon classes
*
* @param $classes
*/
public function the_classes( $classes ) {
$add_classes = apply_filters( 'affcoups_coupon_add_classes', array(), $this );
// Maybe add extra classes
if ( sizeof( $add_classes ) > 0 ) {
foreach ( $add_classes as $class ) {
$classes .= ' affcoups-coupon--' . $class;
}
}
// Output
echo $classes;
}
/**
* Output container attributes
*
* @param bool $echo
* @return string
*/
public function the_container( $echo = true ) {
$output = '';
$attributes = array();
// HTML ID
$output .= ' id="affcoups-coupon-' . $this->id . '"';
// Coupon ID
$attributes['coupon-id'] = $this->id;
// Coupon Title
$attributes['coupon-title'] = $this->get_post_title();
// Add more via filter
$attributes = apply_filters( 'affcoups_coupon_container_attributes', $attributes, $this );
if ( sizeof( $attributes ) != 0 ) {
foreach ( $attributes as $key => $value ) {
$value = strip_tags( $value );
$value = str_replace('"', "'", $value );
// Add attribute to output
if ( ! empty ( $value ) )
$output .= ' data-affcoups-' . $key . '="' . $value . '"';
}
}
if ( ! $echo )
return $output;
if ( ! empty ( $output ) )
echo $output;
}
/**
* Get coupon image
*
* @param null $size
*
* @return bool|mixed
*/
function get_image( $size = null ) {
// Get thumbnail from coupon
$image_size = ( 'small' === $size ) ? 'affcoups-thumb-small' : 'affcoups-thumb';
if ( has_post_thumbnail( $this->id ) ) {
$coupon_thumbnail_id = get_post_thumbnail_id( $this->id );
$coupon_image_alt = get_post_meta( $coupon_thumbnail_id, '_wp_attachment_image_alt', true );
$coupon_image_url = get_the_post_thumbnail_url( $this->id, $image_size );
$coupon_feature_image = array(
'url' => $coupon_image_url,
'alt' => $coupon_image_alt
);
return $coupon_feature_image;
} else {
$coupon_images = rwmb_meta( AFFCOUPS_PREFIX . 'coupon_image', 'type=image&size=' . $image_size, $this->id );
if ( ! empty ( $coupon_images ) && is_array( $coupon_images ) ) {
return array_shift( $coupon_images );
// Get thumbnail from vendor
} else {
$vendor_image = ( $this->vendor ) ? $this->vendor->get_image( $size ) : null;
if ( ! empty( $vendor_image ) )
return $vendor_image;
}
}
// No image found
return null;
}
/**
* Display the coupon image
*/
function the_image() {
// since ver 1.1.12 do not display image
if ( affcoups_is_pro_version() )
if($this->hide_image())
return;
$image = $this->get_image();
$coupon_url = $this->get_url();
$coupon_title = $this->get_title();
$coupon_title = affcoups_cleanup_html_attribute( $coupon_title );
$image_args = array(
'src' => ( ! empty ( $image['url'] ) ) ? $image['url'] : AFFCOUPS_PLUGIN_URL . 'assets/img/placeholder-thumb.png',
'alt' => ( ! empty ( $image['alt'] ) ) ? $image['alt'] : $this->get_title(),
'url' => ( ! empty( $coupon_url ) ) ? $coupon_url : '',
'title' => ( ! empty( $coupon_title ) ) ? $coupon_title : '',
'target' => '_blank',
'rel' => 'nofollow'
);
$image_args = apply_filters( 'affcoups_the_image_args', $image_args, $this );
// Build image
$image_html = '';
// Build thumbnail
if ( ! empty( $image_args['url'] ) ) {
$image_output = '';
$image_output .= $image_html;
$image_output .= '';
} else {
$image_output = '';
$image_output .= $image_html;
$image_output .= '';
}
// Output
echo $image_output ;
}
/**
* Get coupon title
*
* @return mixed|string
*/
function get_title() {
// Coupon title
$title = get_post_meta( $this->id, AFFCOUPS_PREFIX . 'coupon_title', true );
if ( ! empty ( $title ) )
return $title;
// Vendor title
$vendor_title = ( $this->vendor ) ? $this->vendor->get_title() : null;
if ( ! empty( $vendor_title ) )
return $vendor_title;
// Fallback: Coupon post title
return get_the_title( $this->id );
}
/**
* Get coupon post title
*/
function get_post_title() {
return get_the_title( $this->id );
}
/**
* Get coupon url
*
* @return bool|mixed
*/
function get_url() {
// Coupon url
$url = get_post_meta( $this->id, AFFCOUPS_PREFIX . 'coupon_url', true );
// Vendor url
if ( empty( $url ) ) {
$vendor_url = ( $this->vendor ) ? $this->vendor->get_url() : null;
if ( ! empty ( $vendor_url ) )
$url = $vendor_url;
}
$url = apply_filters( 'affcoups_coupon_url', $url, $this );
return $url;
}
/**
* Get coupon description
*
* @return string
*/
function get_description() {
// Coupon description
$description = get_post_meta( $this->id, AFFCOUPS_PREFIX . 'coupon_description', true );
if ( ! empty ( $description ) )
return $description;
// Vendor description
$vendor_description = get_post_meta( $this->vendor_id, AFFCOUPS_PREFIX . 'vendor_description', true );
if ( ! empty( $vendor_description ) )
return $vendor_description;
// Fallback: Coupon post content
if ( ! isset( $this->post->post_content ) )
return null;
$post_content = apply_filters( 'affcoups_the_content', $this->post->post_content );
$post_content = str_replace( ']]>', ']]>', $post_content );
return $post_content;
}
/**
* Get coupon excerpt
*
* @return mixed
*/
function get_excerpt() {
$description = $this->get_description();
$description = trim( $description );
$excerpt_length = affcoups_get_option( 'excerpt_length', 90 );
$excerpt = affcoups_truncate_string( $description, $excerpt_length );
return $excerpt;
}
/**
* Output the coupon excerpt
*/
function the_excerpt() {
$description = $this->get_description();
$excerpt = $this->get_excerpt();
echo $excerpt;
if ( $excerpt != $description ) {
echo '' . __( 'Show More', 'affiliate-coupons' ) . '';
}
}
/**
* Get coupon types
*
* @return array|bool|WP_Error
*/
function get_types() {
$term_list = wp_get_post_terms( $this->id, 'affcoups_coupon_type', array( "fields" => "all" ) );
if ( sizeof( $term_list ) > 0 ) {
return $term_list;
}
return null;
}
/**
* Display coupon types
*/
function the_types() {
$types = '';
$term_list = $this->get_types();
if ( is_array( $term_list ) && sizeof( $term_list ) > 0 ) {
foreach ( $term_list as $term_single ) {
echo '';
echo esc_attr( $term_single->name );
echo '';
}
}
echo esc_attr( $types );
}
/**
* Get coupon discount
*
* @return bool|mixed
*/
function get_discount() {
$discount = get_post_meta( $this->id, AFFCOUPS_PREFIX . 'coupon_discount', true );
return ( ! empty ( $discount ) ) ? $discount : null;
}
/**
* Check whether valid dates should be shown or not
*/
function show_valid_dates() {
if ( ! $this->has_valid_dates() )
return false;
// Check shortcode atts
global $affcoups_template_args;
if ( isset( $affcoups_template_args['hide_dates'] ) ) {
if ( 'true' == $affcoups_template_args['hide_dates'] )
return false;
if ( 'false' == $affcoups_template_args['hide_dates'] )
return true;
}
// Check settings
if ( isset( $this->options['hide_dates'] ) && '1' == $this->options['hide_dates'] )
return false;
return true;
}
/**
* Check whether expiry countdown should be shown or not
*/
function show_expiry_countdown() {
if ($this->is_expiry_countdown_enable() )
return true;
// Check shortcode atts
global $affcoups_template_args;
if ( isset( $affcoups_template_args['show_expiry_countdown'] ) ) {
if ( 'true' == $affcoups_template_args['show_expiry_countdown'] )
return true;
if ( 'false' == $affcoups_template_args['show_expiry_countdown'] )
return false;
}
// Check settings
if ( isset( $this->options['show_expiry_countdown'] ) && '1' == $this->options['show_expiry_countdown'] )
return true;
return false;
}
/**
* Check if coupon has valid dates
*
* @return bool
*/
function has_valid_dates() {
$valid_from = get_post_meta( $this->id, AFFCOUPS_PREFIX . 'coupon_valid_from', true );
$valid_until = get_post_meta( $this->id, AFFCOUPS_PREFIX . 'coupon_valid_until', true );
return ( ! empty ( $valid_from ) || ! empty ( $valid_until ) ) ? true : false;
}
/**
* Display coupon valid dates
*/
function the_valid_dates() {
$date_format = get_option( 'date_format' );
$date_format = apply_filters( 'affcoups_coupon_validation_date_format', $date_format );
$dates = '';
$valid_from = get_post_meta( $this->id, AFFCOUPS_PREFIX . 'coupon_valid_from', true );
$valid_until = get_post_meta( $this->id, AFFCOUPS_PREFIX . 'coupon_valid_until', true );
if ( ! empty ( $valid_from ) && time() < $valid_from ) {
$dates .= __( 'Valid from', 'affiliate-coupons' ) . ' ' . date_i18n( $date_format, $valid_from );
}
if ( ! empty ( $valid_until ) ) {
$dates .= ( empty ( $dates ) ) ? __( 'Valid until', 'affiliate-coupons' ) : ' ' . __( 'until', 'affiliate-coupons' );
$dates .= ' ' . date_i18n( $date_format, $valid_until );
}
echo esc_attr( $dates );
}
/**
* Check whether image should be shown or not , since pro ver 1.1.12
*/
function hide_image() {
// Check shortcode atts
global $affcoups_template_args;
if ( isset( $affcoups_template_args['hide_image'] ) ) {
if ( 'true' == $affcoups_template_args['hide_image'] )
return true;
if ( 'false' == $affcoups_template_args['hide_image'] )
return false;
}
return false;
}
/**
* Check whether code should be shown or not
*
* @return bool
*/
function show_code() {
if ( ! $this->get_code() )
return false;
if ( isset( $this->options['code'] ) && 'hide' === $this->options['code'] )
return false;
return apply_filters( 'affcoups_show_code', true, $this );
}
/**
* Get coupon code
*
* @return bool|string
*/
function get_code() {
$enable_multi_coupon_code = get_post_meta( $this->id, AFFCOUPS_PREFIX . 'enable_multi_coupon_code', true );
if($enable_multi_coupon_code && affcoups_is_pro_version()){
$file_id = get_post_meta( $this->id, AFFCOUPS_PREFIX . 'multi_coupon_code', true );
if(empty($file_id))
return;
// Now process the CSV file: read, remove the first row, and save it
$file_path = get_attached_file($file_id);
// Check if the session is already set
// Start the session to track the user
if (session_status() == PHP_SESSION_NONE) {
// Start the session if it hasn't been started yet
session_start();
}
// Check if the session ID exists, if not, create a new one
if (!isset($_SESSION['session_id'])) {
$_SESSION['session_id'] = session_id();
}
// Open the CSV file
if (($handle = fopen($file_path, 'r+')) !== FALSE) {
$rows = [];
$first_row = null;
// Read the CSV content
while (($data = fgetcsv($handle)) !== FALSE) {
// Capture the first row
if (!$first_row) {
$first_row = $data;
$code=$first_row[0];
} else {
$rows[] = $data;
}
}
// Close the CSV file
fclose($handle);
if (!isset($_SESSION['first_row_deleted']) || $_SESSION['first_row_deleted'] !== true) {
// Reopen the file for writing (truncate)
if (($handle = fopen($file_path, 'w')) !== FALSE) {
// Write the modified data (excluding the first row)
foreach ($rows as $row) {
fputcsv($handle, $row);
}
// Close the file after writing
fclose($handle);
$_SESSION['first_row_deleted'] = true;
}
}
}
}else {
$code = get_post_meta( $this->id, AFFCOUPS_PREFIX . 'coupon_code', true );
}
return ( ! empty ( $code ) ) ? $code : NULL;
}
// since 1.3.4 , one time coupon code display , multiple coupon codes
/**
* Show coupon code
*
* @return bool|string
*/
function is_click_to_reveal_disable() {
$click_to_reveal = get_post_meta( $this->id, AFFCOUPS_PREFIX . 'click_to_reveal_disable', true );
return ( ! empty ( $click_to_reveal ) ) ? $click_to_reveal : null;
}
/**
* Check if social share is disable for a coupon
*
* Since ver 1.6.7.6 */
function is_social_share_disable() {
$social_share = get_post_meta( $this->id, AFFCOUPS_PREFIX . 'social_share_disable', true );
return ( ! empty ( $social_share ) ) ? $social_share : null;
}
function is_expiry_countdown_enable() {
$expiry_countdown = get_post_meta( $this->id, AFFCOUPS_PREFIX . 'show_expiry_countdown', true );
return ( ! empty ( $expiry_countdown ) ) ? $expiry_countdown : null;
}
/**
* Output the code
*/
function the_code() {
$this->the_clipboard();
}
/**
* Output the clipboard
*/
function the_clipboard() {
$code = $this->get_code();
if ( empty( $code ) )
return;
$icon = ( ! empty( $this->options['clipboard_icon'] ) ) ? $this->options['clipboard_icon'] : '';
$classes = 'affcoups-clipboard';
if ( ! empty( $icon ) )
$classes .= ' affcoups-clipboard--icon';
?>