| 1 |
<?php |
| 2 |
namespace YayMail\Page\Source; |
| 3 |
|
| 4 |
use YayMail\Helper\Helper; |
| 5 |
|
| 6 |
defined( 'ABSPATH' ) || exit; |
| 7 |
/** |
| 8 |
* Plugin activate/deactivate logic |
| 9 |
*/ |
| 10 |
class CustomPostType { |
| 11 |
|
| 12 |
public static function checkEmailTemplateExist( $args = false, $order = null ) { |
| 13 |
if ( false != $args ) { |
| 14 |
if ( ! isset( $GLOBALS[ 'yaymail_template_' . $args['email_template'] ] ) ) { |
| 15 |
$default = array( |
| 16 |
'post_type' => 'yaymail_template', |
| 17 |
'post_status' => array( 'publish', 'pending', 'future' ), |
| 18 |
'posts_per_page' => '1', |
| 19 |
'meta_query' => array( |
| 20 |
'relation' => 'AND', |
| 21 |
array( |
| 22 |
'key' => '_yaymail_template', |
| 23 |
'value' => $args['email_template'], |
| 24 |
'compare' => '=', |
| 25 |
), |
| 26 |
array( |
| 27 |
'key' => '_yaymail_template_language', |
| 28 |
'compare' => 'NOT EXISTS', |
| 29 |
'value' => '', |
| 30 |
), |
| 31 |
), |
| 32 |
); |
| 33 |
$res = false; |
| 34 |
$query = new \WP_Query( $default ); |
| 35 |
if ( $query->have_posts() ) { |
| 36 |
$posts = $query->get_posts(); |
| 37 |
foreach ( $posts as $post ) { |
| 38 |
$res = $post->ID; |
| 39 |
break; |
| 40 |
} |
| 41 |
} |
| 42 |
wp_reset_postdata(); |
| 43 |
$GLOBALS[ 'yaymail_template_' . $args['email_template'] ] = $res; |
| 44 |
} |
| 45 |
return $GLOBALS[ 'yaymail_template_' . $args['email_template'] ]; |
| 46 |
} |
| 47 |
return false; |
| 48 |
} |
| 49 |
|
| 50 |
public static function getListPostTemplate() { |
| 51 |
|
| 52 |
$posts = get_posts( |
| 53 |
array( |
| 54 |
'post_type' => 'yaymail_template', |
| 55 |
'post_status' => array( 'publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit' ), |
| 56 |
'numberposts' => -1, |
| 57 |
) |
| 58 |
); |
| 59 |
return $posts; |
| 60 |
} |
| 61 |
|
| 62 |
public static function templateEnableDisable( $getPostID = true ) { |
| 63 |
$template_export = array(); |
| 64 |
$posts = self::getListPostTemplate(); |
| 65 |
if ( count( $posts ) > 0 ) { |
| 66 |
foreach ( $posts as $key => $post ) { |
| 67 |
$template = get_post_meta( $post->ID, '_yaymail_template', true ); |
| 68 |
$template = get_post_meta( $post->ID, '_yaymail_template', true ); |
| 69 |
$template_language = get_post_meta( $post->ID, '_yaymail_template_language', true ); |
| 70 |
if ( isset( $list_use_temp[ $template ][ $template_language ] ) |
| 71 |
&& isset( $list_use_temp[ $template ][ $template_language ]['prev_id'] ) ) { |
| 72 |
wp_delete_post( $post->ID ); |
| 73 |
} else { |
| 74 |
$list_use_temp[ $template ][ $template_language ]['prev_id'] = $post->ID; |
| 75 |
if ( ( ! $template_language ) || 'en' === $template_language ) { |
| 76 |
if ( $getPostID ) { |
| 77 |
$template_export[ $template ]['post_id'] = $post->ID; |
| 78 |
$template_export[ $template ]['_yaymail_status'] = get_post_meta( $post->ID, '_yaymail_status', true ); |
| 79 |
} else { |
| 80 |
$template_export[ $template ] = get_post_meta( $post->ID, '_yaymail_status', true ); |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
return $template_export; |
| 87 |
} |
| 88 |
public static function insert( $args = false ) { |
| 89 |
if ( false != $args && is_array( $args ) ) { |
| 90 |
$arr = array( |
| 91 |
'post_content' => $args['mess'], |
| 92 |
'post_date' => $args['post_date'], |
| 93 |
'post_date_gmt' => $args['post_date'], |
| 94 |
'post_type' => $args['post_type'], |
| 95 |
'post_title' => wp_trim_words( $args['mess'], 200 ), |
| 96 |
'post_status' => 'publish', |
| 97 |
); |
| 98 |
$insert_id = wp_insert_post( $arr ); |
| 99 |
if ( 'yaymail_template' == $args['post_type'] ) { |
| 100 |
$orderItemsTitle = Helper::OrderItemsTitle(); |
| 101 |
$orderItemsDownloadsTitle = Helper::OrderItemsDownloadsTitle(); |
| 102 |
|
| 103 |
update_post_meta( $insert_id, '_yaymail_email_order_item_title', $orderItemsTitle ); |
| 104 |
update_post_meta( $insert_id, '_yaymail_email_order_item_download_title', $orderItemsDownloadsTitle ); |
| 105 |
update_post_meta( $insert_id, '_yaymail_template', $args['_yaymail_template'] ); |
| 106 |
if ( isset( $args['_yaymail_template_language'] ) ) { |
| 107 |
update_post_meta( $insert_id, '_yaymail_template_language', $args['_yaymail_template_language'] ); |
| 108 |
} |
| 109 |
update_post_meta( $insert_id, '_yaymail_elements', $args['_yaymail_elements'] ); |
| 110 |
update_post_meta( $insert_id, '_yaymail_email_backgroundColor_settings', $args['_email_backgroundColor_settings'] ); |
| 111 |
update_post_meta( $insert_id, '_yaymail_status', 0 ); |
| 112 |
} |
| 113 |
return $insert_id; |
| 114 |
} |
| 115 |
return false; |
| 116 |
} |
| 117 |
|
| 118 |
public static function postIDByTemplate( $template ) { |
| 119 |
$agrs_template = array( 'email_template' => $template ); |
| 120 |
if ( self::checkEmailTemplateExist( $agrs_template ) ) { |
| 121 |
$postID = self::checkEmailTemplateExist( $agrs_template ); |
| 122 |
return $postID; |
| 123 |
} |
| 124 |
return false; |
| 125 |
} |
| 126 |
public static function postIDByTemplateLanguage( $template, $language ) { |
| 127 |
$args = array( |
| 128 |
'email_template' => $template, |
| 129 |
'email_language' => $language, |
| 130 |
); |
| 131 |
$default = array( |
| 132 |
'post_type' => 'yaymail_template', |
| 133 |
'post_status' => array( 'publish', 'pending', 'future' ), |
| 134 |
'meta_query' => array( |
| 135 |
'relation' => 'AND', |
| 136 |
array( |
| 137 |
'key' => '_yaymail_template', |
| 138 |
'value' => $args['email_template'], |
| 139 |
'compare' => '=', |
| 140 |
), |
| 141 |
array( |
| 142 |
'key' => '_yaymail_template_language', |
| 143 |
'compare' => '' === $args['email_language'] ? 'NOT EXISTS' : '=', |
| 144 |
'value' => '' === $args['email_language'] ? '' : $args['email_language'], |
| 145 |
), |
| 146 |
), |
| 147 |
); |
| 148 |
$posts = new \WP_Query( $default ); |
| 149 |
if ( $posts->have_posts() ) { |
| 150 |
return $posts->post->ID; |
| 151 |
} |
| 152 |
return false; |
| 153 |
} |
| 154 |
|
| 155 |
public static function getTemplateExport() { |
| 156 |
$template_export = array(); |
| 157 |
$template_export['yaymail_version'] = get_option( 'yaymail_version' ); |
| 158 |
$posts = self::getListPostTemplate(); |
| 159 |
if ( count( $posts ) > 0 ) { |
| 160 |
foreach ( $posts as $key => $post ) { |
| 161 |
$template_export['yaymailTemplateExport'][ $key ]['_yaymail_template'] = get_post_meta( $post->ID, '_yaymail_template', true ); |
| 162 |
$template_export['yaymailTemplateExport'][ $key ]['_yaymail_elements'] = get_post_meta( $post->ID, '_yaymail_elements', true ); |
| 163 |
$template_export['yaymailTemplateExport'][ $key ]['_yaymail_template_language'] = get_post_meta( $post->ID, '_yaymail_template_language', true ); |
| 164 |
} |
| 165 |
} |
| 166 |
return $template_export; |
| 167 |
} |
| 168 |
public static function getListOrders() { |
| 169 |
$data_orders = array(); |
| 170 |
if ( defined( 'WOOCOMMERCE_VERSION' ) ) { |
| 171 |
$list_orders = new \WP_Query( |
| 172 |
array( |
| 173 |
'post_type' => 'shop_order', |
| 174 |
'post_status' => array_keys( wc_get_order_statuses() ), |
| 175 |
'posts_per_page' => 50, |
| 176 |
) |
| 177 |
); |
| 178 |
if ( isset( $list_orders->posts ) && ! empty( ( $list_orders->posts ) ) ) { |
| 179 |
foreach ( $list_orders->posts as $order_item ) { |
| 180 |
if ( $order_item->ID ) { |
| 181 |
$order = new \WC_Order( $order_item->ID ); |
| 182 |
$data_orders[] = array( |
| 183 |
'id' => $order_item->ID, |
| 184 |
'id_real' => $order->get_order_number(), |
| 185 |
'email' => $order->get_billing_email(), |
| 186 |
'first_name' => $order->get_billing_first_name(), |
| 187 |
'last_name' => $order->get_billing_last_name(), |
| 188 |
); |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
return $data_orders; |
| 194 |
|
| 195 |
} |
| 196 |
} |
| 197 |
|