21600, // seconds. 'display' => __( 'Four time by Day', 'newstatpress' ), ); $schedules['weekly'] = array( 'interval' => 604800, 'display' => __( 'Once a Week', 'newstatpress' ), ); $schedules['monthly'] = array( 'interval' => 2635200, 'display' => __( 'Once a Month', 'newstatpress' ), ); return $schedules; } add_filter( 'cron_schedules', 'nsp_cron_intervals' ); /** * Calculate nsp_calculate_epoch_offset_time * * @param int $t1 time 1. * @param int $t2 time 2. * @param string $output_unit unit for output. * @return int delta */ function nsp_calculate_epoch_offset_time( $t1, $t2, $output_unit ) { // to complete with more output_unit. $offset_time_in_seconds = abs( $t1 - $t2 ); if ( 'day' === $output_unit ) { $offset_time = $offset_time_in_seconds / 86400; } if ( 'hour' === $output_unit ) { $offset_time = $offset_time_in_seconds / 3600; } else { $offset_time = $offset_time_in_seconds; } return $offset_time; } /** * Get the number of days since installation * * @return int the number */ function nsp_get_days_installed() { global $nsp_option_vars; $name = $nsp_option_vars['settings']['name']; $settings = get_option( $name ); $install_date = empty( $settings['install_date'] ) ? time() : $settings['install_date']; $num_days_inst = nsp_calculate_epoch_offset_time( $install_date, time(), 'day' ); if ( $num_days_inst < 1 ) { $num_days_inst = 1; } return $num_days_inst; } // --------------------------------------------------------------------------- // URL Functions // --------------------------------------------------------------------------- /** * Extract the feed from the given url * * @param string $url the url to parse. * @return the extracted url *************************************/ function nsp_extract_feed_from_url( $url ) { list($null,$q) = array_pad( explode( '?', $url, 2 ), 2, null ); if ( strpos( $q, '&' ) !== false ) { list($res,$null) = explode( '&', $q ); } else { $res = $q; } return $res; } /** * Get the url */ function nsp_get_url() { $url = nsp_connexion_is_ssl() ? 'https://' : 'http://'; if ( isset( $_SERVER['REQUEST_URI'] ) ) { $url .= NSP_SERVER_NAME . sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ); } return esc_url( $url ); } /** * Fix poorly formed URLs so as not to throw errors or cause problems * * @param string $url the url. * @param string $rem_frag the frag. * @param string $rem_query the query. * @param string $rev the rev. * @return $url the url */ function nsp_fix_url( $url, $rem_frag = false, $rem_query = false, $rev = false ) { $url = trim( $url ); /* Too many forward slashes or colons after http */ $url = preg_replace( '~^(https?)\:+/+~i', '$1://', $url ); /* Too many dots */ $url = preg_replace( '~\.+~i', '.', $url ); /* Too many slashes after the domain */ $url = preg_replace( '~([a-z0-9]+)/+([a-z0-9]+)~i', '$1/$2', $url ); /* Remove fragments */ if ( ! empty( $rem_frag ) && strpos( $url, '#' ) !== false ) { $url_arr = explode( '#', $url ); $url = $url_arr[0]; } /* Remove query string completely */ if ( ! empty( $rem_query ) && strpos( $url, '?' ) !== false ) { $url_arr = explode( '?', $url ); $url = $url_arr[0]; } /* Reverse */ if ( ! empty( $rev ) ) { $url = strrev( $url ); } return $url; } /** * Get query string array from URL * * @param string $url the url. */ function nsp_get_query_args( $url ) { if ( empty( $url ) ) { return array(); } $query_str = nsp_get_query_string( $url ); parse_str( $query_str, $args ); return $args; } /** * Get query string * * @param string $url the url. */ function nsp_get_query_string( $url ) { /*** * Get query string from URL * Filter URLs with nothing after http */ if ( empty( $url ) || preg_match( '~^https?\:*/*$~i', $url ) ) { return ''; } /* Fix poorly formed URLs so as not to throw errors when parsing */ $url = nsp_fix_url( $url ); /* NOW start parsing */ $parsed = @wp_parse_url( $url ); /* Filter URLs with no query string */ if ( empty( $parsed['query'] ) ) { return ''; } $query_str = $parsed['query']; return $query_str; } /** * Admin nag notices */ function nsp_admin_nag_notices() { global $current_user; $nag_notices = get_user_meta( $current_user->ID, 'newstatpress_nag_notices', true ); if ( ! empty( $nag_notices ) ) { $nid = $nag_notices['nid']; $style = $nag_notices['style']; $timenow = time(); $url = nsp_get_url(); $query_args = nsp_get_query_args( $url ); $query_str = '?' . http_build_query( array_merge( $query_args, array( 'newstatpress_hide_nag' => '1', 'nid' => $nid, ) ) ); $query_str_con = 'QUERYSTRING'; $notice = str_replace( array( $query_str_con ), array( $query_str ), $nag_notices['notice'] ); global $pagenow; $page_nsp = 0; if ( isset( $_GET['page'] ) ) { switch ( $_GET['page'] ) { case 'nsp-main': $page_nsp = 1; break; case 'nsp-details': $page_nsp = 1; break; case 'nsp-visits': $page_nsp = 1; break; case 'nsp-search': $page_nsp = 1; break; case 'nsp-tools': $page_nsp = 1; break; case 'nsp-options': $page_nsp = 1; break; case 'nsp-credits': $page_nsp = 1; break; default: $page_nsp = 0; break; } } // Display NSP box if user are in plugins page or nsp plugins. if ( ( 'n03' !== $nid && 1 === $page_nsp ) || ( 'n03' === $nid && 'plugins.php' === $pagenow ) ) { ?>
close'; echo '

