autoship_load_all_products_summary_data_display_notice (function)

( src/product-page.php) Used to load Autoship Cloud data on the WP-Admin > Products > Edit Product page.

/**
* Load Autoship Cloud Data on Edit All Products Screen.
* TODO: Currently hooked into the admin notices but could try to find
*       better hook for early data load.
*/
function autoship_load_all_products_summary_data_display_notice (){

  // Get the Current Screen object and if doesn't exist escape.
  $screen = get_current_screen();
  if ( !$screen )
  return;

  if ( 'edit-product' !== $screen->id || 'edit' !== $screen->parent_base )
  return;

  // Allow devs to disable admin notice if desired.
  if( apply_filters('autoship_display_all_products_sync_admin_notice', false ) )
  return;

  // Since we're on the All Products screen tap into the current query to
  // get the current product ids so we can call the api with these ids.
  global $wp_query;
  $posts = $wp_query->posts;
  $query_ids = wp_list_pluck( $posts, 'ID' );
  $filter_count = count ( $query_ids );

  // Query these products to see if any have the active flag set.
  $products = wc_get_products( array( 'include' => $query_ids, 'limit' => $filter_count ) );

  // If No Products Bail.
  if ( empty( $products ) )
  return;

  // Check if global flag is set and if so all are active else
  $gobal_active = autoship_global_sync_active_enabled();

  $ids = array();

  foreach ($products as $key => $product) {

    if ( $gobal_active ){

      $ids[] = $product->get_id();
      // Check if we need to get all variations if this is a variable product.
      if ( 'variable' == $product->get_type() && $product->has_child() )
      $ids = array_merge( $ids, $product->get_children() );

    } else {

      // Since global isn't active get check product level
      if ( 'yes' === autoship_sync_active_enabled( $product ) ){
        $ids[] = $product->get_id();
        // Check if we need to get all variations if this is a variable product.
        if ( 'variable' == $product->get_type() && $product->has_child() )
        $ids = array_merge( $ids, $product->get_children() );
      }

    }

  }

  $summary_data = !empty( $ids ) ? autoship_get_all_products_sync_summary ( $ids, count( $ids ) ) : array();
  autoship_store_all_products_sync_summary_data( $summary_data );

  // Check for a general invalid flag
  $invalid_exists = autoship_check_site_settings_for_invalid_products();

  if ( !$invalid_exists )
  return;

  $products_report_url  = autoship_admin_products_page_url();

  // For now we're only displaying notice for errors
  $title  = __( 'Invalid Product(s)', 'autoship' );
  $status = 'error';
  $notice[] = apply_filters( 'autoship_all_products_admin_summary_metabox_notice', sprintf( __("<p>1 or more products activated for Autoship Cloud have become invalid.  Please visit <a href=\"%s\"><strong>Autoship Cloud Products</strong></a> to resolve this.</p>" ), $products_report_url ), $summary_data, $products );


  // Disabled this functionality so users don't see the details for now.
  if ( false ){

    $notices = array();
    if ( !empty( $summary_data ) && !is_wp_error( $summary_data ) ) {

      // Check if there are any invalids
      if ( $summary_data['totals']['TotalActiveInvalids'] ){

        $title  = sprintf( __( '%d Invalid Product(s)', 'autoship' ), $summary_data['totals']['TotalActiveInvalids'] );
        $status = 'error';

      // All Active but for some reason summary data isn't loaded.
      } else if ( $summary_data['totals']['TotalAutoshipActive'] ){

        $title  = sprintf( __( '%d Active Product(s)', 'autoship' ), $summary_data['totals']['TotalAutoshipActive'] );
        $status = 'active';

      // All Not Active
      } else if ( !$summary_data['totals']['TotalAutoshipActive'] ){

        $title  = __( 'No Active Product(s)', 'autoship' );
        $status = 'not-active';

      }

      $products_report_url = autoship_admin_products_page_url();

      // Add any error notices
      if ( $summary_data['totals']['TotalAutoshipActive'] && $summary_data['totals']['TotalActiveInvalids'] ){

        $notice[] = apply_filters( 'autoship_all_products_admin_summary_metabox_notice', sprintf( __("<p>A total of <strong>%d</strong> of the filtered Products and associated Variations are Active. <strong>However, %d of the Products or associated Variations filtered are currently Invalid and not synchronized correctly with QPilot.</strong>  Invalid products may not process correctly with Scheduled Orders.  For additional details please please edit the WooCommerce Product or view the <a href=\"%s\">Autoship Cloud > Products</a> report.</p>" ), $summary_data['totals']['TotalAutoshipActive'], $summary_data['totals']['TotalActiveInvalids'], $products_report_url ), $summary_data, $products );

      } else if ( $summary_data['totals']['TotalAutoshipActive'] ){

        $notice[] = apply_filters( 'autoship_all_products_admin_summary_metabox_notice', sprintf( __( '<p>There are currently <strong>%d Products</strong> actively synchronized with QPilot. Changes made in WooCommerce to these Products and/or any associated Variations will be synchronized with QPilot.</p>', "autoship" ), $summary_data['totals']['TotalAutoshipActive']), $summary_data, $products );

      } else if ( $summary_data['totals']['TotalActiveInvalids'] ){

        $notice[] = apply_filters( 'autoship_all_products_admin_summary_metabox_notice', sprintf( __("<p><strong>All %d of the Products and associated Variations filtered below that are Activated are currently not synchronized correctly with QPilot and may be Invalid.</strong>  Invalid products may not process correctly with Scheduled Orders.  For additional details please please edit the WooCommerce Product or view the <a href=\"%s\">Autoship Cloud > Products</a> report.</p>" ), $summary_data['totals']['TotalActiveInvalids'], $products_report_url ), $summary_data, $products );

      } else if ( !$summary_data['totals']['TotalAutoshipActive'] ){

        $notice[] = apply_filters( 'autoship_all_products_admin_summary_metabox_notice', sprintf( __( '<p>There are currently <strong>No Products</strong> actively synchronized with QPilot. Only changes made in WooCommerce to Active Products and/or any associated Variations will be synchronized with QPilot.</p>', "autoship" ), $summary_data['totals']['TotalAutoshipActive']), $summary_data, $products );

      }

      if ( $summary_data['totals']['QuantityScheduled'] ){

        $total_string = array();
        if ( $summary_data['totals']['TotalQuantityScheduledActive'] )
        $total_string[] = $summary_data['totals']['TotalQuantityScheduledActive'] . ' Active';

        if ( $summary_data['totals']['TotalQuantityScheduledPaused'] )
        $total_string[] = $summary_data['totals']['TotalQuantityScheduledPaused'] . ' Paused';

        if ( $summary_data['totals']['TotalQuantityFailed'] )
        $total_string[] = $summary_data['totals']['TotalQuantityFailed'] . ' Failed';

        if ( $summary_data['totals']['TotalQuantityProcessing'] )
        $total_string[] = $summary_data['totals']['TotalQuantityProcessing'] . ' Processing';

        $notice[] = apply_filters( 'autoship_all_products_admin_summary_metabox_subnotice', sprintf(
        __("<hr/><p>There are a total of <strong>%d Scheduled Orders ( %s )</strong> containing the Active Products and associated Variations filtered below. <br/><strong>Important!</strong> Changes made to product data that is already synchronized (i.e. changing Product Type, Published Status, Moving to Trash, Deleting Products, etc. ) may invalidate those products in QPilot and prevent them from processing with Scheduled Orders' </p>", 'autpship' ),
        $summary_data['totals']['QuantityScheduled'],
        implode(', ', $total_string ) ), $summary_data, $products );
      }

    } else {

      // Check for Total Sync Error
      if ( is_wp_error( $summary_data ) ){

        $title  = __( 'Error', 'autoship' );
        $status = 'error';
        $notice[] = apply_filters( 'autoship_all_products_admin_summary_metabox_notice', sprintf( "<p>%s</p>", $summary_data->get_error_message() ), $summary_data, $products );

      // No Active Filtered Products.
      } else if ( empty( $ids ) ){

        $title  = __( 'No Active Product(s)', 'autoship' );
        $status = 'not-active';

        $notice[] = apply_filters( 'autoship_all_products_admin_summary_metabox_notice', sprintf( __( '<p>There are currently <strong>No Products</strong> actively synchronized with QPilot. Only changes made in WooCommerce to Active Products and/or any associated Variations will be synchronized with QPilot.</p>', "autoship" ), 0 ), $summary_data, $products );

      // Check for missing data & possible QPilot outage
      } else if ( empty( $summary_data ) ){
        $api_health_url       = autoship_admin_settings_page_url();

        $title  = __( 'Not Available', 'autoship' );
        $status = 'error';
        $notice[] = apply_filters( 'autoship_all_products_admin_summary_metabox_notice', sprintf(  __( "<p>A problem was encountered while trying to retrieve Product Sync Information from QPilot. Please confirm your <a href=\"%s\">API connection is healthy</a> and setup correctly.", "autoship" ), $api_health_url ), $summary_data, $products );

      }

    }

  }

  ?>
  <div class="autoship-admin-notice notice autoship-<?php echo $status;?>-notice is-dismissible">
    <h2><?php echo sprintf( __( 'Autoship Status: <span class="%s">%s</span>', 'autoship' ), $status, $title ); ?></h2>
    <?php echo implode('', $notice ); ?>
    <?php do_action( 'autoship_all_products_admin_summary_metabox_notice', $status, $summary_data, $products ); ?>
  </div>
  <?php

}