| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin Notices class. |
| 4 |
* |
| 5 |
* @package WebberZone\Top_Ten\Admin |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WebberZone\Top_Ten\Admin; |
| 9 |
|
| 10 |
use WebberZone\Top_Ten\Database; |
| 11 |
use WebberZone\Top_Ten\Util\Hook_Registry; |
| 12 |
use function WebberZone\Top_Ten\wz_top_ten; |
| 13 |
|
| 14 |
if ( ! defined( 'WPINC' ) ) { |
| 15 |
die; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Admin Notices Class. |
| 20 |
* |
| 21 |
* @since 4.1.0 |
| 22 |
*/ |
| 23 |
class Admin_Notices { |
| 24 |
|
| 25 |
/** |
| 26 |
* Constructor class. |
| 27 |
* |
| 28 |
* @since 4.1.0 |
| 29 |
*/ |
| 30 |
public function __construct() { |
| 31 |
Hook_Registry::add_action( 'admin_init', array( $this, 'register_notices' ) ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Register notices with the Admin_Notices_API. |
| 36 |
* |
| 37 |
* @since 4.3.0 |
| 38 |
*/ |
| 39 |
public function register_notices() { |
| 40 |
$admin_notices_api = wz_top_ten()->admin->admin_notices_api ?? null; |
| 41 |
if ( ! $admin_notices_api ) { |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
// Result notice after running the create-missing-tables action. |
| 46 |
if ( isset( $_GET['tptn_tables_created'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 47 |
$status = sanitize_key( wp_unslash( $_GET['tptn_tables_created'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 48 |
|
| 49 |
if ( 'created' === $status ) { |
| 50 |
$admin_notices_api->register_notice( |
| 51 |
array( |
| 52 |
'id' => 'tptn_tables_created', |
| 53 |
'message' => '<p>' . esc_html__( 'Top 10: Missing database tables were created successfully.', 'top-10' ) . '</p>', |
| 54 |
'type' => 'success', |
| 55 |
'dismissible' => true, |
| 56 |
'capability' => 'manage_options', |
| 57 |
) |
| 58 |
); |
| 59 |
} elseif ( 'failed' === $status ) { |
| 60 |
$admin_notices_api->register_notice( |
| 61 |
array( |
| 62 |
'id' => 'tptn_tables_create_failed', |
| 63 |
'message' => '<p>' . esc_html__( 'Top 10: One or more database tables could not be created. Please check your database permissions or try the Recreate tables option on the Tools page.', 'top-10' ) . '</p>', |
| 64 |
'type' => 'error', |
| 65 |
'dismissible' => true, |
| 66 |
'capability' => 'manage_options', |
| 67 |
) |
| 68 |
); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
$missing = $this->get_missing_tables(); |
| 73 |
if ( empty( $missing ) ) { |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
$admin_notices_api->register_notice( |
| 78 |
array( |
| 79 |
'id' => 'tptn_missing_tables', |
| 80 |
'message' => $this->build_missing_table_message( $missing ), |
| 81 |
'type' => 'warning', |
| 82 |
'dismissible' => false, |
| 83 |
'capability' => 'manage_options', |
| 84 |
) |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Get the list of missing Top 10 tables. |
| 90 |
* |
| 91 |
* @since 4.3.0 |
| 92 |
* |
| 93 |
* @return array Array of HTML-formatted missing table descriptions. |
| 94 |
*/ |
| 95 |
private function get_missing_tables(): array { |
| 96 |
$all_tables = array( |
| 97 |
Database::get_table( false ) => __( 'Top 10 (total counts)', 'top-10' ), |
| 98 |
Database::get_table( true ) => __( 'Top 10 Daily (daily counts)', 'top-10' ), |
| 99 |
Database::get_log_table() => __( 'Visits Log', 'top-10' ), |
| 100 |
Database::get_funnel_table() => __( 'Visits Funnel', 'top-10' ), |
| 101 |
); |
| 102 |
|
| 103 |
$missing = array(); |
| 104 |
$statuses = Database::get_table_installation_status(); |
| 105 |
foreach ( $all_tables as $table => $label ) { |
| 106 |
if ( empty( $statuses[ $table ] ) ) { |
| 107 |
$missing[] = sprintf( '<code>%s</code> (%s)', esc_html( $table ), esc_html( $label ) ); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
return $missing; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Build the missing-tables notice message HTML. |
| 116 |
* |
| 117 |
* @since 4.3.0 |
| 118 |
* |
| 119 |
* @param array $missing Array of HTML-formatted missing table descriptions. |
| 120 |
* @return string Notice HTML. |
| 121 |
*/ |
| 122 |
private function build_missing_table_message( array $missing ): string { |
| 123 |
$create_url = wp_nonce_url( |
| 124 |
admin_url( 'admin.php?action=tptn_create_missing_tables' ), |
| 125 |
'tptn-create-missing-tables' |
| 126 |
); |
| 127 |
|
| 128 |
$items = ''; |
| 129 |
foreach ( $missing as $item ) { |
| 130 |
$items .= '<li>' . wp_kses( $item, array( 'code' => array() ) ) . '</li>'; |
| 131 |
} |
| 132 |
|
| 133 |
return sprintf( |
| 134 |
'<p><strong>%1$s</strong></p><ul style="list-style:disc;padding-left:2em;margin:0 0 .5em;">%2$s</ul><p><a href="%3$s" class="button button-primary">%4$s</a></p>', |
| 135 |
esc_html__( 'Top 10: The following database tables are missing:', 'top-10' ), |
| 136 |
$items, |
| 137 |
esc_url( $create_url ), |
| 138 |
esc_html__( 'Create missing tables', 'top-10' ) |
| 139 |
); |
| 140 |
} |
| 141 |
} |
| 142 |
|