' . esc_html__( 'NewStatPress News', 'newstatpress' ) . '

'; } echo wp_kses( $notice, array( 'a' => array( 'href' => array(), 'target' => array(), 'class' => array(), ), 'br' => array(), 'p' => array(), 'i' => array(), ) ); ?>
ID, 'newstatpress_nag_status', true ); if ( ! empty( $status['currentnag'] ) ) { add_action( 'admin_notices', 'nsp_admin_nag_notices' ); return; } if ( ! is_array( $status ) ) { $status = array(); update_user_meta( $current_user->ID, 'newstatpress_nag_status', $status ); } $timenow = time(); $num_days_inst = nsp_get_days_installed(); $votedate = 14; $donatedate = 90; $query_str_con = 'QUERYSTRING'; /* Notices (Positive Nags) */ if ( empty( $status['news'] ) ) { $nid = 'n03'; $style = 'updated '; $notice_text = '

' . __( 'Since the introduction of new method for speed up the display (needed for dashboard and overview), you must activate the external API in Options>Api page.', 'newstatpress' ) . '

'; $notice_text .= __( 'This major change was necessary in aim to improve the plugin for users with big database. In addition, it will allow to be more flexible about the graphical informations in next versions.', 'newstatpress' ) . '

'; $status['currentnag'] = true; $status['news'] = false; } if ( empty( $status['currentnag'] ) && ( empty( $status['lastnag'] ) || $status['lastnag'] <= $timenow - 1209600 ) ) { if ( empty( $status['vote'] ) && $num_days_inst >= $votedate ) { $nid = 'n01'; $style = 'notice'; $notice_text = '

' . __( 'It looks like you\'ve been using NewStatPress for a while now. That\'s great!', 'newstatpress' ) . '

'; $notice_text .= '

' . __( 'If you find this plugin useful, would you take a moment to give it a rating on WordPress.org?', 'newstatpress' ); $notice_text .= '
(' . __( 'NB: please open a ticket on the support page instead of adding it to your rating commentaries if you wish to report an issue with the plugin, it will be processed more quickly by the team.', 'newstatpress' ) . ')

'; $notice_text .= '' . __( 'Yes, I\'d like to rate it!', 'newstatpress' ) . ''; $notice_text .= '   '; $notice_text .= '' . __( 'I already did!', 'newstatpress' ) . ''; $status['currentnag'] = true; $status['vote'] = false; } elseif ( empty( $status['donate'] ) && $num_days_inst >= $donatedate ) { $nid = 'n02'; $style = 'notice'; $notice_text = '

' . __( 'You\'ve been using NewStatPress for several months now. We hope that means you like it and are finding it helpful.', 'newstatpress' ) . '

'; $notice_text .= '

' . __( 'NewStatPress is provided for free and maintained only on free time. If you like the plugin, consider a donation to help further its development', 'newstatpress' ) . '

