1 <?php
  2 /**
  3  * Post Types Admin
  4  *
  5  * @author      WooThemes
  6  * @category    Admin
  7  * @package     WooCommerce/Admin
  8  * @version     2.1.0
  9  */
 10 
 11 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
 12 
 13 if ( ! class_exists( 'WC_Admin_Post_Types' ) ) :
 14 
 15 /**
 16  * WC_Admin_Post_Types Class
 17  */
 18 class WC_Admin_Post_Types {
 19 
 20     /**
 21      * Constructor
 22      */
 23     public function __construct() {
 24         add_action( 'admin_init', array( $this, 'include_post_type_handlers' ) );
 25         add_filter( 'post_updated_messages', array( $this, 'post_updated_messages' ) );
 26         add_action( 'admin_print_scripts', array( $this, 'disable_autosave' ) );
 27 
 28         // Status transitions
 29         add_action( 'delete_post', array( $this, 'delete_post' ) );
 30         add_action( 'wp_trash_post', array( $this, 'trash_post' ) );
 31         add_action( 'untrash_post', array( $this, 'untrash_post' ) );
 32     }
 33 
 34     /**
 35      * Conditonally load classes and functions only needed when viewing a post type.
 36      */
 37     public function include_post_type_handlers() {
 38         include( 'post-types/class-wc-admin-meta-boxes.php' );
 39         include( 'post-types/class-wc-admin-cpt-product.php' );
 40         include( 'post-types/class-wc-admin-cpt-shop_order.php' );
 41         include( 'post-types/class-wc-admin-cpt-shop_coupon.php' );
 42 
 43         if ( ! function_exists( 'duplicate_post_plugin_activation' ) )
 44             include( 'class-wc-admin-duplicate-product.php' );
 45     }
 46 
 47     /**
 48      * Change messages when a post type is updated.
 49      *
 50      * @param  array $messages
 51      * @return array
 52      */
 53     public function post_updated_messages( $messages ) {
 54         global $post, $post_ID;
 55 
 56         $messages['product'] = array(
 57             0 => '', // Unused. Messages start at index 1.
 58             1 => sprintf( __( 'Product updated. <a href="%s">View Product</a>', 'woocommerce' ), esc_url( get_permalink($post_ID) ) ),
 59             2 => __( 'Custom field updated.', 'woocommerce' ),
 60             3 => __( 'Custom field deleted.', 'woocommerce' ),
 61             4 => __( 'Product updated.', 'woocommerce' ),
 62             5 => isset($_GET['revision']) ? sprintf( __( 'Product restored to revision from %s', 'woocommerce' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
 63             6 => sprintf( __( 'Product published. <a href="%s">View Product</a>', 'woocommerce' ), esc_url( get_permalink($post_ID) ) ),
 64             7 => __( 'Product saved.', 'woocommerce' ),
 65             8 => sprintf( __( 'Product submitted. <a target="_blank" href="%s">Preview Product</a>', 'woocommerce' ), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
 66             9 => sprintf( __( 'Product scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview Product</a>', 'woocommerce' ),
 67               date_i18n( __( 'M j, Y @ G:i', 'woocommerce' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
 68             10 => sprintf( __( 'Product draft updated. <a target="_blank" href="%s">Preview Product</a>', 'woocommerce' ), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
 69         );
 70 
 71         $messages['shop_order'] = array(
 72             0 => '', // Unused. Messages start at index 1.
 73             1 => __( 'Order updated.', 'woocommerce' ),
 74             2 => __( 'Custom field updated.', 'woocommerce' ),
 75             3 => __( 'Custom field deleted.', 'woocommerce' ),
 76             4 => __( 'Order updated.', 'woocommerce' ),
 77             5 => isset($_GET['revision']) ? sprintf( __( 'Order restored to revision from %s', 'woocommerce' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
 78             6 => __( 'Order updated.', 'woocommerce' ),
 79             7 => __( 'Order saved.', 'woocommerce' ),
 80             8 => __( 'Order submitted.', 'woocommerce' ),
 81             9 => sprintf( __( 'Order scheduled for: <strong>%1$s</strong>.', 'woocommerce' ),
 82               date_i18n( __( 'M j, Y @ G:i', 'woocommerce' ), strtotime( $post->post_date ) ) ),
 83             10 => __( 'Order draft updated.', 'woocommerce' )
 84         );
 85 
 86         $messages['shop_coupon'] = array(
 87             0 => '', // Unused. Messages start at index 1.
 88             1 => __( 'Coupon updated.', 'woocommerce' ),
 89             2 => __( 'Custom field updated.', 'woocommerce' ),
 90             3 => __( 'Custom field deleted.', 'woocommerce' ),
 91             4 => __( 'Coupon updated.', 'woocommerce' ),
 92             5 => isset($_GET['revision']) ? sprintf( __( 'Coupon restored to revision from %s', 'woocommerce' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
 93             6 => __( 'Coupon updated.', 'woocommerce' ),
 94             7 => __( 'Coupon saved.', 'woocommerce' ),
 95             8 => __( 'Coupon submitted.', 'woocommerce' ),
 96             9 => sprintf( __( 'Coupon scheduled for: <strong>%1$s</strong>.', 'woocommerce' ),
 97               date_i18n( __( 'M j, Y @ G:i', 'woocommerce' ), strtotime( $post->post_date ) ) ),
 98             10 => __( 'Coupon draft updated.', 'woocommerce' )
 99         );
100 
101         return $messages;
102     }
103 
104     /**
105      * Disable the auto-save functionality for Orders.
106      *
107      * @access public
108      * @return void
109      */
110     public function disable_autosave(){
111         global $post;
112 
113         if ( $post && get_post_type( $post->ID ) === 'shop_order' ) {
114             wp_dequeue_script( 'autosave' );
115         }
116     }
117 
118     /**
119      * Removes variations etc belonging to a deleted post, and clears transients
120      *
121      * @access public
122      * @param mixed $id ID of post being deleted
123      * @return void
124      */
125     public function delete_post( $id ) {
126         global $woocommerce, $wpdb;
127 
128         if ( ! current_user_can( 'delete_posts' ) )
129             return;
130 
131         if ( $id > 0 ) {
132 
133             $post_type = get_post_type( $id );
134 
135             switch( $post_type ) {
136                 case 'product' :
137 
138                     $child_product_variations = get_children( 'post_parent=' . $id . '&post_type=product_variation' );
139 
140                     if ( $child_product_variations ) {
141                         foreach ( $child_product_variations as $child ) {
142                             wp_delete_post( $child->ID, true );
143                         }
144                     }
145 
146                     $child_products = get_children( 'post_parent=' . $id . '&post_type=product' );
147 
148                     if ( $child_products ) {
149                         foreach ( $child_products as $child ) {
150                             $child_post = array();
151                             $child_post['ID'] = $child->ID;
152                             $child_post['post_parent'] = 0;
153                             wp_update_post( $child_post );
154                         }
155                     }
156 
157                     wc_delete_product_transients();
158 
159                 break;
160                 case 'product_variation' :
161 
162                     wc_delete_product_transients();
163 
164                 break;
165             }
166         }
167     }
168 
169     /**
170      * woocommerce_trash_post function.
171      *
172      * @access public
173      * @param mixed $id
174      * @return void
175      */
176     public function trash_post( $id ) {
177         if ( $id > 0 ) {
178 
179             $post_type = get_post_type( $id );
180 
181             if ( 'shop_order' == $post_type ) {
182 
183                 // Delete count - meta doesn't work on trashed posts
184                 $user_id = get_post_meta( $id, '_customer_user', true );
185 
186                 if ( $user_id > 0 ) {
187                     update_user_meta( $user_id, '_order_count', '' );
188                     update_user_meta( $user_id, '_money_spent', '' );
189                 }
190 
191                 delete_transient( 'woocommerce_processing_order_count' );
192                 delete_transient( 'wc_term_counts' );
193             }
194 
195         }
196     }
197 
198     /**
199      * woocommerce_untrash_post function.
200      *
201      * @access public
202      * @param mixed $id
203      * @return void
204      */
205     public function untrash_post( $id ) {
206         if ( $id > 0 ) {
207 
208             $post_type = get_post_type( $id );
209 
210             if ( 'shop_order' == $post_type ) {
211 
212                 // Delete count - meta doesn't work on trashed posts
213                 $user_id = get_post_meta( $id, '_customer_user', true );
214 
215                 if ( $user_id > 0 ) {
216                     update_user_meta( $user_id, '_order_count', '' );
217                     update_user_meta( $user_id, '_money_spent', '' );
218                 }
219 
220                 delete_transient( 'woocommerce_processing_order_count' );
221                 delete_transient( 'wc_term_counts' );
222             }
223 
224         }
225     }
226 }
227 
228 endif;
229 
230 return new WC_Admin_Post_Types();
WooCommerce API documentation generated by ApiGen 2.8.0