Example #1
0
/**
 * Function generates the plugin settings page.
 *
 * @since	1.0
 */
function tptn_options()
{
    global $wpdb;
    $tptn_settings = tptn_read_options();
    /* Temporary check to remove the deprecated hook */
    if (wp_next_scheduled('ald_tptn_hook')) {
        wp_clear_scheduled_hook('ald_tptn_hook');
        tptn_enable_run($tptn_settings['cron_hour'], $tptn_settings['cron_min'], $tptn_settings['cron_recurrence']);
    }
    /*
    	Temporary check if default styles are off and left thumbnails are selected - will be eventually deprecated
    	// This is a mismatch, so we force it to no style */
    if (false == $tptn_settings['include_default_style'] && 'left_thumbs' == $tptn_settings['tptn_styles']) {
        $tptn_settings['tptn_styles'] = 'no_style';
        update_option('ald_tptn_settings', $tptn_settings);
    }
    if (true == $tptn_settings['include_default_style'] && 'left_thumbs' != $tptn_settings['tptn_styles']) {
        $tptn_settings['tptn_styles'] = 'left_thumbs';
        update_option('ald_tptn_settings', $tptn_settings);
    }
    /* Parse post types */
    parse_str($tptn_settings['post_types'], $post_types);
    $wp_post_types = get_post_types(array('public' => true));
    $posts_types_inc = array_intersect($wp_post_types, $post_types);
    /* Save options has been triggered */
    if (isset($_POST['tptn_save']) && check_admin_referer('tptn-plugin-settings')) {
        /* General options */
        $tptn_settings['activate_overall'] = isset($_POST['activate_overall']) ? true : false;
        $tptn_settings['activate_daily'] = isset($_POST['activate_daily']) ? true : false;
        $tptn_settings['cache'] = isset($_POST['cache']) ? true : false;
        $tptn_settings['cache_fix'] = isset($_POST['cache_fix']) ? true : false;
        $tptn_settings['external_tracker'] = isset($_POST['external_tracker']) ? true : false;
        $tptn_settings['daily_midnight'] = isset($_POST['daily_midnight']) ? true : false;
        $tptn_settings['daily_range'] = intval($_POST['daily_range']);
        $tptn_settings['hour_range'] = intval($_POST['hour_range']);
        $tptn_settings['uninstall_clean_options'] = isset($_POST['uninstall_clean_options']) ? true : false;
        $tptn_settings['uninstall_clean_tables'] = isset($_POST['uninstall_clean_tables']) ? true : false;
        $tptn_settings['show_metabox'] = isset($_POST['show_metabox']) ? true : false;
        $tptn_settings['show_metabox_admins'] = isset($_POST['show_metabox_admins']) ? true : false;
        $tptn_settings['show_credit'] = isset($_POST['show_credit']) ? true : false;
        /* Counter and tracker options */
        $tptn_settings['add_to_content'] = isset($_POST['add_to_content']) ? true : false;
        $tptn_settings['count_on_pages'] = isset($_POST['count_on_pages']) ? true : false;
        $tptn_settings['add_to_feed'] = isset($_POST['add_to_feed']) ? true : false;
        $tptn_settings['add_to_home'] = isset($_POST['add_to_home']) ? true : false;
        $tptn_settings['add_to_category_archives'] = isset($_POST['add_to_category_archives']) ? true : false;
        $tptn_settings['add_to_tag_archives'] = isset($_POST['add_to_tag_archives']) ? true : false;
        $tptn_settings['add_to_archives'] = isset($_POST['add_to_archives']) ? true : false;
        $tptn_settings['count_disp_form'] = $_POST['count_disp_form'];
        $tptn_settings['count_disp_form_zero'] = $_POST['count_disp_form_zero'];
        $tptn_settings['dynamic_post_count'] = isset($_POST['dynamic_post_count']) ? true : false;
        $tptn_settings['track_authors'] = isset($_POST['track_authors']) ? true : false;
        $tptn_settings['track_admins'] = isset($_POST['track_admins']) ? true : false;
        $tptn_settings['track_editors'] = isset($_POST['track_editors']) ? true : false;
        $tptn_settings['pv_in_admin'] = isset($_POST['pv_in_admin']) ? true : false;
        $tptn_settings['show_count_non_admins'] = isset($_POST['show_count_non_admins']) ? true : false;
        /* Popular post list options */
        $tptn_settings['limit'] = intval($_POST['limit']);
        $tptn_settings['how_old'] = intval($_POST['how_old']);
        // Process post types to be selected
        $wp_post_types = get_post_types(array('public' => true));
        $post_types_arr = isset($_POST['post_types']) && is_array($_POST['post_types']) ? $_POST['post_types'] : array('post' => 'post');
        $post_types = array_intersect($wp_post_types, $post_types_arr);
        $tptn_settings['post_types'] = http_build_query($post_types, '', '&');
        $tptn_settings['exclude_post_ids'] = $_POST['exclude_post_ids'] == '' ? '' : implode(',', array_map('intval', explode(',', $_POST['exclude_post_ids'])));
        /**** Exclude categories ****/
        $exclude_categories_slugs = array_map('trim', explode(',', wp_kses_post($_POST['exclude_cat_slugs'])));
        $tptn_settings['exclude_cat_slugs'] = implode(', ', $exclude_categories_slugs);
        foreach ($exclude_categories_slugs as $exclude_categories_slug) {
            $catObj = get_category_by_slug($exclude_categories_slug);
            if (isset($catObj->term_taxonomy_id)) {
                $exclude_categories[] = $catObj->term_taxonomy_id;
            }
        }
        $tptn_settings['exclude_categories'] = isset($exclude_categories) ? join(',', $exclude_categories) : '';
        $tptn_settings['title'] = wp_kses_post($_POST['title']);
        $tptn_settings['title_daily'] = wp_kses_post($_POST['title_daily']);
        $tptn_settings['blank_output'] = $_POST['blank_output'] == 'blank' ? true : false;
        $tptn_settings['blank_output_text'] = wp_kses_post($_POST['blank_output_text']);
        $tptn_settings['show_excerpt'] = isset($_POST['show_excerpt']) ? true : false;
        $tptn_settings['excerpt_length'] = intval($_POST['excerpt_length']);
        $tptn_settings['show_date'] = isset($_POST['show_date']) ? true : false;
        $tptn_settings['show_author'] = isset($_POST['show_author']) ? true : false;
        $tptn_settings['title_length'] = intval($_POST['title_length']);
        $tptn_settings['disp_list_count'] = isset($_POST['disp_list_count']) ? true : false;
        $tptn_settings['link_new_window'] = isset($_POST['link_new_window']) ? true : false;
        $tptn_settings['link_nofollow'] = isset($_POST['link_nofollow']) ? true : false;
        $tptn_settings['exclude_on_post_ids'] = $_POST['exclude_on_post_ids'] == '' ? '' : implode(',', array_map('intval', explode(',', $_POST['exclude_on_post_ids'])));
        // List HTML options
        $tptn_settings['before_list'] = $_POST['before_list'];
        $tptn_settings['after_list'] = $_POST['after_list'];
        $tptn_settings['before_list_item'] = $_POST['before_list_item'];
        $tptn_settings['after_list_item'] = $_POST['after_list_item'];
        /* Thumbnail options */
        $tptn_settings['post_thumb_op'] = $_POST['post_thumb_op'];
        $tptn_settings['thumb_size'] = $_POST['thumb_size'];
        $tptn_settings['thumb_width'] = intval($_POST['thumb_width']);
        $tptn_settings['thumb_height'] = intval($_POST['thumb_height']);
        $tptn_settings['thumb_crop'] = isset($_POST['thumb_crop']) ? true : false;
        $tptn_settings['thumb_html'] = $_POST['thumb_html'];
        $tptn_settings['thumb_meta'] = '' == $_POST['thumb_meta'] ? 'post-image' : $_POST['thumb_meta'];
        $tptn_settings['scan_images'] = isset($_POST['scan_images']) ? true : false;
        $tptn_settings['thumb_default_show'] = isset($_POST['thumb_default_show']) ? true : false;
        $tptn_settings['thumb_default'] = '' == $_POST['thumb_default'] || '/default.png' == $_POST['thumb_default'] ? TOP_TEN_PLUGIN_URL . '/default.png' : $_POST['thumb_default'];
        /* Styles */
        $tptn_settings['custom_CSS'] = wp_kses_post($_POST['custom_CSS']);
        $tptn_settings['tptn_styles'] = $_POST['tptn_styles'];
        if ('left_thumbs' == $tptn_settings['tptn_styles']) {
            $tptn_settings['include_default_style'] = true;
            $tptn_settings['post_thumb_op'] = 'inline';
        } elseif ('text_only' == $tptn_settings['tptn_styles']) {
            $tptn_settings['include_default_style'] = false;
            $tptn_settings['post_thumb_op'] = 'text_only';
        } else {
            $tptn_settings['include_default_style'] = false;
        }
        /**
         * Filter the settings array just before saving them to the database
         *
         * @since	2.0.4
         *
         * @param	array	$tptn_settings	Settings array
         */
        $tptn_settings = apply_filters('tptn_save_options', $tptn_settings);
        /* Update the options */
        update_option('ald_tptn_settings', $tptn_settings);
        /* Let's get the options again after we update them */
        $tptn_settings = tptn_read_options();
        parse_str($tptn_settings['post_types'], $post_types);
        $posts_types_inc = array_intersect($wp_post_types, $post_types);
        // Delete the cache
        tptn_cache_delete();
        /* Echo a success message */
        $str = '<div id="message" class="updated fade"><p>' . __('Options saved successfully. If enabled, the cache has been cleared.', 'top-10') . '</p>';
        if ('left_thumbs' == $tptn_settings['tptn_styles']) {
            $str .= '<p>' . __('Left Thumbnails style selected. Post thumbnail location set to Inline before text.', 'top-10') . '</p>';
        }
        if ('text_only' == $tptn_settings['tptn_styles']) {
            $str .= '<p>' . __('Text Only style selected. Thumbnails will not be displayed.', 'top-10') . '</p>';
        }
        if ('tptn_thumbnail' != $tptn_settings['thumb_size']) {
            $str .= '<p>' . sprintf(__('Pre-built thumbnail size selected. Thumbnail set to %d x %d.', 'top-10'), $tptn_settings['thumb_width'], $tptn_settings['thumb_height']) . '</p>';
        }
        $str .= '</div>';
        echo $str;
    }
    /* Default options has been triggered */
    if (isset($_POST['tptn_default']) && check_admin_referer('tptn-plugin-settings')) {
        delete_option('ald_tptn_settings');
        $tptn_settings = tptn_default_options();
        update_option('ald_tptn_settings', $tptn_settings);
        tptn_disable_run();
        $str = '<div id="message" class="updated fade"><p>' . __('Options set to Default.', 'top-10') . '</p></div>';
        echo $str;
    }
    /* Truncate overall posts table */
    if (isset($_POST['tptn_trunc_all']) && check_admin_referer('tptn-plugin-settings')) {
        tptn_trunc_count(false);
        $str = '<div id="message" class="updated fade"><p>' . __('Top 10 popular posts reset', 'top-10') . '</p></div>';
        echo $str;
    }
    /* Truncate daily posts table */
    if (isset($_POST['tptn_trunc_daily']) && check_admin_referer('tptn-plugin-settings')) {
        tptn_trunc_count(true);
        $str = '<div id="message" class="updated fade"><p>' . __('Top 10 daily popular posts reset', 'top-10') . '</p></div>';
        echo $str;
    }
    /* Clean duplicates */
    if (isset($_POST['tptn_clean_duplicates']) && check_admin_referer('tptn-plugin-settings')) {
        tptn_clean_duplicates(true);
        tptn_clean_duplicates(false);
        $str = '<div id="message" class="updated fade"><p>' . __('Duplicate rows cleaned from tables', 'top-10') . '</p></div>';
        echo $str;
    }
    /* Merge blog IDs */
    if (isset($_POST['tptn_merge_blogids']) && check_admin_referer('tptn-plugin-settings')) {
        tptn_merge_blogids(true);
        tptn_merge_blogids(false);
        $str = '<div id="message" class="updated fade"><p>' . __('Post counts across blog IDs 0 and 1 have been merged', 'top-10') . '</p></div>';
        echo $str;
    }
    /* Save maintenance options */
    if (isset($_POST['tptn_mnts_save']) && check_admin_referer('tptn-plugin-settings')) {
        $tptn_settings['cron_hour'] = min(23, intval($_POST['cron_hour']));
        $tptn_settings['cron_min'] = min(59, intval($_POST['cron_min']));
        $tptn_settings['cron_recurrence'] = $_POST['cron_recurrence'];
        if (isset($_POST['cron_on'])) {
            $tptn_settings['cron_on'] = true;
            tptn_enable_run($tptn_settings['cron_hour'], $tptn_settings['cron_min'], $tptn_settings['cron_recurrence']);
            $str = '<div id="message" class="updated fade"><p>' . __('Scheduled maintenance enabled / modified', 'top-10') . '</p></div>';
        } else {
            $tptn_settings['cron_on'] = false;
            tptn_disable_run();
            $str = '<div id="message" class="updated fade"><p>' . __('Scheduled maintenance disabled', 'top-10') . '</p></div>';
        }
        update_option('ald_tptn_settings', $tptn_settings);
        $tptn_settings = tptn_read_options();
        echo $str;
    }
    if (isset($_POST['tptn_import']) && check_admin_referer('tptn-plugin-settings')) {
        $top_ten_all_mu_tables = isset($_POST['top_ten_all_mu_tables']) ? $_POST['top_ten_all_mu_tables'] : array();
        $top_ten_mu_tables_blog_ids = explode(',', $_POST['top_ten_mu_tables_blog_ids']);
        $top_ten_mu_tables_sel_blog_ids = array_values($top_ten_all_mu_tables);
        foreach ($top_ten_mu_tables_sel_blog_ids as $top_ten_mu_tables_sel_blog_id) {
            $sql = '
                    INSERT INTO ' . $wpdb->base_prefix . "top_ten (postnumber, cntaccess, blog_id)\n\t\t\t\t\t  SELECT postnumber, cntaccess, '%d' FROM " . $wpdb->base_prefix . $top_ten_mu_tables_sel_blog_id . '_top_ten
                      ON DUPLICATE KEY UPDATE ' . $wpdb->base_prefix . 'top_ten.cntaccess = ' . $wpdb->base_prefix . 'top_ten.cntaccess + (
                        SELECT ' . $wpdb->base_prefix . $top_ten_mu_tables_sel_blog_id . '_top_ten.cntaccess FROM ' . $wpdb->base_prefix . $top_ten_mu_tables_sel_blog_id . '_top_ten WHERE ' . $wpdb->base_prefix . $top_ten_mu_tables_sel_blog_id . '_top_ten.postnumber = ' . $wpdb->base_prefix . 'top_ten.postnumber
                      )
                ';
            $wpdb->query($wpdb->prepare($sql, $top_ten_mu_tables_sel_blog_id));
            $sql = '
                    INSERT INTO ' . $wpdb->base_prefix . "top_ten_daily (postnumber, cntaccess, dp_date, blog_id)\n\t\t\t\t\t  SELECT postnumber, cntaccess, dp_date, '%d' FROM " . $wpdb->base_prefix . $top_ten_mu_tables_sel_blog_id . '_top_ten_daily
                      ON DUPLICATE KEY UPDATE ' . $wpdb->base_prefix . 'top_ten_daily.cntaccess = ' . $wpdb->base_prefix . 'top_ten_daily.cntaccess + (
                        SELECT ' . $wpdb->base_prefix . $top_ten_mu_tables_sel_blog_id . '_top_ten_daily.cntaccess FROM ' . $wpdb->base_prefix . $top_ten_mu_tables_sel_blog_id . '_top_ten_daily WHERE ' . $wpdb->base_prefix . $top_ten_mu_tables_sel_blog_id . '_top_ten_daily.postnumber = ' . $wpdb->base_prefix . 'top_ten_daily.postnumber
                      )
                ';
            $wpdb->query($wpdb->prepare($sql, $top_ten_mu_tables_sel_blog_id));
        }
        update_site_option('top_ten_mu_tables_sel_blog_ids', array_unique(array_merge($top_ten_mu_tables_sel_blog_ids, get_site_option('top_ten_mu_tables_sel_blog_ids', array()))));
        $str = '<div id="message" class="updated fade"><p>' . __('Counts from selected sites have been imported.', 'top-10') . '</p></div>';
        echo $str;
    }
    if ((isset($_POST['tptn_delete_selected_tables']) || isset($_POST['tptn_delete_imported_tables'])) && check_admin_referer('tptn-plugin-settings')) {
        $top_ten_all_mu_tables = isset($_POST['top_ten_all_mu_tables']) ? $_POST['top_ten_all_mu_tables'] : array();
        $top_ten_mu_tables_blog_ids = explode(',', $_POST['top_ten_mu_tables_blog_ids']);
        $top_ten_mu_tables_sel_blog_ids = array_values($top_ten_all_mu_tables);
        if (isset($_POST['tptn_delete_selected_tables'])) {
            $top_ten_mu_tables_sel_blog_ids = array_intersect($top_ten_mu_tables_sel_blog_ids, get_site_option('top_ten_mu_tables_sel_blog_ids', array()));
        } else {
            $top_ten_mu_tables_sel_blog_ids = get_site_option('top_ten_mu_tables_sel_blog_ids', array());
        }
        if (!empty($top_ten_mu_tables_sel_blog_ids)) {
            $sql = 'DROP TABLE ';
            foreach ($top_ten_mu_tables_sel_blog_ids as $top_ten_mu_tables_sel_blog_id) {
                $sql .= $wpdb->base_prefix . $top_ten_mu_tables_sel_blog_id . '_top_ten, ';
                $sql .= $wpdb->base_prefix . $top_ten_mu_tables_sel_blog_id . '_top_ten_daily, ';
            }
            $sql = substr($sql, 0, -2);
            $wpdb->query($sql);
            $str = '<div id="message" class="updated fade"><p>' . __('Selected tables have been deleted. Note that only imported tables have been deleted.', 'top-10') . '</p></div>';
            echo $str;
        }
    }
    /**** Include the views page ****/
    include_once 'main-view.php';
}
Example #2
0
/**
 * Function to clear the Top 10 Cache with Ajax.
 *
 * @since	2.2.0
 *
 */
function tptn_ajax_clearcache()
{
    tptn_cache_delete();
    exit(json_encode(array('success' => 1, 'message' => __('Top 10 cache has been cleared', 'tptn'))));
}