/**
  * Perform any version-related changes.
  *
  * @since 1.0.0
  * @param int $installed_version the currently installed version of the plugin
  */
 protected function upgrade($installed_version)
 {
     // Always update icon options
     $this->icons->update_icon_options();
     if (version_compare($installed_version, '1.1.0', '<')) {
         foreach ($this->order_statuses->get_core_order_statuses() as $slug => $core_status) {
             $status = new WC_Order_Status_Manager_Order_Status($slug);
             $post_id = $status->get_id();
             $slug = str_replace('wc-', '', $slug);
             switch ($slug) {
                 case 'processing':
                     update_post_meta($post_id, '_include_in_reports', 'yes');
                     break;
                 case 'on-hold':
                     update_post_meta($post_id, '_include_in_reports', 'yes');
                     break;
                 case 'completed':
                     update_post_meta($post_id, '_include_in_reports', 'yes');
                     break;
                 case 'refunded':
                     update_post_meta($post_id, '_include_in_reports', 'yes');
                     break;
             }
         }
     }
 }
 /**
  * Display status description as a tooltip on the view-order screen
  *
  * @since 1.0.0
  * @param int $order_id The order ID
  */
 public function add_order_description_tooltip($order_id)
 {
     $order = wc_get_order($order_id);
     if ($order) {
         $status = new WC_Order_Status_Manager_Order_Status($order->get_status());
         if ($status && ($description = $status->get_description())) {
             wc_enqueue_js("\n\t\t\t\t\t\$( 'mark.order-status' ).tipTip({ content: '" . esc_js($description) . "' });\n\t\t\t\t");
         }
     }
 }
 /**
  * Output custom column content
  *
  * @since 1.0.0
  * @param string $column
  * @param int $post_id
  */
 public function custom_column_content($column, $post_id)
 {
     $status = new WC_Order_Status_Manager_Order_Status($post_id);
     switch ($column) {
         case 'icon':
             $color = $status->get_color();
             $icon = $status->get_icon();
             $style = '';
             if ($color) {
                 if ($icon) {
                     $style = 'color: ' . $color . ';';
                 } else {
                     $style = 'background-color: ' . $color . '; color: ' . wc_order_status_manager()->icons->get_contrast_text_color($color) . ';';
                 }
             }
             if (is_numeric($icon)) {
                 $icon_src = wp_get_attachment_image_src($icon, 'wc_order_status_icon');
                 if ($icon_src) {
                     $style .= 'background-image: url( ' . $icon_src[0] . ');';
                 }
             }
             printf('<mark class="%s %s tips" style="%s" data-tip="%s">%s</mark>', sanitize_title($status->get_slug()), $icon ? 'has-icon ' . $icon : '', $style, esc_attr($status->get_name()), esc_html($status->get_name()));
             break;
         case 'slug':
             echo esc_html($status->get_slug());
             break;
         case 'description':
             echo esc_html($status->get_description());
             break;
         case 'type':
             printf('<span class="badge %s">%s</span>', sanitize_title($status->get_type()), esc_html($status->get_type()));
             break;
     }
 }
    /**
     * Print styles for custom order status icons
     *
     * @since 1.0.0
     */
    public function custom_order_status_icons()
    {
        $custom_status_colors = array();
        $custom_status_badges = array();
        $custom_status_icons = array();
        $custom_action_icons = array();
        foreach (wc_get_order_statuses() as $slug => $name) {
            $status = new WC_Order_Status_Manager_Order_Status($slug);
            // Sanity check: bail if no status was found.
            // This can happen if some statuses are registered late
            if (!$status || !$status->get_id()) {
                continue;
            }
            $color = $status->get_color();
            $icon = $status->get_icon();
            $action_icon = $status->get_action_icon();
            if ($color) {
                $custom_status_colors[esc_attr($status->get_slug())] = $color;
            }
            // Font icon
            if ($icon && ($icon_details = wc_order_status_manager()->icons->get_icon_details($icon))) {
                $custom_status_icons[esc_attr($status->get_slug())] = $icon_details;
            } elseif (is_numeric($icon) && ($icon_src = wp_get_attachment_image_src($icon, 'wc_order_status_icon'))) {
                $custom_status_icons[esc_attr($status->get_slug())] = $icon_src[0];
            } elseif (!$icon) {
                $custom_status_badges[] = esc_attr($status->get_slug());
            }
            // Font action icon
            if ($action_icon && ($action_icon_details = wc_order_status_manager()->icons->get_icon_details($action_icon))) {
                $custom_action_icons[esc_attr($status->get_slug())] = $action_icon_details;
            } elseif (is_numeric($action_icon) && ($action_icon_src = wp_get_attachment_image_src($action_icon, 'wc_order_status_icon'))) {
                $custom_action_icons[esc_attr($status->get_slug())] = $action_icon_src[0];
            }
        }
        ?>
		<!-- Custom Order Status Icon styles -->
		<style type="text/css">
			/*<![CDATA[*/

			<?php 
        // General styles for status badges
        ?>
			<?php 
        if (!empty($custom_status_badges)) {
            ?>
				.widefat .column-order_status mark.<?php 
            echo implode(', .widefat .column-order_status mark.', $custom_status_badges);
            ?>
 {
					display: inline-block;
					font-size: 0.8em;
					line-height: 1.1;
					text-indent: 0;
					background-color: #666;
					width: auto;
					height: auto;
					padding: 0.4em;
					color: #fff;
					border-radius: 2px;
					word-wrap: break-word;
					max-width: 100%;
				}

				.widefat .column-order_status mark.<?php 
            echo implode(':after, .widefat .column-order_status mark.', $custom_status_badges);
            ?>
:after {
					display: none;
				}
			<?php 
        }
        ?>

			<?php 
        // General styles for status icons
        ?>
			<?php 
        if (!empty($custom_status_icons)) {
            ?>

				<?php 
            $custom_status_font_icons = array_filter($custom_status_icons, 'is_array');
            ?>

				<?php 
            if (!empty($custom_status_font_icons)) {
                ?>

					.widefat .column-order_status mark.<?php 
                echo implode(':after, .widefat .column-order_status mark.', array_keys($custom_status_font_icons));
                ?>
:after {
						speak: none;
						font-weight: normal;
						font-variant: normal;
						text-transform: none;
						line-height: 1;
						-webkit-font-smoothing: antialiased;
						margin: 0;
						text-indent: 0;
						position: absolute;
						top: 0;
						left: 0;
						width: 100%;
						height: 100%;
						text-align: center;
					}

				<?php 
            }
            ?>
			<?php 
        }
        ?>

			<?php 
        // General styles for action icons
        ?>
			.widefat .column-order_actions a.button {
				padding: 0 0.5em;
				height: 2em;
				line-height: 1.9em;
			}

			<?php 
        if (!empty($custom_action_icons)) {
            ?>

				<?php 
            $custom_action_font_icons = array_filter($custom_action_icons, 'is_array');
            ?>
				<?php 
            if (!empty($custom_action_font_icons)) {
                ?>

					.order_actions .<?php 
                echo implode(', .order_actions .', array_keys($custom_action_icons));
                ?>
 {
						display: block;
						text-indent: -9999px;
						position: relative;
						padding: 0!important;
						height: 2em!important;
						width: 2em;
					}
					.order_actions .<?php 
                echo implode(':after, .order_actions .', array_keys($custom_action_icons));
                ?>
:after {
						speak: none;
						font-weight: 400;
						font-variant: normal;
						text-transform: none;
						-webkit-font-smoothing: antialiased;
						margin: 0;
						text-indent: 0;
						position: absolute;
						top: 0;
						left: 0;
						width: 100%;
						height: 100%;
						text-align: center;
						line-height: 1.85;
					}

				<?php 
            }
            ?>
			<?php 
        }
        ?>

			<?php 
        // Specific status icon styles
        ?>
			<?php 
        if (!empty($custom_status_icons)) {
            ?>
				<?php 
            foreach ($custom_status_icons as $status => $value) {
                ?>

					<?php 
                if (is_array($value)) {
                    ?>
						.widefat .column-order_status mark.<?php 
                    echo $status;
                    ?>
:after {
							font-family: "<?php 
                    echo $value['font'];
                    ?>
";
							content:     "<?php 
                    echo $value['glyph'];
                    ?>
";
						}
					<?php 
                } else {
                    ?>
						.widefat .column-order_status mark.<?php 
                    echo $status;
                    ?>
 {
							background-size: 100% 100%;
							background-image: url( <?php 
                    echo $value;
                    ?>
 );
						}
					<?php 
                }
                ?>

				<?php 
            }
            ?>
			<?php 
        }
        ?>

			<?php 
        // Specific status color styles
        ?>
			<?php 
        if (!empty($custom_status_colors)) {
            ?>
				<?php 
            foreach ($custom_status_colors as $status => $color) {
                ?>

					<?php 
                if (in_array($status, $custom_status_badges)) {
                    ?>
						.widefat .column-order_status mark.<?php 
                    echo $status;
                    ?>
 {
							background-color: <?php 
                    echo $color;
                    ?>
;
							color: <?php 
                    echo wc_order_status_manager()->icons->get_contrast_text_color($color);
                    ?>
;
						}
					<?php 
                }
                ?>

					<?php 
                if (isset($custom_status_icons[$status])) {
                    ?>
						.widefat .column-order_status mark.<?php 
                    echo $status;
                    ?>
:after {
							color: <?php 
                    echo $color;
                    ?>
;
						}
					<?php 
                }
                ?>

				<?php 
            }
            ?>
			<?php 
        }
        ?>

			<?php 
        // Specific  action icon styles
        ?>
			<?php 
        if (!empty($custom_action_icons)) {
            ?>
				<?php 
            foreach ($custom_action_icons as $status => $value) {
                ?>

					<?php 
                if (is_array($value)) {
                    ?>
						.order_actions .<?php 
                    echo $status;
                    ?>
:after {
							font-family: "<?php 
                    echo $value['font'];
                    ?>
";
							content:     "<?php 
                    echo $value['glyph'];
                    ?>
";
						}
					<?php 
                } else {
                    ?>
						.order_actions .<?php 
                    echo $status;
                    ?>
,
						.order_actions .<?php 
                    echo $status;
                    ?>
:focus,
						.order_actions .<?php 
                    echo $status;
                    ?>
:hover {
							background-size: 69% 69%;
							background-position: center center;
							background-repeat: no-repeat;
							background-image: url( <?php 
                    echo $value;
                    ?>
 );
						}
					<?php 
                }
                ?>

				<?php 
            }
            ?>
			<?php 
        }
        ?>

			/*]]>*/
		</style>
		<?php 
    }
 /**
  * Ensure orders with custom statuses are included in partial refund report caulculations
  *
  * @since 1.1.0
  * @param array $args
  * @return array $args
  */
 public function order_report_data_args($args)
 {
     // don't alter the order statuses if it's not an array or if 'refunded' is the only status
     if (!isset($args['parent_order_status']) || !is_array($args['parent_order_status']) || 1 === count($args['parent_order_status']) && 'refunded' === $args['parent_order_status'][0]) {
         return $args;
     }
     $order_statuses = new WC_Order_Status_Manager_Order_Statuses();
     $status_posts = $order_statuses->get_order_status_posts(array('fields' => 'ids'));
     foreach ($status_posts as $post_id) {
         $status = new WC_Order_Status_Manager_Order_Status($post_id);
         if ($status->include_in_reports()) {
             $args['parent_order_status'][] = $status->get_slug();
         } else {
             if (($key = array_search($status->get_slug(), $args['parent_order_status'])) !== false) {
                 unset($args['parent_order_status'][$key]);
             }
         }
     }
     // ensure parent order statuses are unique
     $args['parent_order_status'] = array_unique($args['parent_order_status']);
     return $args;
 }
 /**
  * Handle deleting an order status
  *
  * Will assign all orders that have the to-be deleted status
  * a replacement status, which defaults to `wc-on-hold`.
  * Also removes the status form any next statuses.
  *
  * @since 1.0.0
  * @param int $post_id the order status post id
  */
 public function handle_order_status_delete($post_id)
 {
     global $wpdb;
     // Bail out if not an order status or not published
     if ('wc_order_status' !== get_post_type($post_id) || 'publish' !== get_post_status($post_id)) {
         return;
     }
     $order_status = new WC_Order_Status_Manager_Order_Status($post_id);
     if (!$order_status->get_id()) {
         return;
     }
     /**
      * Filter the replacement status when an order status is deleted
      *
      * This filter is applied just before the order status is deleted,
      * but after the order status meta has already been deleted.
      *
      * @since 1.0.0
      *
      * @param string $replacement Replacement order status slug.
      * @param string $original Original order status slug.
      */
     $replacement_status = apply_filters('wc_order_status_manager_deleted_status_replacement', 'on-hold', $order_status->get_slug());
     $replacement_status = str_replace('wc-', '', $replacement_status);
     $old_status_name = $order_status->get_name();
     $order_rows = $wpdb->get_results($wpdb->prepare("\n\t\t\tSELECT ID FROM {$wpdb->posts}\n\t\t\tWHERE post_type = 'shop_order' AND post_status = %s\n\t\t", $order_status->get_slug(true)), ARRAY_A);
     $num_updated = 0;
     if (!empty($order_rows)) {
         foreach ($order_rows as $order_row) {
             $order = wc_get_order($order_row['ID']);
             $order->update_status($replacement_status, __("Order status updated because the previous status was deleted.", WC_Order_Status_Manager::TEXT_DOMAIN));
             $num_updated++;
         }
     }
     // If any other order statuses have specified this status
     // as a 'next status', remove it from there
     $rows = $wpdb->get_results($wpdb->prepare("\n\t\t\tSELECT pm.post_id\n\t\t\tFROM {$wpdb->postmeta} pm\n\t\t\tRIGHT JOIN {$wpdb->posts} p ON pm.post_id = p.ID\n\t\t\tWHERE post_type = 'wc_order_status'\n\t\t\tAND meta_key = '_next_statuses'\n\t\t\tAND meta_value LIKE %s\n\t\t", '%' . $wpdb->esc_like($order_status->get_slug()) . '%'));
     if ($rows) {
         foreach ($rows as $row) {
             $next_statuses = get_post_meta($row->post_id, '_next_statuses', true);
             // Remove the next status slug
             if (($key = array_search($order_status->get_slug(), $next_statuses)) !== false) {
                 unset($next_statuses[$key]);
             }
             update_post_meta($row->post_id, '_next_statuses', $next_statuses);
         }
     }
     // Add admin notice
     if ($num_updated && is_admin() && !defined('DOING_AJAX')) {
         $new_status = new WC_Order_Status_Manager_Order_Status($replacement_status);
         $message = sprintf(_n('%d order that was previously %s is now %s.', '%d orders that were previously %s are now %s.', $num_updated, WC_Order_Status_Manager::TEXT_DOMAIN), $num_updated, esc_html($old_status_name), esc_html($new_status->get_name()));
         wc_order_status_manager()->get_message_handler()->add_message($message);
     }
 }