'', 'heading' => '', 'limit' => 0, ); } /** * Get the saved settings merged with the defaults. * * @return array */ function ssrss_get_settings() { $stored = get_option( SSRSS_OPTION_KEY, array() ); if ( ! is_array( $stored ) ) { $stored = array(); } return wp_parse_args( $stored, ssrss_get_defaults() ); } /** * Cache lifetime in seconds. * * @return int */ function ssrss_cache_ttl() { /** * Filter the RSS response cache lifetime. * * @param int $ttl Lifetime in seconds. */ return (int) apply_filters( 'ssrss_cache_ttl', 15 * MINUTE_IN_SECONDS ); } /** * Store default options on activation. * * @return void */ function ssrss_activate() { if ( false === get_option( SSRSS_OPTION_KEY ) ) { add_option( SSRSS_OPTION_KEY, ssrss_get_defaults() ); } } register_activation_hook( __FILE__, 'ssrss_activate' ); /** * Clear the cached feed when the settings change. * * @param array $old Previous settings. * @param array $new New settings. * @return void */ function ssrss_clear_cache( $old, $new ) { foreach ( array( $old, $new ) as $settings ) { if ( is_array( $settings ) && ! empty( $settings['url'] ) ) { delete_transient( 'ssrss_' . md5( trim( (string) $settings['url'] ) ) ); } } } add_action( 'update_option_' . SSRSS_OPTION_KEY, 'ssrss_clear_cache', 10, 2 ); /** * Register the admin settings page. * * @return void */ function ssrss_admin_menu() { add_options_page( __( 'Super Simple RSS Reader', 'super-simple-rssreader' ), __( 'RSS Reader', 'super-simple-rssreader' ), 'manage_options', 'ssrss', 'ssrss_render_settings_page' ); } add_action( 'admin_menu', 'ssrss_admin_menu' ); /** * Register settings, sections and fields. * * @return void */ function ssrss_register_settings() { register_setting( 'ssrss_settings_group', SSRSS_OPTION_KEY, array( 'type' => 'array', 'sanitize_callback' => 'ssrss_sanitize_settings', 'default' => ssrss_get_defaults(), ) ); add_settings_section( 'ssrss_main', __( 'Feed settings', 'super-simple-rssreader' ), '__return_false', 'ssrss' ); add_settings_field( 'ssrss_url', __( 'Feed URL', 'super-simple-rssreader' ), 'ssrss_field_url', 'ssrss', 'ssrss_main' ); add_settings_field( 'ssrss_heading', __( 'Heading', 'super-simple-rssreader' ), 'ssrss_field_heading', 'ssrss', 'ssrss_main' ); add_settings_field( 'ssrss_limit', __( 'Max items', 'super-simple-rssreader' ), 'ssrss_field_limit', 'ssrss', 'ssrss_main' ); } add_action( 'admin_init', 'ssrss_register_settings' ); /** * Sanitize the submitted settings. * * @param mixed $input Raw input. * @return array */ function ssrss_sanitize_settings( $input ) { $defaults = ssrss_get_defaults(); $input = is_array( $input ) ? $input : array(); return array( 'url' => isset( $input['url'] ) ? esc_url_raw( trim( (string) $input['url'] ) ) : $defaults['url'], 'heading' => isset( $input['heading'] ) ? sanitize_text_field( (string) $input['heading'] ) : $defaults['heading'], 'limit' => isset( $input['limit'] ) ? absint( $input['limit'] ) : 0, ); } /** * Render the Feed URL field. * * @return void */ function ssrss_field_url() { $settings = ssrss_get_settings(); printf( '', esc_attr( SSRSS_OPTION_KEY ), esc_attr( $settings['url'] ) ); echo '
' . esc_html__( 'Full URL of the RSS feed to read. The plugin shows nothing until this is set.', 'super-simple-rssreader' ) . '
'; } /** * Render the Heading field. * * @return void */ function ssrss_field_heading() { $settings = ssrss_get_settings(); printf( '', esc_attr( SSRSS_OPTION_KEY ), esc_attr( $settings['heading'] ) ); echo '' . esc_html__( 'Optional heading shown above the list. Leave empty for no heading.', 'super-simple-rssreader' ) . '
'; } /** * Render the Max items field. * * @return void */ function ssrss_field_limit() { $settings = ssrss_get_settings(); printf( '', esc_attr( SSRSS_OPTION_KEY ), esc_attr( (int) $settings['limit'] ) ); echo '' . esc_html__( 'Maximum number of items to show. Use 0 to show all items.', 'super-simple-rssreader' ) . '
'; } /** * Render the settings page. * * @return void */ function ssrss_render_settings_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } ?>[super_simple_rssreader]' ); ?>