'; $notice_text .= '' . __( 'Yes, I\'d like to donate!', 'newstatpress' ) . ''; $notice_text .= '   '; $notice_text .= '' . __( 'I already did!', 'newstatpress' ) . ''; $status['currentnag'] = true; $status['donate'] = false; } } if ( ! empty( $status['currentnag'] ) ) { add_action( 'admin_notices', 'nsp_admin_nag_notices' ); $new_nag_notice = array( 'nid' => $nid, 'style' => $style, 'notice' => $notice_text, ); update_user_meta( $current_user->ID, 'newstatpress_nag_notices', $new_nag_notice ); update_user_meta( $current_user->ID, 'newstatpress_nag_status', $status ); } } /** * Admin notices */ function nsp_admin_notices() { $admin_notices = get_option( 'newstatpress_admin_notices' ); if ( ! empty( $admin_notices ) ) { $style = $admin_notices['style']; $notice = $admin_notices['notice']; $query_str_con = 'QUERYSTRING'; echo '

' . esc_html( $notice ) . '

'; } delete_option( 'newstatpress_admin_notices' ); } add_action( 'admin_init', 'nsp_hide_nag_notices', -10 ); /** * Hide Nag notice */ function nsp_hide_nag_notices() { $ns_codes = array( 'n01' => 'vote', 'n02' => 'donate', 'n03' => 'news', ); if ( ! isset( $_GET['newstatpress_hide_nag'], $_GET['nid'], $ns_codes[ $_GET['nid'] ] ) || '1' !== $_GET['newstatpress_hide_nag'] ) { return; } global $current_user; $status = get_user_meta( $current_user->ID, 'newstatpress_nag_status', true ); $timenow = time(); $url = nsp_get_url(); $query_args = nsp_get_query_args( $url ); unset( $query_args['newstatpress_hide_nag'], $query_args['nid'] ); $query_str = http_build_query( $query_args ); if ( '' !== $query_str ) { $query_str = '?' . $query_str; } $redirect_url = nsp_fix_url( $url, true, true ) . $query_str; $status['currentnag'] = false; if ( 'n03' !== $_GET['nid'] ) { $status['lastnag'] = $timenow; } $status[ $ns_codes[ sanitize_text_field( wp_unslash( $_GET['nid'] ) ) ] ] = true; update_user_meta( $current_user->ID, 'newstatpress_nag_status', $status ); update_user_meta( $current_user->ID, 'newstatpress_nag_notices', array() ); wp_safe_redirect( $redirect_url ); exit; } // --------------------------------------------------------------------------- // OTHER Functions // --------------------------------------------------------------------------- /** * Show loading time */ function nsp_load_time() { echo "Page generated in " . esc_html( timer_stop( 0, 2 ) ) . 's ' . esc_html( get_num_queries() ) . ' SQL queries'; } /** * Display tabs of navigation bar for menu in page * * @param string $menu_tabs list of menu tabs. * @param string $current current tabs. * @param string $ref page reference. */ function nsp_display_tabs_navbar_for_menu_page( $menu_tabs, $current, $ref ) { echo '

'; echo ''; } // --------------------------------------------------------------------------- // TABLE Functions // --------------------------------------------------------------------------- /** * Show tables size * * @param string $table the table to use. */ function nsp_table_size( $table ) { global $wpdb; // use prepare. $res = $wpdb->get_results( $wpdb->prepare( 'SHOW TABLE STATUS LIKE %s', $table ) ); // db call ok; no-cache ok. foreach ( $res as $fstatus ) { $data_lenght = $fstatus->Data_length; // phpcs:ignore -- not in valid snake_case format: it is a DB field! $data_rows = $fstatus->Rows; // phpcs:ignore -- not in valid snake_case format: it is a DB field! } return number_format( ( $data_lenght / 1024 / 1024 ), 2, ',', ' ' ) . " Mb ($data_rows " . __( 'records', 'newstatpress' ) . ')'; } /** * Show tables size 2 * * @param string $table the table to use. */ function nsp_table_size2( $table ) { global $wpdb; // use prepare. $res = $wpdb->get_results( $wpdb->prepare( 'SHOW TABLE STATUS LIKE %s', $table ) ); // db call ok; no-cache ok. foreach ( $res as $fstatus ) { $data_lenght = $fstatus->Data_length; // phpcs:ignore -- not in valid snake_case format: it is a DB field! $data_rows = $fstatus->Rows; // phpcs:ignore -- not in valid snake_case format: it is a DB field! } return number_format( ( $data_lenght / 1024 / 1024 ), 2, ',', ' ' ) . ' ' . __( 'Mb', 'newstatpress' ); } /** * Show tables records * * @param string $table the table to use. */ function nsp_table_records( $table ) { global $wpdb; // use prepare. $res = $wpdb->get_results( $wpdb->prepare( 'SHOW TABLE STATUS LIKE %s', $table ) ); // db call ok; no-cache ok. foreach ( $res as $fstatus ) { $data_lenght = $fstatus->Data_length; // phpcs:ignore -- not in valid snake_case format: it is a DB field! $data_rows = $fstatus->Rows; // phpcs:ignore -- not in valid snake_case format: it is a DB field! } return $data_rows; } ?>