|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Constant Contact + WooCommerce Health Panel |
| 4 | + * |
| 5 | + * Adds debugging information to the Site Health panel. |
| 6 | + * |
| 7 | + * @since 2.2.0 |
| 8 | + * @package cc-woo |
| 9 | + */ |
| 10 | + |
| 11 | +namespace WebDevStudios\CCForWoo\Utility; |
| 12 | + |
| 13 | +use WebDevStudios\CCForWoo\Meta\ConnectionStatus; |
| 14 | +use WebDevStudios\CCForWoo\Plugin; |
| 15 | +use WebDevStudios\CCForWoo\AbandonedCheckouts\CheckoutsTable; |
| 16 | + |
| 17 | +/** |
| 18 | + * Class HealthPanel |
| 19 | + * |
| 20 | + * @package WebDevStudios\CCForWoo\Utility |
| 21 | + * @since 2.2.0 |
| 22 | + */ |
| 23 | +class HealthPanel { |
| 24 | + |
| 25 | + public function __construct() { |
| 26 | + add_filter( 'debug_information', [ $this, 'health_information' ], 1 ); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Callback to add in our own cusotm site health information. |
| 31 | + * |
| 32 | + * @since 2.2.0 |
| 33 | + * |
| 34 | + * @throws Exception |
| 35 | + * |
| 36 | + * @param array $debug_info Array of debug info panels. |
| 37 | + * @return array |
| 38 | + */ |
| 39 | + public function health_information( $debug_info ) { |
| 40 | + |
| 41 | + $connection = new ConnectionStatus(); |
| 42 | + $debug_info['constant-contact-woocommerce'] = [ |
| 43 | + 'label' => esc_html__( 'Constant Contact + WooCommerce', 'constant-contact-woocommerce' ), |
| 44 | + 'description' => esc_html__( 'Debugging and troubleshooting information for support purposes', 'constant-contact-woocommerce' ), |
| 45 | + 'fields' => [ |
| 46 | + [ |
| 47 | + 'label' => esc_html__( 'Plugin version', 'constant-contact-woocommerce' ), |
| 48 | + 'value' => Plugin::PLUGIN_VERSION, |
| 49 | + ], |
| 50 | + [ |
| 51 | + 'label' => esc_html__( 'Connection status', 'constant-contact-woocommerce' ), |
| 52 | + 'value' => ( $connection->is_connected() ) |
| 53 | + ? esc_html__( 'Connected', 'constant-contact-woocommerce' ) |
| 54 | + : esc_html__( 'Disconnected', 'constant-contact-woocommerce' ) |
| 55 | + ], |
| 56 | + [ |
| 57 | + 'label' => esc_html__( 'Abandoned checkouts total pending items', 'constant-contact-woocommerce' ), |
| 58 | + 'value' => $this->abandoned_checkouts_count(), |
| 59 | + ], |
| 60 | + [ |
| 61 | + 'label' => esc_html__( 'Abandoned checkouts expiration cron status', 'constant-contact-woocommerce' ), |
| 62 | + 'value' => ( wp_next_scheduled( 'cc_woo_check_expired_checkouts' ) ) |
| 63 | + ? esc_html__( 'Scheduled', 'constant-contact-woocommerce' ) |
| 64 | + : esc_html__( 'Not scheduled', 'constant-contact-woocommerce' ), |
| 65 | + ], |
| 66 | + [ |
| 67 | + 'label' => esc_html__( 'Current user has a CC key', 'constant-contact-woocommerce' ), |
| 68 | + 'value' => ( $this->user_has_cc_key() ) |
| 69 | + ? esc_html__( 'True', 'constant-contact-woocommerce' ) |
| 70 | + : esc_html__( 'False', 'constant-contact-woocommerce' ), |
| 71 | + ], |
| 72 | + ] |
| 73 | + ]; |
| 74 | + |
| 75 | + return $debug_info; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Check if the current user has a Constant Contact API key. |
| 80 | + * |
| 81 | + * @since 2.2.0 |
| 82 | + * |
| 83 | + * @return bool |
| 84 | + */ |
| 85 | + private function user_has_cc_key(): bool { |
| 86 | + $user_id = get_current_user_id(); |
| 87 | + |
| 88 | + if ( ! $user_id ) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + $query = <<<SQL |
| 93 | +SELECT |
| 94 | + key_id |
| 95 | +FROM |
| 96 | +{$GLOBALS['wpdb']->prefix}woocommerce_api_keys |
| 97 | +WHERE |
| 98 | + user_id = %d |
| 99 | +AND |
| 100 | + ( |
| 101 | + description LIKE '%Constant Contact%' |
| 102 | + OR |
| 103 | + description LIKE '%ConstantContact%' |
| 104 | + ) |
| 105 | +SQL; |
| 106 | + |
| 107 | + return ! empty( $GLOBALS['wpdb']->get_col( $GLOBALS['wpdb']->prepare( $query, $user_id ) ) ); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Returns a count of total abandoned checkouts. |
| 112 | + * |
| 113 | + * @since 2.2.0 |
| 114 | + * |
| 115 | + * @return string |
| 116 | + */ |
| 117 | + private function abandoned_checkouts_count() : string { |
| 118 | + $table = CheckoutsTable::get_table_name(); |
| 119 | + $query = <<<SQL |
| 120 | +SELECT |
| 121 | + count(*) |
| 122 | +FROM |
| 123 | + {$table} |
| 124 | +SQL; |
| 125 | + $count = (string) $GLOBALS['wpdb']->get_var( $query ); |
| 126 | + return ( ! empty( $count ) ) ? $count : '0'; |
| 127 | + } |
| 128 | +} |
0 commit comments