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