Admin Rss Feed plugin! Go to Plugin Settings Page', 'admin-dashboard-rss-feed' ),
esc_url( admin_url( 'options-general.php?page=wsx-rss-feed-settings' ) )
)
);
?>
' . esc_html__( 'Settings', 'admin-dashboard-rss-feed' ) . '';
return $links;
}
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'wsx_rss_feed_plugin_settings_link' );
/**
* Register settings page under Settings menu.
*/
function wsx_rss_feed_admin_page() {
add_options_page(
__( 'Admin Dashboard RSS Feed Plugin Settings', 'admin-dashboard-rss-feed' ),
__( 'Admin Dashboard RSS Feed', 'admin-dashboard-rss-feed' ),
'manage_options',
'wsx-rss-feed-settings',
'wsx_rss_feed_admin_settings_page'
);
}
add_action( 'admin_menu', 'wsx_rss_feed_admin_page' );
/**
* Enqueue admin styles/scripts only where needed.
*
* @param string $hook Current admin page hook.
*/
function wsx_rss_feed_styles( $hook ) {
$load_styles = ( 'settings_page_wsx-rss-feed-settings' === $hook ) || ( 'index.php' === $hook );
if ( ! $load_styles ) {
return;
}
wp_register_style(
'wsx-rss-styles',
plugin_dir_url( __FILE__ ) . 'admin/css/style.css',
array(),
WSX_RSS_FEED_VERSION
);
wp_enqueue_style( 'wsx-rss-styles' );
// Media uploader + settings script only on the settings page.
if ( 'settings_page_wsx-rss-feed-settings' === $hook ) {
wp_enqueue_media();
wp_enqueue_script(
'custom-admin-js',
plugin_dir_url( __FILE__ ) . 'admin/js/admin.js',
array( 'jquery' ),
WSX_RSS_FEED_VERSION,
true
);
wp_localize_script(
'custom-admin-js',
'rssFeedVars',
array(
'attachment_id' => absint( get_option( 'wsx_rss_feed_image_attachment_id', 0 ) ),
)
);
}
}
add_action( 'admin_enqueue_scripts', 'wsx_rss_feed_styles' );
/**
* Register plugin settings and handle logo attachment ID save.
*/
function wsx_rss_feed_admin_settings() {
if (
isset( $_POST['option_page'], $_POST['wsx_rss_settings_page'], $_POST['wsx_rss_feed_nonce'] ) &&
'wsx-rss-feed-settings-group' === $_POST['option_page'] &&
wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['wsx_rss_feed_nonce'] ) ), 'wsx_rss_feed_nonce_action' )
) {
if ( isset( $_POST['image_attachment_id'] ) ) {
update_option( 'wsx_rss_feed_image_attachment_id', absint( wp_unslash( $_POST['image_attachment_id'] ) ) );
}
}
register_setting(
'wsx-rss-feed-settings-group',
'wsx_logo_title',
array(
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
'default' => '',
)
);
register_setting(
'wsx-rss-feed-settings-group',
'wsx_logo_target_link',
array(
'type' => 'string',
'sanitize_callback' => 'esc_url_raw',
'default' => '',
)
);
register_setting(
'wsx-rss-feed-settings-group',
'wsx_rss_feed_url',
array(
'type' => 'string',
'sanitize_callback' => 'esc_url_raw',
'default' => '',
)
);
register_setting(
'wsx-rss-feed-settings-group',
'wsx_rss_feed_count',
array(
'type' => 'integer',
'sanitize_callback' => 'wsx_rss_feed_sanitize_count',
'default' => 5,
)
);
}
add_action( 'admin_init', 'wsx_rss_feed_admin_settings' );
/**
* Sanitize feed item count (1–10).
*
* @param mixed $count Submitted count.
* @return int
*/
function wsx_rss_feed_sanitize_count( $count ) {
$count = absint( $count );
if ( $count < 1 ) {
$count = 1;
}
if ( $count > 10 ) {
$count = 10;
}
return $count;
}
/**
* Render plugin settings page.
*/
function wsx_rss_feed_admin_settings_page() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
?>
';
if ( $wsx_logo_url && $wsx_logo_target_link && $wsx_logo_title ) {
echo '';
} elseif ( $wsx_logo_url && $wsx_logo_target_link ) {
echo '';
} elseif ( $wsx_logo_url && $wsx_logo_title ) {
echo '';
echo '
 . ')
';
echo '
' . esc_html( $wsx_logo_title ) . '
';
} elseif ( $wsx_logo_title && $wsx_logo_target_link ) {
echo '';
} else {
if ( $wsx_logo_url && $wsx_logo_id ) {
echo '';
echo wp_get_attachment_image(
$wsx_logo_id,
'full',
false,
array(
'alt' => esc_attr__( 'Logo', 'admin-dashboard-rss-feed' ),
)
);
echo '
';
}
if ( $wsx_logo_title ) {
echo '' . esc_html( $wsx_logo_title ) . '
';
}
if ( $wsx_logo_target_link ) {
echo '';
}
}
echo '';
}
/**
* Display RSS feed items in the dashboard widget.
*/
function wsx_rss_feed_create_box() {
include_once ABSPATH . WPINC . '/feed.php';
$rss_feed_url = get_option( 'wsx_rss_feed_url', '' );
$feed_count = wsx_rss_feed_sanitize_count( get_option( 'wsx_rss_feed_count', 5 ) );
wsx_rss_feed_render_branding();
echo '';
if ( empty( $rss_feed_url ) ) {
echo '- ' . esc_html__( 'Enter a valid news feed URL in the plugin settings.', 'admin-dashboard-rss-feed' ) . '
';
echo '
';
return;
}
// Default placeholder URL should prompt the user to configure settings.
if ( 'http://feeds.feedburner.com/Webstix' === $rss_feed_url ) {
$settings_url = admin_url( 'options-general.php?page=wsx-rss-feed-settings' );
echo '';
echo wp_kses_post(
sprintf(
/* translators: %s: link to settings page. */
__( 'Something went wrong! Please check the Feed URL in Settings and provide a valid RSS feed URL, then check back on this page.', 'admin-dashboard-rss-feed' ),
esc_url( $settings_url )
)
);
echo '';
return;
}
$rss = fetch_feed( $rss_feed_url );
if ( is_wp_error( $rss ) ) {
$settings_url = admin_url( 'options-general.php?page=wsx-rss-feed-settings' );
echo '';
echo wp_kses_post(
sprintf(
/* translators: %s: link to settings page. */
__( 'Unable to load the RSS feed. Please verify the Feed URL in Settings.', 'admin-dashboard-rss-feed' ),
esc_url( $settings_url )
)
);
echo '';
return;
}
$maxitems = $rss->get_item_quantity( $feed_count );
$rss_items = $rss->get_items( 0, $maxitems );
if ( empty( $rss_items ) ) {
echo '' . esc_html__( 'No items found in this feed.', 'admin-dashboard-rss-feed' ) . '';
} else {
foreach ( $rss_items as $item ) {
$title = $item->get_title();
$link = $item->get_permalink();
$summary = wp_strip_all_tags( html_entity_decode( $item->get_description(), ENT_QUOTES, 'UTF-8' ) );
$excerpt = function_exists( 'mb_substr' ) ? mb_substr( $summary, 0, 120 ) : substr( $summary, 0, 120 );
echo '';
echo '' . esc_html( $title ) . '';
if ( '' !== $excerpt ) {
echo '' . esc_html( $excerpt ) . '...
';
}
echo '';
}
}
echo '';
}