| 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' => 'OR', |
| 17 |
array( |
| 18 |
'key' => '_yaymail_template', |
| 19 |
'value' => $args['email_template'], |
| 20 |
'compare' => '=', |
| 21 |
), |
| 22 |
), |
| 23 |
); |
| 24 |
$posts = new \WP_Query( $default ); |
| 25 |
if ( $posts->have_posts() ) { |
| 26 |
return $posts->post->ID; |
| 27 |
} |
| 28 |
} |
| 29 |
return false; |
| 30 |
} |
| 31 |
public static function getListPostTemplate() { |
| 32 |
|
| 33 |
$posts = get_posts( |
| 34 |
array( |
| 35 |
'post_type' => 'yaymail_template', |
| 36 |
'post_status' => array( 'publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit' ), |
| 37 |
'numberposts' => -1, |
| 38 |
) |
| 39 |
); |
| 40 |
return $posts; |
| 41 |
} |
| 42 |
// delete all template in database. |
| 43 |
// public static function deleteAllTemplate() |
| 44 |
// { |
| 45 |
// $templates = self::getListPostTemplate(); |
| 46 |
// foreach ($templates as $key => $template) { |
| 47 |
// wp_delete_post($template->ID); |
| 48 |
// } |
| 49 |
// } |
| 50 |
public static function templateEnableDisable( $getPostID = true ) { |
| 51 |
$template_export = array(); |
| 52 |
$posts = self::getListPostTemplate(); |
| 53 |
$settingDefaultEnableDisable = array( |
| 54 |
'new_order' => 1, |
| 55 |
'cancelled_order' => 1, |
| 56 |
'failed_order' => 1, |
| 57 |
'customer_on_hold_order' => 1, |
| 58 |
'customer_processing_order' => 1, |
| 59 |
'customer_completed_order' => 1, |
| 60 |
'customer_refunded_order' => 1, |
| 61 |
'customer_invoice' => 0, |
| 62 |
'customer_note' => 0, |
| 63 |
'customer_reset_password' => 0, |
| 64 |
'customer_new_account' => 0, |
| 65 |
); |
| 66 |
if ( count( $posts ) > 0 ) { |
| 67 |
foreach ( $posts as $key => $post ) { |
| 68 |
$template = get_post_meta( $post->ID, '_yaymail_template', true ); |
| 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 |
// free version |
| 74 |
if ( array_key_exists( $template, $settingDefaultEnableDisable ) ) { |
| 75 |
$template_export[ $template ] = get_post_meta( $post->ID, '_yaymail_status', true ); |
| 76 |
} else { |
| 77 |
update_post_meta( $post->ID, '_yaymail_status', 0 ); |
| 78 |
$template_export[ $template ] = get_post_meta( $post->ID, '_yaymail_status', false ); |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
return $template_export; |
| 84 |
} |
| 85 |
public static function insert( $args = false ) { |
| 86 |
if ( false != $args && is_array( $args ) ) { |
| 87 |
$arr = array( |
| 88 |
'post_content' => $args['mess'], |
| 89 |
'post_date' => $args['post_date'], |
| 90 |
'post_date_gmt' => $args['post_date'], |
| 91 |
'post_type' => $args['post_type'], |
| 92 |
'post_title' => wp_trim_words( $args['mess'], 200 ), |
| 93 |
'post_status' => 'publish', |
| 94 |
); |
| 95 |
$insert_id = wp_insert_post( $arr ); |
| 96 |
if ( 'yaymail_template' == $args['post_type'] ) { |
| 97 |
update_post_meta( $insert_id, '_yaymail_template', $args['_yaymail_template'] ); |
| 98 |
update_post_meta( $insert_id, '_yaymail_html', $args['_yaymail_html'] ); |
| 99 |
update_post_meta( $insert_id, '_yaymail_elements', $args['_yaymail_elements'] ); |
| 100 |
update_post_meta( $insert_id, '_yaymail_email_backgroundColor_settings', $args['_email_backgroundColor_settings'] ); |
| 101 |
update_post_meta( $insert_id, '_yaymail_status', 0 ); |
| 102 |
} |
| 103 |
return $insert_id; |
| 104 |
} |
| 105 |
return false; |
| 106 |
} |
| 107 |
|
| 108 |
public static function postIDByTemplate( $template ) { |
| 109 |
$agrs_template = array( 'email_template' => $template ); |
| 110 |
if ( self::checkEmailTemplateExist( $agrs_template ) ) { |
| 111 |
$postID = self::checkEmailTemplateExist( $agrs_template ); |
| 112 |
return $postID; |
| 113 |
} |
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
public static function getTemplateExport() { |
| 118 |
$template_export = array(); |
| 119 |
$template_export['yaymail_version'] = get_option( 'yaymail_version' ); |
| 120 |
$posts = self::getListPostTemplate(); |
| 121 |
if ( count( $posts ) > 0 ) { |
| 122 |
foreach ( $posts as $key => $post ) { |
| 123 |
$template_export['yaymailTemplateExport'][ $key ]['_yaymail_template'] = get_post_meta( $post->ID, '_yaymail_template', true ); |
| 124 |
$template_export['yaymailTemplateExport'][ $key ]['_yaymail_html'] = get_post_meta( $post->ID, '_yaymail_html', true ); |
| 125 |
$template_export['yaymailTemplateExport'][ $key ]['_yaymail_elements'] = get_post_meta( $post->ID, '_yaymail_elements', true ); |
| 126 |
} |
| 127 |
} |
| 128 |
return $template_export; |
| 129 |
} |
| 130 |
public static function getListOrders() { |
| 131 |
$data_orders = array(); |
| 132 |
if ( defined( 'WOOCOMMERCE_VERSION' ) ) { |
| 133 |
$list_orders = new \WP_Query( |
| 134 |
array( |
| 135 |
'post_type' => 'shop_order', |
| 136 |
'post_status' => array_keys( wc_get_order_statuses() ), |
| 137 |
'posts_per_page' => 50, |
| 138 |
) |
| 139 |
); |
| 140 |
if ( isset( $list_orders->posts ) && ! empty( ( $list_orders->posts ) ) ) { |
| 141 |
foreach ( $list_orders->posts as $order_item ) { |
| 142 |
if ( $order_item->ID ) { |
| 143 |
$order = new \WC_Order( $order_item->ID ); |
| 144 |
$data_orders[] = array( |
| 145 |
'id' => $order_item->ID, |
| 146 |
'id_real' => $order->get_order_number(), |
| 147 |
'email' => $order->get_billing_email(), |
| 148 |
'first_name' => $order->get_billing_first_name(), |
| 149 |
'last_name' => $order->get_billing_last_name(), |
| 150 |
); |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
return $data_orders; |
| 156 |
|
| 157 |
} |
| 158 |
} |
| 159 |
|