Changeset 2689611
- Timestamp:
- 03/06/2022 03:58:13 PM (4 years ago)
- Location:
- wc-cart-automation/trunk
- Files:
-
- 3 added
- 1 deleted
- 6 edited
-
README.md (added)
-
README.txt (deleted)
-
css/admin.css (modified) (1 diff)
-
css/admin.css.map (modified) (1 diff)
-
includes/class-cpt-automation.php (modified) (8 diffs)
-
includes/class-wcca-admin.php (modified) (10 diffs)
-
includes/class-wcca.php (modified) (5 diffs)
-
js (added)
-
js/wcca_admin.js (added)
-
wc-cart-automation.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
wc-cart-automation/trunk/css/admin.css
r2678573 r2689611 1 body.post-type-wcca .wcca-field{display:flex;align-items:start;margin:1rem 0}body.post-type-wcca .wcca-field .wcca-label{font-weight:bold;flex-basis:30%}body.post-type-wcca .wcca-field .wcca-input{flex-grow:1}body.post-type-wcca .wcca-field .wcca-input>*{width:100%}body.post-type-wcca .wcca-field .wcca-input-checkbox,body.post-type-wcca .wcca-field .wcca-input-radio{display:block;overflow:auto;padding:.5em 0}body.post-type-wcca .wcca-field .wcca-input-checkbox>label,body.post-type-wcca .wcca-field .wcca-input-radio>label{display:block}body.post-type-wcca .wcca-field .wcca-input-checkbox>label+label,body.post-type-wcca .wcca-field .wcca-input-radio>label+label{margin-top:.5em}body.post-type-wcca .wcca-field .wcca-input-textarea{flex-basis:100%;flex-shrink:0}body.post-type-wcca p.warning{padding:.5em;color:#c00;background-color:#ffe6e6}/*# sourceMappingURL=admin.css.map */ 1 body.post-type-wcca .wcca-field { 2 display: flex; 3 align-items: start; 4 margin: 1rem 0; 5 } 6 body.post-type-wcca .wcca-field .wcca-label { 7 font-weight: bold; 8 flex-basis: 30%; 9 } 10 body.post-type-wcca .wcca-field .wcca-input { 11 flex-grow: 1; 12 } 13 body.post-type-wcca .wcca-field .wcca-input > * { 14 width: 100%; 15 } 16 body.post-type-wcca .wcca-field .wcca-input-checkbox, body.post-type-wcca .wcca-field .wcca-input-radio { 17 display: block; 18 overflow: auto; 19 padding: 0.5em 0; 20 } 21 body.post-type-wcca .wcca-field .wcca-input-checkbox > label, body.post-type-wcca .wcca-field .wcca-input-radio > label { 22 display: block; 23 } 24 body.post-type-wcca .wcca-field .wcca-input-checkbox > label + label, body.post-type-wcca .wcca-field .wcca-input-radio > label + label { 25 margin-top: 0.5em; 26 } 27 body.post-type-wcca .wcca-field .wcca-input-textarea { 28 flex-basis: 100%; 29 flex-shrink: 0; 30 } 31 body.post-type-wcca p.warning { 32 padding: 0.5em; 33 color: #cc0000; 34 background-color: #ffe6e6; 35 } 36 37 /*# sourceMappingURL=admin.css.map */ -
wc-cart-automation/trunk/css/admin.css.map
r2678573 r2689611 1 {"version":3,"sourceRoot":"","sources":["admin.scss"],"names":[],"mappings":"AACC ,gCACC,aACA,kBACA,cAEA,4CACC,iBACA,eAGD,4CACC,YAEA,8CACC,WAGD,uGACC,cACA,cACA,eAEA,mHACC,cAEA,+HACC,gBAKH,qDACC,gBACA,cAKH,8BACC,aACA,WACA","file":"admin.css"}1 {"version":3,"sourceRoot":"","sources":["admin.scss"],"names":[],"mappings":"AACC;EACC;EACA;EACA;;AAEA;EACC;EACA;;AAGD;EACC;;AAEA;EACC;;AAGD;EACC;EACA;EACA;;AAEA;EACC;;AAEA;EACC;;AAKH;EACC;EACA;;AAKH;EACC;EACA;EACA","file":"admin.css"} -
wc-cart-automation/trunk/includes/class-cpt-automation.php
r2678573 r2689611 49 49 ] ); 50 50 51 static::add_field( 'products', __( 'Products to add', WCCA_PLUGIN_NAME ), 'select2', [ 52 'post_type' => 'product', 53 'single' => false, 51 static::add_field( 'products', __( 'Products to add', WCCA_PLUGIN_NAME ), 'repeater', [ 52 'sub_fields' => [ 53 [ 54 'option' => 'quantity', 55 'label' => __( 'Quantity', WCCA_PLUGIN_NAME ), 56 'type' => 'number', 57 ], 58 [ 59 'option' => 'product', 60 'label' => __( 'Product', WCCA_PLUGIN_NAME ), 61 'type' => 'select2', 62 'args' => [ 63 'post_type' => 'product', 64 'single' => true, 65 ], 66 ], 67 ], 54 68 ] ); 55 69 … … 64 78 * @param string $label 65 79 * @param string $type 66 * @param array $args80 * @param array $args 67 81 */ 68 82 private static function add_field( string $option, string $label, string $type = 'text', array $args = [] ): void { … … 98 112 // ensure that the cart is loaded 99 113 WC()->cart->get_cart(); 100 foreach ( $cart[ 'content'] as $product ) {114 foreach ( $cart['content'] as $product ) { 101 115 WC()->cart->add_to_cart( $product ); 102 116 } 103 117 104 foreach ( $cart[ 'coupons'] as $coupon ) {118 foreach ( $cart['coupons'] as $coupon ) { 105 119 if ( $the_coupon = new WC_Coupon( $coupon ) ) { 106 120 WC()->cart->apply_coupon( $the_coupon->get_code() ); … … 116 130 */ 117 131 public static function woocommerce_init(): void { 118 if ( ! ( $wcca = $_REQUEST[ 'wcca'] ?? null ) ) {132 if ( ! ( $wcca = $_REQUEST['wcca'] ?? null ) ) { 119 133 // bail early if there is no wcca code in the URL 120 134 return; … … 168 182 $wcca_ID = $wcca_ID->ID; 169 183 } 170 171 $wcca = get_post_meta( $wcca_ID );172 184 173 185 // ensure that the cart is loaded … … 192 204 wcca()->add_customer_wcca_opening( $wcca_ID ); 193 205 194 foreach ( $wcca[ 'wcca_products' ] as $product ) { 195 WC()->cart->add_to_cart( $product ); 196 } 197 198 foreach ( $wcca[ 'wcca_coupons' ] as $coupon ) { 206 foreach ( get_post_meta( $wcca_ID, 'wcca_products', true ) as $product ) { 207 if ( is_scalar( $product ) ) { 208 // store only the product with quantity 1 (early version of the plugin) 209 WC()->cart->add_to_cart( $product ); 210 } else { 211 $wc_product = wc_get_product( $product['product'] ); 212 if ( $wc_product->is_type( 'simple' ) ) { 213 WC()->cart->add_to_cart( $wc_product->get_id(), $product['quantity'] ?? 1 ); 214 } elseif ( $wc_product->is_type( 'variation' ) ) { 215 WC()->cart->add_to_cart( $wc_product->get_parent_id(), $product['quantity'] ?? 1, $wc_product->get_id() ); 216 } 217 } 218 } 219 220 foreach ( get_post_meta( $wcca_ID, 'wcca_coupons' ) as $coupon ) { 199 221 if ( $the_coupon = new WC_Coupon( $coupon ) ) { 200 222 WC()->cart->apply_coupon( $the_coupon->get_code() ); … … 218 240 public static function save_post( int $wcca_ID ): void { 219 241 foreach ( static::$fields as $field => $data ) { 220 if ( $data[ 'args' ][ 'single' ] ?? true ) { 242 if ( ! empty( $data['args']['sub_fields'] ?? [] ) ) { 243 /*$sub_fields = []; 244 foreach ( $_REQUEST[ 'wcca_' . $field ] ?? [] as $row_index => $row_values ) { 245 foreach ( $data['args']['sub_fields'] as $key => $sub_field ) { 246 $a = 0; 247 } 248 }*/ 249 // store all the repeater values in the same repeater meta 250 $sanitized_values = []; 251 foreach ( $_REQUEST[ 'wcca_' . $field ] ?? [] as $row_id => $row ) { 252 if ( $row_id < 0 ) { 253 continue; 254 } 255 foreach ( $row as $key => $value ) { 256 if ( is_array( $value ) ) { 257 $row[ $key ] = array_map( 'sanitize_text_field', $value ); 258 } else { 259 $row[ $key ] = sanitize_text_field( $value ); 260 } 261 } 262 $sanitized_values[] = $row; 263 } 264 265 update_post_meta( $wcca_ID, 'wcca_' . $field, $sanitized_values ); 266 } elseif ( $data['args']['single'] ?? true ) { 221 267 update_post_meta( $wcca_ID, 'wcca_' . $field, sanitize_text_field( $_REQUEST[ 'wcca_' . $field ] ?? null ) ); 222 268 } else { … … 274 320 public static function render_meta_box_fields() { 275 321 foreach ( static::$fields as $field ) { 276 WCCA_Admin::the_custom_field_admin( $field[ 'name' ], $field[ 'label' ], $field[ 'type' ], $field[ 'args'] );322 WCCA_Admin::the_custom_field_admin( $field['name'], $field['label'], $field['type'], $field['args'] ); 277 323 } 278 324 } -
wc-cart-automation/trunk/includes/class-wcca-admin.php
r2678573 r2689611 4 4 5 5 use Exception; 6 use WP_Post; 6 7 use WP_Query; 7 8 … … 30 31 $count = count( $openings, COUNT_RECURSIVE ); 31 32 printf( _x( '<span title="all %s">a : %s</span>', 'all', WCCA_PLUGIN_NAME ), $count, $count ); 32 } else if ( 'orders' === $column ) {33 } elseif ( 'orders' === $column ) { 33 34 $orders = get_option( 'wcca_orders_' . $post_id, [] ); 34 35 echo count( $orders ); … … 37 38 38 39 public static function manage_wcca_posts_columns( array $columns ): array { 39 $columns[ 'openings'] = __( 'Openings', WCCA_PLUGIN_NAME );40 $columns[ 'orders'] = __( 'Orders', WCCA_PLUGIN_NAME );40 $columns['openings'] = __( 'Openings', WCCA_PLUGIN_NAME ); 41 $columns['orders'] = __( 'Orders', WCCA_PLUGIN_NAME ); 41 42 42 43 return $columns; 43 44 } 44 45 45 46 46 /** … … 72 72 73 73 $stati = get_post_stati(); 74 unset( $stati[ 'auto-draft' ], $stati[ 'revision' ], $stati[ 'publish'] );74 unset( $stati['auto-draft'], $stati['revision'], $stati['publish'] ); 75 75 $inactive = ( new WP_Query( [ 'post_type' => 'wcca', 'post_status' => $stati ] ) )->post_count; 76 76 … … 84 84 * @param string $label 85 85 * @param string $type 86 * @param array $args86 * @param array $args 87 87 * 88 88 * @throws Exception 89 89 */ 90 90 public static function the_custom_field_admin( string $option, string $label, string $type = 'text', array $args = [] ): void { 91 $required = $args[ 'required' ] ?? false; 91 $required = $args['required'] ?? false; 92 $single = $args['single'] ?? true; 93 $default_value = $args['default_value'] ?? $_REQUEST[ 'wcca_' . $option ] ?? get_post_meta( get_the_ID(), 'wcca_' . $option, $single ) ?: null; 92 94 93 95 switch ( $type ) { 96 case 'repeater': 97 // register the repeater JS 98 wp_enqueue_script( WCCA_PLUGIN_NAME . '-admin', plugin_dir_url( WCCA_PLUGIN_FILE ) . '/js/wcca_admin.js' ); 99 100 // get the repeated values 101 $repeater_values = get_post_meta( get_the_ID(), 'wcca_' . $option, true ); 102 $repeater_count = count( $repeater_values ) ?: 1; 103 ob_start(); 104 // create every repeater row 105 for ( $repeater_key = - 1; $repeater_key < $repeater_count; $repeater_key ++ ) { 106 printf( '<div class="repeater-row" style="display:%s;">', $repeater_key === - 1 ? 'none' : 'block' ); 107 foreach ( $args['sub_fields'] as $sub_field ) { 108 $sub_field['args'] = $sub_field['args'] ?? []; 109 $sub_field['args']['default_value'] = $repeater_values[ $repeater_key ][ $sub_field['option'] ] ?? null; 110 WCCA_Admin::the_custom_field_admin( $option . '[' . $repeater_key . ']' . '[' . $sub_field['option'] . ']', $sub_field['label'], $sub_field['type'], $sub_field['args'] ); 111 } 112 printf( '<a href="#" class="delete_current_row">%s</a>', __( 'Delete', WCCA_PLUGIN_NAME ) ); 113 echo '<hr>'; 114 echo '</div>'; 115 } 116 117 // insert the "add row" button 118 printf( '<a href="#" class="add_row">%s</a>', __( 'Add', WCCA_PLUGIN_NAME ) ); 119 120 $html = ob_get_clean(); 121 122 break; 94 123 case 'text': 95 124 case 'url': … … 97 126 case 'email': 98 127 case 'number': 99 $default_value = $_REQUEST[ 'wcca_' . $option ] ?? get_post_meta( get_the_ID(), 'wcca_' . $option, true ) ?: null;100 128 $default_value = esc_attr( $default_value ); 101 129 … … 110 138 break; 111 139 case 'textarea': 112 $default_value = $_REQUEST[ 'wcca_' . $option ] ?? get_post_meta( get_the_ID(), 'wcca_' . $option, true ) ?: null;113 140 $default_value = esc_attr( $default_value ); 114 141 … … 119 146 case 'select': 120 147 case 'select2': 121 $single = $args[ 'single' ] ?? true; 122 $default_value = $_REQUEST[ 'wcca_' . $option ] ?? get_post_meta( get_the_ID(), 'wcca_' . $option, $single ); 123 $default_value = esc_attr( $default_value ); 124 125 if ( $post_type = $args[ 'post_type' ] ?? null ) { 148 if ( is_array( $default_value ) ) { 149 $default_value = array_map( 'esc_attr', $default_value ); 150 } else { 151 $default_value = esc_attr( $default_value ); 152 } 153 154 $opt_groups = []; 155 156 if ( $post_type = $args['post_type'] ?? null ) { 126 157 $query = new WP_Query( [ 127 158 'post_type' => $post_type, 128 ' limit'=> - 1,159 'posts_per_page' => - 1, 129 160 'post_status__not_in' => [ 'trash' ], 161 'order' => 'ASC', 162 'orderby' => 'title', 130 163 ] ); 131 164 $choices = []; 132 foreach ( $query->posts as $product ) { 133 $choices[ $product->ID ] = $product->post_title; 165 166 foreach ( $query->posts as $post ) { 167 if ( 'product' === $post->post_type ) { 168 $wc_product = wc_get_product( $post ); 169 if ( $wc_product->is_type( 'simple' ) ) { 170 $choices[ $wc_product->get_id() ] = $wc_product->get_title(); 171 } elseif ( $wc_product->is_type( 'variable' ) ) { 172 $choices[ $wc_product->get_id() ] = $wc_product->get_name(); 173 174 foreach ( $wc_product->get_available_variations() as $variation ) { 175 $variation = wc_get_product( $variation['variation_id'] ); 176 $opt_groups[ $variation->get_id() ] = $wc_product->get_id(); 177 $choices[ $variation->get_id() ] = $variation->get_name(); 178 } 179 } 180 } elseif ( $post instanceof WP_Post ) { 181 $choices[ $post->ID ] = $post->post_title; 182 } 134 183 } 135 184 } else { 136 $choices = $args[ 'choices'] ?? [];185 $choices = $args['choices'] ?? []; 137 186 } 138 187 … … 167 216 $single ? '' : '[]', 168 217 $option, 169 ( $args[ 'required'] ?? false ) ? 'required' : '',218 ( $args['required'] ?? false ) ? 'required' : '', 170 219 $single ? '' : 'multiple' 171 220 ); … … 177 226 foreach ( $choices as $key => $value ) { 178 227 $selected = in_array( $key, (array) $default_value ); 179 $html .= sprintf( 180 '<option value="%s" %s>%s</option>', 181 $key, 182 ( $selected ? 'selected' : '' ), 183 $value 184 ); 228 if ( in_array( $key, $opt_groups ) ) { 229 // open the option group 230 $html .= sprintf( 231 '<optgroup label="%s">', 232 $value, 233 ); 234 } else { 235 // close the option group if the current option is not in a group 236 if ( ! array_key_exists( $key, $opt_groups ) ) { 237 $html .= '</optgroup>'; 238 } 239 240 $html .= sprintf( 241 '<option value="%s" %s>%s</option>', 242 $key, 243 ( $selected ? 'selected' : '' ), 244 $value 245 ); 246 } 185 247 } 186 248 -
wc-cart-automation/trunk/includes/class-wcca.php
r2678573 r2689611 72 72 public function add_customer_wcca_opening( ?int $wcca_ID = null ) { 73 73 if ( empty( $wcca_ID ) ) { 74 if ( empty( $wcca_ID = ( $_COOKIE[ 'wcca'] ?? null ) ) ) {74 if ( empty( $wcca_ID = ( $_COOKIE['wcca'] ?? null ) ) ) { 75 75 // something wrong happened and the trace of the user has been lost 76 76 return; … … 79 79 80 80 $openings = get_option( 'wcca_openings_' . $wcca_ID, [] ); 81 if ( empty( $current_user = WC()->session->get_customer_unique_id() ) ) { 81 82 if ( method_exists( WC()->session, 'get_customer_unique_id' ) ) { 83 $current_user = WC()->session->get_customer_unique_id(); 84 } else { 85 $current_user = WC()->session->get_customer_id(); 86 } 87 88 if ( empty( $current_user ) ) { 82 89 // we should not get here, but just in case, consider that is some anonymous visitor 83 90 $current_user = 0; … … 88 95 89 96 setcookie( 'wcca', $wcca_ID ); 90 $_COOKIE[ 'wcca'] = $wcca_ID;97 $_COOKIE['wcca'] = $wcca_ID; 91 98 92 99 update_option( 'wcca_openings_' . $wcca_ID, $openings ); … … 97 104 * 98 105 * @param WC_Order|null $order 99 * @param ?int $wcca_ID106 * @param ?int $wcca_ID 100 107 */ 101 108 public function add_order_to_wcca( WC_Order $order = null, ?int $wcca_ID = null ) { 102 109 if ( empty( $wcca_ID ) ) { 103 if ( empty( $wcca_ID = ( $_COOKIE[ 'wcca'] ?? null ) ) ) {110 if ( empty( $wcca_ID = ( $_COOKIE['wcca'] ?? null ) ) ) { 104 111 // something wrong happened and the trace of the user has been lost 105 112 return; … … 108 115 109 116 setcookie( 'wcca', null ); 110 $_COOKIE[ 'wcca'] = null;117 $_COOKIE['wcca'] = null; 111 118 112 119 $orders = get_option( 'wcca_orders_' . $wcca_ID, [] ); -
wc-cart-automation/trunk/wc-cart-automation.php
r2678573 r2689611 8 8 * Text Domain: wc-cart-automation 9 9 * Domain Path: /languages 10 * Version: 0. 1.210 * Version: 0.2.0 11 11 * 12 12 * @package Wc_Promo_Cart
Note: See TracChangeset
for help on using the changeset viewer.