function wp_rp_settings_page()
{
    if (!current_user_can('delete_users')) {
        die('Sorry, you don\'t have permissions to access this page.');
    }
    wp_rp_register();
    $options = wp_rp_get_options();
    $meta = wp_rp_get_meta();
    // Update already subscribed but in the old pipeline
    if (!empty($meta["email"]) && empty($meta["subscribed"])) {
        wp_rp_subscription($meta["email"], $options["subscription_types"]);
    }
    if (isset($_GET['wprp_global_notice']) && $_GET['wprp_global_notice'] === '0') {
        $meta['global_notice'] = null;
        wp_rp_update_meta($meta);
    }
    $postdata = stripslashes_deep($_POST);
    if (sizeof($_POST)) {
        if (!isset($_POST['_wp_rp_nonce']) || !wp_verify_nonce($_POST['_wp_rp_nonce'], 'wp_rp_settings')) {
            die('Sorry, your nonce did not verify.');
        }
        $old_options = $options;
        $new_options = array('on_single_post' => isset($postdata['wp_rp_on_single_post']), 'max_related_posts' => isset($postdata['wp_rp_max_related_posts']) && is_numeric(trim($postdata['wp_rp_max_related_posts'])) ? intval(trim($postdata['wp_rp_max_related_posts'])) : 5, 'on_rss' => isset($postdata['wp_rp_on_rss']), 'related_posts_title' => isset($postdata['wp_rp_related_posts_title']) ? trim($postdata['wp_rp_related_posts_title']) : '', 'promoted_content_enabled' => isset($postdata['wp_rp_promoted_content_enabled']), 'enable_themes' => isset($postdata['wp_rp_enable_themes']), 'max_related_post_age_in_days' => isset($postdata['wp_rp_max_related_post_age_in_days']) && is_numeric(trim($postdata['wp_rp_max_related_post_age_in_days'])) ? intval(trim($postdata['wp_rp_max_related_post_age_in_days'])) : 0, 'custom_size_thumbnail_enabled' => isset($postdata['wp_rp_custom_size_thumbnail_enabled']) && $postdata['wp_rp_custom_size_thumbnail_enabled'] === 'yes', 'custom_thumbnail_width' => isset($postdata['wp_rp_custom_thumbnail_width']) ? intval(trim($postdata['wp_rp_custom_thumbnail_width'])) : WP_RP_CUSTOM_THUMBNAILS_WIDTH, 'custom_thumbnail_height' => isset($postdata['wp_rp_custom_thumbnail_height']) ? intval(trim($postdata['wp_rp_custom_thumbnail_height'])) : WP_RP_CUSTOM_THUMBNAILS_HEIGHT, 'thumbnail_use_custom' => isset($postdata['wp_rp_thumbnail_use_custom']) && $postdata['wp_rp_thumbnail_use_custom'] === 'yes', 'thumbnail_custom_field' => isset($postdata['wp_rp_thumbnail_custom_field']) ? trim($postdata['wp_rp_thumbnail_custom_field']) : '', 'display_zemanta_linky' => $meta['show_zemanta_linky_option'] ? isset($postdata['wp_rp_display_zemanta_linky']) : true, 'only_admins_can_edit_related_posts' => !empty($postdata['wp_rp_only_admins_can_edit_related_posts']), 'desktop' => array('display_comment_count' => isset($postdata['wp_rp_desktop_display_comment_count']), 'display_publish_date' => isset($postdata['wp_rp_desktop_display_publish_date']), 'display_excerpt' => isset($postdata['wp_rp_desktop_display_excerpt']), 'display_thumbnail' => isset($postdata['wp_rp_desktop_display_thumbnail']), 'excerpt_max_length' => isset($postdata['wp_rp_desktop_excerpt_max_length']) && is_numeric(trim($postdata['wp_rp_desktop_excerpt_max_length'])) ? intval(trim($postdata['wp_rp_desktop_excerpt_max_length'])) : 200, 'custom_theme_enabled' => isset($postdata['wp_rp_desktop_custom_theme_enabled'])));
        if (!isset($postdata['wp_rp_exclude_categories'])) {
            $new_options['exclude_categories'] = '';
        } else {
            if (is_array($postdata['wp_rp_exclude_categories'])) {
                $new_options['exclude_categories'] = join(',', $postdata['wp_rp_exclude_categories']);
            } else {
                $new_options['exclude_categories'] = trim($postdata['wp_rp_exclude_categories']);
            }
        }
        foreach (array('desktop') as $platform) {
            if (isset($postdata['wp_rp_' . $platform . '_theme_name'])) {
                // If this isn't set, maybe the AJAX didn't load...
                $new_options[$platform]['theme_name'] = trim($postdata['wp_rp_' . $platform . '_theme_name']);
            } else {
                $new_options[$platform]['theme_name'] = $old_options[$platform]['theme_name'];
            }
            if (isset($postdata['wp_rp_' . $platform . '_theme_custom_css'])) {
                $new_options[$platform]['theme_custom_css'] = $postdata['wp_rp_' . $platform . '_theme_custom_css'];
            } elseif (isset($postdata["wp_rp_{$platform}_custom_theme_enabled"])) {
                $new_options[$platform]['theme_custom_css'] = '';
            } else {
                $new_options[$platform]['theme_custom_css'] = $old_options[$platform]['theme_custom_css'];
            }
        }
        if (isset($postdata['wp_rp_classic_state'])) {
            $meta['classic_user'] = true;
        } else {
            $meta['classic_user'] = false;
        }
        wp_rp_update_meta($meta);
        if (isset($postdata['wp_rp_turn_on_button_pressed'])) {
            $meta['show_turn_on_button'] = false;
            $meta['turn_on_button_pressed'] = $postdata['wp_rp_turn_on_button_pressed'];
            $new_options['desktop']['display_thumbnail'] = true;
        }
        $preprocess_thumbnails = $new_options['desktop']['display_thumbnail'] && !$old_options['desktop']['display_thumbnail'];
        $default_thumbnail_path = wp_rp_upload_default_thumbnail_file();
        if ($default_thumbnail_path === false) {
            // no file uploaded
            if (isset($postdata['wp_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'];
                wp_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 (!wp_rp_update_options($new_options)) {
                wp_rp_add_admin_notice('error', __('Failed to save settings.', 'wp_related_posts'));
            } else {
                wp_rp_add_admin_notice('updated', __('Settings saved.', 'wp_related_posts'));
            }
            if ($preprocess_thumbnails) {
                wp_rp_process_latest_post_thumbnails();
            }
        } else {
            // I should duplicate success message here
            wp_rp_add_admin_notice('updated', __('Settings saved.', 'wp_related_posts'));
        }
    }
    $input_hidden = array('wp_rp_ajax_nonce' => wp_create_nonce("wp_rp_ajax_nonce"), 'wp_rp_json_url' => esc_attr(WP_RP_CONTENT_BASE_URL . WP_RP_STATIC_JSON_PATH), 'wp_rp_version' => esc_attr(WP_RP_VERSION), 'wp_rp_dashboard_url' => esc_attr(WP_RP_CTR_DASHBOARD_URL), 'wp_rp_static_base_url' => esc_attr(WP_RP_STATIC_BASE_URL), 'wp_rp_plugin_static_base_url' => esc_attr(plugins_url("static/", __FILE__)));
    $settings_file = __FILE__;
    $form_url = admin_url('admin.php?page=wordpress-related-posts');
    $form_display = 'block';
    //($meta['show_turn_on_button'] && !$meta['turn_on_button_pressed'] ? 'none' : 'block');
    global $wpdb;
    $custom_fields = $wpdb->get_col("SELECT meta_key FROM {$wpdb->postmeta} GROUP BY meta_key HAVING meta_key NOT LIKE '\\_%' ORDER BY LOWER(meta_key)");
    $exclude_categories = explode(',', $options['exclude_categories']);
    $categories = get_categories(array('orderby' => 'name', 'order' => 'ASC', 'hide_empty' => false));
    $blog_url = get_site_url();
    include wp_rp_get_template('settings');
}
function wp_rp_handle_postdata()
{
    $postdata = stripslashes_deep($_POST);
    if (!count($_POST)) {
        return;
    }
    if (!isset($_POST['_wp_rp_nonce']) || !wp_verify_nonce($_POST['_wp_rp_nonce'], 'wp_rp_settings')) {
        die('Sorry, your nonce did not verify.');
    }
    $options = wp_rp_get_options();
    $meta = wp_rp_get_meta();
    $old_options = $options;
    $new_options = array('on_single_post' => isset($postdata['wp_rp_on_single_post']), 'max_related_posts' => isset($postdata['wp_rp_max_related_posts']) && is_numeric(trim($postdata['wp_rp_max_related_posts'])) ? intval(trim($postdata['wp_rp_max_related_posts'])) : 5, 'on_rss' => isset($postdata['wp_rp_on_rss']), 'related_posts_title' => isset($postdata['wp_rp_related_posts_title']) ? trim($postdata['wp_rp_related_posts_title']) : '', 'promoted_content_enabled' => isset($postdata['wp_rp_promoted_content_enabled']), 'enable_themes' => isset($postdata['wp_rp_enable_themes']), 'custom_size_thumbnail_enabled' => isset($postdata['wp_rp_custom_size_thumbnail_enabled']) && $postdata['wp_rp_custom_size_thumbnail_enabled'] === 'yes', 'custom_thumbnail_width' => isset($postdata['wp_rp_custom_thumbnail_width']) ? intval(trim($postdata['wp_rp_custom_thumbnail_width'])) : WP_RP_CUSTOM_THUMBNAILS_WIDTH, 'custom_thumbnail_height' => isset($postdata['wp_rp_custom_thumbnail_height']) ? intval(trim($postdata['wp_rp_custom_thumbnail_height'])) : WP_RP_CUSTOM_THUMBNAILS_HEIGHT, 'thumbnail_use_custom' => isset($postdata['wp_rp_thumbnail_use_custom']) && $postdata['wp_rp_thumbnail_use_custom'] === 'yes', 'thumbnail_custom_field' => isset($postdata['wp_rp_thumbnail_custom_field']) ? trim($postdata['wp_rp_thumbnail_custom_field']) : '', 'display_zemanta_linky' => $meta['show_zemanta_linky_option'] ? isset($postdata['wp_rp_display_zemanta_linky']) : true, 'only_admins_can_edit_related_posts' => !empty($postdata['wp_rp_only_admins_can_edit_related_posts']), 'max_related_post_age_in_days' => 0, 'subscription_types' => $old_options["subscription_types"], 'mobile' => array('display_comment_count' => isset($postdata['wp_rp_mobile_display_comment_count']), 'display_publish_date' => isset($postdata['wp_rp_mobile_display_publish_date']), 'display_excerpt' => isset($postdata['wp_rp_mobile_display_excerpt']), 'display_thumbnail' => isset($postdata['wp_rp_mobile_display_thumbnail']), 'excerpt_max_length' => isset($postdata['wp_rp_mobile_excerpt_max_length']) && is_numeric(trim($postdata['wp_rp_mobile_excerpt_max_length'])) ? intval(trim($postdata['wp_rp_mobile_excerpt_max_length'])) : 200, 'custom_theme_enabled' => isset($postdata['wp_rp_mobile_custom_theme_enabled'])), 'desktop' => array('display_comment_count' => isset($postdata['wp_rp_desktop_display_comment_count']), 'display_publish_date' => isset($postdata['wp_rp_desktop_display_publish_date']), 'display_excerpt' => isset($postdata['wp_rp_desktop_display_excerpt']), 'display_thumbnail' => isset($postdata['wp_rp_desktop_display_thumbnail']), 'excerpt_max_length' => isset($postdata['wp_rp_desktop_excerpt_max_length']) && is_numeric(trim($postdata['wp_rp_desktop_excerpt_max_length'])) ? intval(trim($postdata['wp_rp_desktop_excerpt_max_length'])) : 200, 'custom_theme_enabled' => isset($postdata['wp_rp_desktop_custom_theme_enabled'])));
    if (!isset($postdata['wp_rp_exclude_categories'])) {
        $new_options['exclude_categories'] = '';
    } else {
        if (is_array($postdata['wp_rp_exclude_categories'])) {
            $new_options['exclude_categories'] = join(',', $postdata['wp_rp_exclude_categories']);
        } else {
            $new_options['exclude_categories'] = trim($postdata['wp_rp_exclude_categories']);
        }
    }
    foreach (array('desktop') as $platform) {
        if (isset($postdata['wp_rp_' . $platform . '_theme_name'])) {
            // If this isn't set, maybe the AJAX didn't load...
            $new_options[$platform]['theme_name'] = trim($postdata['wp_rp_' . $platform . '_theme_name']);
        } else {
            $new_options[$platform]['theme_name'] = $old_options[$platform]['theme_name'];
        }
        if (isset($postdata['wp_rp_' . $platform . '_theme_custom_css'])) {
            $new_options[$platform]['theme_custom_css'] = $postdata['wp_rp_' . $platform . '_theme_custom_css'];
        } elseif (isset($postdata["wp_rp_{$platform}_custom_theme_enabled"])) {
            $new_options[$platform]['theme_custom_css'] = '';
        } else {
            $new_options[$platform]['theme_custom_css'] = $old_options[$platform]['theme_custom_css'];
        }
    }
    if (isset($postdata['wp_rp_classic_state'])) {
        $meta['classic_user'] = true;
    } else {
        $meta['classic_user'] = false;
    }
    wp_rp_update_meta($meta);
    if (isset($postdata['wp_rp_turn_on_button_pressed'])) {
        $meta['show_turn_on_button'] = false;
        $meta['turn_on_button_pressed'] = $postdata['wp_rp_turn_on_button_pressed'];
        $new_options['mobile']['display_thumbnail'] = true;
        $new_options['desktop']['display_thumbnail'] = true;
    }
    $preprocess_thumbnails = $new_options['desktop']['display_thumbnail'] && !$old_options['desktop']['display_thumbnail'] || $new_options['mobile']['display_thumbnail'] && !$old_options['mobile']['display_thumbnail'];
    $default_thumbnail_path = wp_rp_upload_default_thumbnail_file();
    if ($default_thumbnail_path === false) {
        // no file uploaded
        if (isset($postdata['wp_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'];
            wp_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 (!wp_rp_update_options($new_options)) {
            wp_rp_add_admin_notice('error', __('Failed to save settings.', 'wp_related_posts'));
        } else {
            wp_rp_add_admin_notice('updated', __('Settings saved.', 'wp_related_posts'));
        }
        if ($preprocess_thumbnails) {
            wp_rp_process_latest_post_thumbnails();
        }
    } else {
        // I should duplicate success message here
        wp_rp_add_admin_notice('updated', __('Settings saved.', 'wp_related_posts'));
    }
}