function zem_rp_update_related_posts_callback()
{
    check_ajax_referer('zem_rp_ajax_nonce');
    if (!current_user_can('edit_posts')) {
        die('error');
    }
    $options = zem_rp_get_options();
    if (!isset($_POST['related_posts']) || !isset($_POST['post_id'])) {
        die('error');
    }
    global $wpdb;
    $post_id = intval(stripslashes($_POST['post_id']));
    $articles_json = stripslashes($_POST['related_posts']);
    if ($articles_json) {
        $articles = json_decode($articles_json);
    } else {
        $articles = '';
    }
    update_post_meta($post_id, '_zem_rp_selected_related_posts', $articles);
    die('ok');
}
Example #2
0
function zem_rp_activate_hook()
{
    zem_rp_get_options();
    zem_rp_schedule_notifications_cron();
}
function zem_rp_fetch_random_posts($limit = 10, $exclude_ids = array())
{
    global $wpdb, $post;
    $options = zem_rp_get_options();
    $exclude_ids_str = zem_rp_get_exclude_ids_list_string($exclude_ids);
    $q1 = "SELECT ID FROM {$wpdb->posts} posts WHERE post_status = 'publish' AND post_type = 'post' AND ID NOT IN ({$exclude_ids_str})";
    if ($options['exclude_categories']) {
        $q1 .= " AND ID NOT IN (SELECT tr.object_id FROM {$wpdb->term_taxonomy} tt, {$wpdb->term_relationships} tr WHERE tt.taxonomy = 'category' AND tt.term_taxonomy_id = tr.term_taxonomy_id AND tt.term_id IN (" . $options['exclude_categories'] . "))";
    }
    if ($options['max_related_post_age_in_days'] > 0) {
        $q1 .= " AND post_date > DATE_SUB(CURDATE(), INTERVAL " . $options['max_related_post_age_in_days'] . " DAY)";
    }
    $ids = $wpdb->get_col($q1, 0);
    $count = count($ids);
    if ($count === 0) {
        return false;
        /*} else if($count === 1) {
        		$rnd = $ids;*/
    } else {
        if ($count > 1) {
            $display_number = min($limit, $count);
            $next_seed = rand();
            $t = time();
            $seed = $t - $t % 300 + $post->ID << 4;
            // We keep the same seed for 5 minutes, so MySQL can cache the `q2` query.
            srand($seed);
            shuffle($ids);
            $ids = array_slice($ids, 0, $display_number);
            srand($next_seed);
        }
    }
    $q2 = "SELECT ID, post_title, post_content, post_excerpt, post_date, comment_count FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type = 'post' AND ID IN (" . implode(',', $ids) . ")";
    $results = $wpdb->get_results($q2);
    asort($ids);
    $ids_keys = array_keys($ids);
    array_multisort($ids_keys, $results);
    return $results;
}
Example #4
0
function zem_rp_get_related_posts()
{
    if (zem_rp_should_exclude()) {
        return;
    }
    global $post, $zem_rp_is_first_widget;
    $options = zem_rp_get_options();
    $platform_options = zem_rp_get_platform_options();
    $meta = zem_rp_get_meta();
    $posts_and_title = zem_rp_fetch_posts_and_title();
    $related_posts = $posts_and_title['posts'];
    $title = $posts_and_title['title'];
    $selected_related_posts = zem_rp_get_selected_posts();
    $related_posts_content = "";
    if (!$related_posts) {
        return;
    }
    $posts_footer = '';
    if (current_user_can($options['only_admins_can_edit_related_posts'] ? 'manage_options' : 'edit_posts')) {
        $posts_footer .= '<div class="zem_rp_footer" style="text-align: right;"><a class="zem_rp_edit" href="#" id="zem_rp_edit_related_posts">' . __('Edit Related Posts', 'zemanta_related_posts') . '</a></div>';
    }
    if ($options['display_zemanta_linky']) {
        $posts_footer .= '<div class="zem_rp_footer" style="text-align: right;">' . '<a class="zem_rp_backlink" style="color: #999; font-size: 11px; text-decoration: none;" target="_blank" href="http://www.zemanta.com/?related-posts" rel="nofollow">Zemanta</a>' . '</div>';
    }
    $css_classes = 'related_post zem_rp';
    $css_classes_wrap = str_replace(array('.css', '-'), array('', '_'), esc_attr('zem_rp_th_' . $platform_options['theme_name']));
    if ($related_posts) {
        $related_posts_lis = zem_rp_generate_related_posts_list_items($related_posts, $selected_related_posts);
        $related_posts_ul = '<ul class="' . $css_classes . '">' . $related_posts_lis . '</ul>';
        $related_posts_content = $title ? '<h3 class="related_post_title">' . $title . '</h3>' : '';
        $related_posts_content .= $related_posts_ul;
    }
    $first_id_attr = '';
    if ($zem_rp_is_first_widget) {
        $zem_rp_is_first_widget = false;
        $first_id_attr = 'id="zem_rp_first"';
    }
    $output = '<div class="zem_rp_wrap ' . $css_classes_wrap . '" ' . $first_id_attr . '>' . '<div class="zem_rp_content">' . $related_posts_content . $posts_footer . '</div>' . '</div>';
    return "\n" . $output . "\n";
}
Example #5
0
function zem_rp_settings_page()
{
    if (!current_user_can('delete_users')) {
        die('Sorry, you don\'t have permissions to access this page.');
    }
    zem_rp_register();
    $options = zem_rp_get_options();
    $meta = zem_rp_get_meta();
    if ($meta['email'] && !$meta['subscribed']) {
        $sub_types = "activityreport,newsletter";
        if (!empty($options['subscription_types'])) {
            $sub_types = $options['subscription_types'];
        }
        zem_rp_subscribe($meta['email'], $sub_types);
    }
    $articles_count = zem_rp_article_count($meta['zemanta_api_key']);
    $articles_uploaded = false;
    if (isset($_GET['zem_upload_articles']) && !empty($meta['zemanta_api_key'])) {
        $articles_uploaded = zem_rp_upload_articles($meta['zemanta_api_key']);
    }
    if (isset($_GET['zem_global_notice']) && $_GET['zem_global_notice'] === '0') {
        $meta['global_notice'] = null;
        zem_rp_update_meta($meta);
    }
    $postdata = stripslashes_deep($_POST);
    if (sizeof($_POST)) {
        if (!isset($_POST['_zem_rp_nonce']) || !wp_verify_nonce($_POST['_zem_rp_nonce'], 'zem_rp_settings')) {
            die('Sorry, your nonce did not verify.');
        }
        $old_options = $options;
        $new_options = array('on_single_post' => isset($postdata['zem_rp_on_single_post']), 'max_related_posts' => isset($postdata['zem_rp_max_related_posts']) && is_numeric(trim($postdata['zem_rp_max_related_posts'])) ? intval(trim($postdata['zem_rp_max_related_posts'])) : 5, 'on_rss' => isset($postdata['zem_rp_on_rss']), 'related_posts_title' => isset($postdata['zem_rp_related_posts_title']) ? trim($postdata['zem_rp_related_posts_title']) : '', 'max_related_post_age_in_days' => isset($postdata['zem_rp_max_related_post_age_in_days']) && is_numeric(trim($postdata['zem_rp_max_related_post_age_in_days'])) ? intval(trim($postdata['zem_rp_max_related_post_age_in_days'])) : 0, 'custom_size_thumbnail_enabled' => isset($postdata['zem_rp_custom_size_thumbnail_enabled']) && $postdata['zem_rp_custom_size_thumbnail_enabled'] === 'yes', 'custom_thumbnail_width' => isset($postdata['zem_rp_custom_thumbnail_width']) ? intval(trim($postdata['zem_rp_custom_thumbnail_width'])) : ZEM_RP_CUSTOM_THUMBNAILS_WIDTH, 'custom_thumbnail_height' => isset($postdata['zem_rp_custom_thumbnail_height']) ? intval(trim($postdata['zem_rp_custom_thumbnail_height'])) : ZEM_RP_CUSTOM_THUMBNAILS_HEIGHT, 'thumbnail_use_custom' => isset($postdata['zem_rp_thumbnail_use_custom']), 'thumbnail_custom_field' => isset($postdata['zem_rp_thumbnail_custom_field']) ? trim($postdata['zem_rp_thumbnail_custom_field']) : '', 'display_zemanta_linky' => isset($postdata['zem_rp_display_zemanta_linky']), 'only_admins_can_edit_related_posts' => !empty($postdata['zem_rp_only_admins_can_edit_related_posts']), 'desktop' => array('display_thumbnail' => isset($postdata['zem_rp_desktop_display_thumbnail']), 'display_comment_count' => isset($postdata['zem_rp_desktop_display_comment_count']), 'display_publish_date' => isset($postdata['zem_rp_desktop_display_publish_date']), 'display_excerpt' => isset($postdata['zem_rp_desktop_display_excerpt']), 'excerpt_max_length' => isset($postdata['zem_rp_desktop_excerpt_max_length']) && is_numeric(trim($postdata['zem_rp_desktop_excerpt_max_length'])) ? intval(trim($postdata['zem_rp_desktop_excerpt_max_length'])) : 200, 'custom_theme_enabled' => isset($postdata['zem_rp_desktop_custom_theme_enabled'])));
        if (!isset($postdata['zem_rp_exclude_categories'])) {
            $new_options['exclude_categories'] = '';
        } else {
            if (is_array($postdata['zem_rp_exclude_categories'])) {
                $new_options['exclude_categories'] = join(',', $postdata['zem_rp_exclude_categories']);
            } else {
                $new_options['exclude_categories'] = trim($postdata['zem_rp_exclude_categories']);
            }
        }
        foreach (array('desktop') as $platform) {
            if (isset($postdata['zem_rp_' . $platform . '_theme_name'])) {
                // If this isn't set, maybe the AJAX didn't load...
                $new_options[$platform]['theme_name'] = trim($postdata['zem_rp_' . $platform . '_theme_name']);
                if (isset($postdata['zem_rp_' . $platform . '_theme_custom_css'])) {
                    $new_options[$platform]['theme_custom_css'] = $postdata['zem_rp_' . $platform . '_theme_custom_css'];
                } else {
                    $new_options[$platform]['theme_custom_css'] = '';
                }
            } else {
                $new_options[$platform]['theme_name'] = $old_options[$platform]['theme_name'];
                $new_options[$platform]['theme_custom_css'] = $old_options[$platform]['theme_custom_css'];
            }
        }
        if (isset($postdata['zem_classic_state'])) {
            $meta['classic_user'] = true;
        } else {
            $meta['classic_user'] = false;
        }
        zem_rp_update_meta($meta);
        $default_thumbnail_path = zem_rp_upload_default_thumbnail_file();
        if ($default_thumbnail_path === false) {
            // no file uploaded
            if (isset($postdata['zem_rp_default_thumbnail_remove'])) {
                $new_options['default_thumbnail_path'] = false;
            } else {
                $new_options['default_thumbnail_path'] = $old_options['default_thumbnail_path'];
            }
        } else {
            if (is_wp_error($default_thumbnail_path)) {
                // error while upload
                $new_options['default_thumbnail_path'] = $old_options['default_thumbnail_path'];
                zem_rp_add_admin_notice('error', $default_thumbnail_path->get_error_message());
            } else {
                // file successfully uploaded
                $new_options['default_thumbnail_path'] = $default_thumbnail_path;
            }
        }
        if ((array) $old_options != $new_options) {
            if (!zem_rp_update_options($new_options)) {
                zem_rp_add_admin_notice('error', __('Failed to save settings.', 'zemanta_related_posts'));
            } else {
                zem_rp_add_admin_notice('updated', __('Settings saved.', 'zemanta_related_posts'));
            }
        } else {
            // I should duplicate success message here
            zem_rp_add_admin_notice('updated', __('Settings saved.', 'zemanta_related_posts'));
        }
    }
    $settings_file = __FILE__;
    $blog_url = get_site_url();
    include zem_rp_get_template('settings');
}