parent_id = add_menu_page( esc_html__( 'Top 10 Dashboard', 'top-10' ), esc_html__( 'Top 10', 'top-10' ), 'manage_options', 'tptn_dashboard', array( $this, 'plugin_settings_page' ), 'dashicons-editor-ol' );
add_submenu_page( 'tptn_dashboard', esc_html__( 'Top 10 Dashboard', 'top-10' ), esc_html__( 'Dashboard', 'top-10' ), 'manage_options', 'tptn_dashboard', array( $this, 'plugin_settings_page' ) );
add_action( 'load-' . $this->parent_id, array( $this, 'help_tabs' ) );
}
/**
* Enqueue scripts in admin area.
*
* @since 3.0.0
*
* @param string $hook The current admin page.
*/
public function admin_enqueue_scripts( $hook ) {
wp_register_script( 'top-ten-moment-js', TOP_TEN_PLUGIN_URL . 'includes/admin/js/moment.min.js', array(), '1.0', true );
wp_register_script( 'top-ten-chart-js', TOP_TEN_PLUGIN_URL . 'includes/admin/js/chart.min.js', array(), '1.0', true );
wp_register_script( 'top-ten-chart-datalabels-js', TOP_TEN_PLUGIN_URL . 'includes/admin/js/chartjs-plugin-datalabels.min.js', array( 'top-ten-chart-js' ), '1.0', true );
wp_register_script( 'top-ten-chartjs-adapter-moment-js', TOP_TEN_PLUGIN_URL . 'includes/admin/js/chartjs-adapter-moment.min.js', array( 'top-ten-moment-js', 'top-ten-chart-js' ), '1.0', true );
wp_register_script( 'top-ten-chart-data-js', TOP_TEN_PLUGIN_URL . 'includes/admin/js/chart-data.min.js', array( 'jquery', 'top-ten-chart-js', 'top-ten-chart-datalabels-js', 'top-ten-moment-js', 'top-ten-chartjs-adapter-moment-js' ), '1.0', true );
wp_register_script( 'top-ten-admin-js', TOP_TEN_PLUGIN_URL . 'includes/admin/js/admin-scripts.min.js', array( 'jquery', 'jquery-ui-tabs', 'jquery-ui-datepicker' ), '1.0', true );
if ( $hook === $this->parent_id ) {
wp_enqueue_script( 'top-ten-chart-js' );
wp_enqueue_script( 'top-ten-chart-datalabels-js' );
wp_enqueue_script( 'top-ten-moment-js' );
wp_enqueue_script( 'top-ten-chartjs-adapter-moment-js' );
wp_enqueue_script( 'top-ten-chart-data-js' );
wp_enqueue_script( 'top-ten-admin-js' );
wp_enqueue_style(
'tptn-admin-ui-css',
TOP_TEN_PLUGIN_URL . 'includes/admin/css/top-10-admin.min.css',
false,
'1.0',
false
);
wp_localize_script(
'top-ten-chart-data-js',
'tptn_chart_data',
array(
'security' => wp_create_nonce( 'tptn-dashboard' ),
'datasetlabel' => __( 'Visits', 'top-10' ),
'charttitle' => __( 'Daily Visits', 'top-10' ),
)
);
}
}
/**
* Function to add an action to search for tags using Ajax.
*
* @since 3.0.0
*/
public function get_chart_data() {
global $wpdb;
check_ajax_referer( 'tptn-dashboard', 'security' );
$blog_id = get_current_blog_id();
// Add date selector.
$to_date = isset( $_REQUEST['to_date'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['to_date'] ) ) : current_time( 'd M Y' );
$from_date = isset( $_REQUEST['from_date'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['from_date'] ) ) : gmdate( 'd M Y', strtotime( '-1 week' ) );
$post_date_from = gmdate( 'Y-m-d', strtotime( $from_date ) );
$post_date_to = gmdate( 'Y-m-d', strtotime( $to_date ) );
$sql = $wpdb->prepare(
" SELECT SUM(cntaccess) AS visits, DATE(dp_date) as date
FROM {$wpdb->base_prefix}top_ten_daily
WHERE DATE(dp_date) >= DATE(%s)
AND DATE(dp_date) <= DATE(%s)
AND blog_id = %d
GROUP BY date
ORDER BY date ASC
",
$post_date_from,
$post_date_to,
$blog_id
);
$result = $wpdb->get_results( $sql ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared
$data = array();
foreach ( $result as $row ) {
$data[] = $row;
}
echo wp_json_encode( $data );
wp_die();
}
/**
* Array containing the settings' sections.
*
* @since 3.0.0
*
* @return array Settings array
*/
public function get_tabs() {
$tabs = array(
'today' => array(
'title' => __( 'Today', 'top-10' ),
'from_date' => current_time( 'd M Y' ),
'to_date' => current_time( 'd M Y' ),
),
'yesterday' => array(
'title' => __( 'Yesterday', 'top-10' ),
'from_date' => gmdate( 'd M Y', strtotime( '-1 day' ) ),
'to_date' => gmdate( 'd M Y', strtotime( '-1 day' ) ),
),
'lastweek' => array(
'title' => __( 'Last 7 days', 'top-10' ),
'from_date' => gmdate( 'd M Y', strtotime( '-1 week' ) ),
'to_date' => current_time( 'd M Y' ),
),
'lastmonth' => array(
'title' => __( 'Last 30 days', 'top-10' ),
'from_date' => gmdate( 'd M Y', strtotime( '-30 days' ) ),
'to_date' => current_time( 'd M Y' ),
),
'overall' => array(
'title' => __( 'All time', 'top-10' ),
'daily' => false,
),
);
return $tabs;
}
/**
* Get popular posts for a date range.
*
* @since 3.0.0
*
* @param string|array $args {
* Optional. Array or string of Query parameters.
*
* @type bool $daily Set to true to get the daily/custom period posts. False for overall.
* @type string $from_date From date. A date/time string.
* @type int $numberposts Number of posts to fetch.
* @type string $to_date To date. A date/time string.
* }
* @return string HTML table with popular posts.
*/
public function display_popular_posts( $args = array() ) {
$output = '';
$defaults = array(
'daily' => true,
'from_date' => null,
'numberposts' => 20,
'to_date' => null,
);
$args = wp_parse_args( $args, $defaults );
$results = $this->get_popular_posts( $args );
ob_start();
if ( $results ) :
?>
visits );
$result = get_post( $result->ID );
?>
|
|
get_current_blog_id(),
'daily' => true,
'from_date' => null,
'numberposts' => 20,
'offset' => 0,
'to_date' => null,
);
$args = wp_parse_args( $args, $defaults );
if ( $args['daily'] ) {
$table_name = $wpdb->base_prefix . 'top_ten_daily';
} else {
$table_name = $wpdb->base_prefix . 'top_ten';
}
// Fields to return.
$fields[] = ( $args['daily'] ) ? "SUM({$table_name}.cntaccess) as visits" : "{$table_name}.cntaccess as visits";
$fields[] = "{$wpdb->posts}.ID";
$fields = implode( ', ', $fields );
// Create the JOIN clause.
$join = " INNER JOIN {$wpdb->posts} ON {$table_name}.postnumber={$wpdb->posts}.ID ";
// Create the base WHERE clause.
$where = $wpdb->prepare( " AND {$table_name}.blog_id = %d ", $args['blog_id'] ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$where .= " AND ($wpdb->posts.post_status = 'publish' OR $wpdb->posts.post_status = 'inherit') "; // Show published posts and attachments.
$where .= " AND ($wpdb->posts.post_type <> 'revision' ) "; // No revisions.
if ( isset( $args['from_date'] ) ) {
$from_date = gmdate( 'Y-m-d', strtotime( $args['from_date'] ) );
$where .= $wpdb->prepare( " AND DATE({$table_name}.dp_date) >= DATE(%s) ", $from_date ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
}
if ( isset( $args['to_date'] ) ) {
$to_date = gmdate( 'Y-m-d', strtotime( $args['to_date'] ) );
$where .= $wpdb->prepare( " AND DATE({$table_name}.dp_date) <= DATE(%s) ", $to_date ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
}
// Create the base GROUP BY clause.
if ( $args['daily'] ) {
$groupby = " {$wpdb->posts}.ID";
}
// Create the base ORDER BY clause.
$orderby = ' visits DESC ';
// Create the base LIMITS clause.
$limits = $wpdb->prepare( ' LIMIT %d, %d ', $args['offset'], $args['numberposts'] );
if ( ! empty( $groupby ) ) {
$groupby = " GROUP BY {$groupby} ";
}
if ( ! empty( $orderby ) ) {
$orderby = " ORDER BY {$orderby} ";
}
$sql = "SELECT DISTINCT $fields FROM {$table_name} $join WHERE 1=1 $where $groupby $orderby $limits";
$result = $wpdb->get_results( $sql ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared
return $result;
}
/**
* Generates the help tabs.
*
* @since 3.0.0
*/
public function help_tabs() {
$screen = get_current_screen();
$screen->set_help_sidebar(
/* translators: 1: Support link. */
'' . sprintf( __( 'For more information or how to get support visit the WebberZone support site.', 'top-10' ), esc_url( 'https://webberzone.com/support/' ) ) . '
' .
/* translators: 1: Forum link. */
'' . sprintf( __( 'Support queries should be posted in the WordPress.org support forums.', 'top-10' ), esc_url( 'https://wordpress.org/support/plugin/top-10' ) ) . '
' .
'' . sprintf(
/* translators: 1: Github Issues link, 2: Github page. */
__( 'Post an issue on GitHub (bug reports only).', 'top-10' ),
esc_url( 'https://github.com/WebberZone/top-10/issues' ),
esc_url( 'https://github.com/WebberZone/top-10' )
) . '
'
);
$screen->add_help_tab(
array(
'id' => 'tptn-dashboard-general',
'title' => __( 'General', 'top-10' ),
'content' =>
'' . __( 'This screen displays the historical traffic on your site.', 'top-10' ) . '
' .
/* translators: 1: Constant holding number of days data is stored. */
'' . sprintf( __( 'The data is pulled from the daily tables in the database. If you have enabled maintenance then the amount of historical data that is available will be limited to %d days.', 'top-10' ), TOP_TEN_STORE_DATA ) . '
' .
'' . __( 'You can change this by setting the constant TOP_TEN_STORE_DATA to the number of days of your choice in your wp-config.php.', 'top-10' ) . '
',
)
);
}
}
/**
* Function to initialise stats page.
*
* @since 3.0.0
*/
function tptn_load_dashboard() {
Top_Ten_Dashboard::get_instance();
}
add_action( 'plugins_loaded', 'tptn_load_dashboard' );