PluginProbe
WebberZone Top 10 — Popular Posts / 4.3.2
WebberZone Top 10 — Popular Posts v4.3.2
4.5.1 4.5.0 4.4.3 4.4.2 4.4.1 4.4.0 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 trunk 1.0 1.0.1 1.1 1.2 1.3 1.4 1.4.1 1.5 1.5.1 1.5.2 1.5.3 1.6 1.6.1 All 117 releases
top-10 / includes / admin / class-admin-notices.php

class-admin-notices.php in WebberZone Top 10 — Popular Posts 4.3.2, at includes/admin/class-admin-notices.php

141 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 foreach ( $all_tables as $table => $label ) {
105 if ( ! Database::is_table_installed( $table ) ) {
106 $missing[] = sprintf( '<code>%s</code> (%s)', esc_html( $table ), esc_html( $label ) );
107 }
108 }
109
110 return $missing;
111 }
112
113 /**
114 * Build the missing-tables notice message HTML.
115 *
116 * @since 4.3.0
117 *
118 * @param array $missing Array of HTML-formatted missing table descriptions.
119 * @return string Notice HTML.
120 */
121 private function build_missing_table_message( array $missing ): string {
122 $create_url = wp_nonce_url(
123 admin_url( 'admin.php?action=tptn_create_missing_tables' ),
124 'tptn-create-missing-tables'
125 );
126
127 $items = '';
128 foreach ( $missing as $item ) {
129 $items .= '<li>' . wp_kses( $item, array( 'code' => array() ) ) . '</li>';
130 }
131
132 return sprintf(
133 '<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>',
134 esc_html__( 'Top 10: The following database tables are missing:', 'top-10' ),
135 $items,
136 esc_url( $create_url ),
137 esc_html__( 'Create missing tables', 'top-10' )
138 );
139 }
140 }
141