admin-bar
1 year ago
client-migration
6 months ago
compatibility
1 year ago
customizer
1 year ago
freemius
8 months ago
menu-icons
1 year ago
metabox
1 year ago
onboarding
2 weeks ago
panel
9 months ago
post-settings
2 months ago
preloader
1 year ago
shortcodes
1 year ago
themepanel
1 year ago
widgets
8 months ago
wizard
3 years ago
adobe-font.php
1 year ago
custom-code.php
8 months ago
dashboard.php
1 year ago
image-resizer.php
1 year ago
jshrink.php
3 years ago
mautic.php
2 months ago
ocean-extra-strings.php
3 years ago
plugins-tab.php
1 year ago
update-message.php
1 year ago
utils.php
1 year ago
walker.php
4 years ago
dashboard.php
243 lines
| 1 | <?php |
| 2 | /** |
| 3 | * OceanWP News |
| 4 | * |
| 5 | * @package OceanWP WordPress theme |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; |
| 10 | } |
| 11 | |
| 12 | if ( ! class_exists( 'OceanWP_Dashboard_News' ) ) : |
| 13 | |
| 14 | class OceanWP_Dashboard_News { |
| 15 | |
| 16 | /** |
| 17 | * Feed URL. |
| 18 | * |
| 19 | * @since 1.4.9 |
| 20 | */ |
| 21 | private static $feed = 'https://oceanwp.org/feed/'; |
| 22 | |
| 23 | /** |
| 24 | * Setup class. |
| 25 | * |
| 26 | * @since 1.4.9 |
| 27 | */ |
| 28 | public function __construct() { |
| 29 | add_action( 'wp_dashboard_setup', array( $this, 'register_dashboard_news' ) ); |
| 30 | add_action( 'admin_enqueue_scripts', array( $this, 'script' ) ); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Register the news block on the WordPress dashboard |
| 35 | * |
| 36 | * @since 1.4.9 |
| 37 | */ |
| 38 | public function register_dashboard_news() { |
| 39 | if ( apply_filters( 'oceanwp_news_enabled', false ) ) { |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | wp_add_dashboard_widget( 'owp_dashboard_news', __( 'OceanWP Overview', 'ocean-extra' ), array( $this, 'dashboard_news_widget' ) ); |
| 44 | |
| 45 | // Move our widget to top. |
| 46 | global $wp_meta_boxes; |
| 47 | |
| 48 | $dashboard = $wp_meta_boxes['dashboard']['normal']['core']; |
| 49 | $widget = array( |
| 50 | 'owp_dashboard_news' => $dashboard['owp_dashboard_news'], |
| 51 | ); |
| 52 | |
| 53 | $wp_meta_boxes['dashboard']['normal']['core'] = array_merge( $widget, $dashboard ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * OceanWP News & Updates |
| 58 | * |
| 59 | * @since 1.4.9 |
| 60 | */ |
| 61 | public function dashboard_news_widget() { |
| 62 | |
| 63 | // Get theme |
| 64 | $owp = wp_get_theme( 'oceanwp' ); |
| 65 | |
| 66 | // OceanWP img url |
| 67 | $owp_img = OE_URL . 'assets/img/oceanwp.png'; ?> |
| 68 | |
| 69 | <div class="owp-dashboard-widget"> |
| 70 | <div class="owp-header"> |
| 71 | <div class="owp-logo"><img src="<?php echo esc_url( $owp_img ); ?>" alt="OceanWP" /></div> |
| 72 | <div class="owp-versions"><?php echo __( 'OceanWP', 'ocean-extra' ); ?> v<?php echo esc_html( $owp->get( 'Version' ) ); ?></div> |
| 73 | </div> |
| 74 | <div class="rss-widget"> |
| 75 | <h3 class="owp-heading"><?php echo __( 'News & Updates', 'ocean-extra' ); ?></h3> |
| 76 | <?php |
| 77 | $this->oceanwp_cached_rss_widget( 'owp_dashboard_news', array( $this, 'oceanwp_news_output' ) ); ?> |
| 78 | </div> |
| 79 | <div class="owp-news-footer"> |
| 80 | <?php |
| 81 | printf( |
| 82 | '<a href="%1$s" class="owp-post-link" target="_blank">%2$s <span class="screen-reader-text">%3$s</span><span aria-hidden="true" class="dashicons dashicons-external"></span></a>', |
| 83 | 'https://oceanwp.org/demos/', |
| 84 | __( 'Demos' ), |
| 85 | /* translators: accessibility text */ |
| 86 | __( '(opens in a new window)' ) |
| 87 | ); |
| 88 | printf( |
| 89 | '<a href="%1$s" class="owp-post-link" target="_blank">%2$s <span class="screen-reader-text">%3$s</span><span aria-hidden="true" class="dashicons dashicons-external"></span></a>', |
| 90 | 'https://oceanwp.org/blog/', |
| 91 | __( 'Blog' ), |
| 92 | /* translators: accessibility text */ |
| 93 | __( '(opens in a new window)' ) |
| 94 | ); |
| 95 | printf( |
| 96 | '<a href="%1$s" class="owp-post-link" target="_blank">%2$s <span class="screen-reader-text">%3$s</span><span aria-hidden="true" class="dashicons dashicons-external"></span></a>', |
| 97 | 'https://oceanwp.org/support/', |
| 98 | __( 'Help' ), |
| 99 | /* translators: accessibility text */ |
| 100 | __( '(opens in a new window)' ) |
| 101 | ); |
| 102 | |
| 103 | // If no premium extensions |
| 104 | if ( true != apply_filters( 'oceanwp_licence_tab_enable', false ) ) { |
| 105 | printf( |
| 106 | '<a href="%1$s" class="owp-post-link" target="_blank"><span style="color: #fe5252;">%2$s <span class="screen-reader-text">%3$s</span><span aria-hidden="true" class="dashicons dashicons-external"></span></span></a>', |
| 107 | 'https://see.oceanwp.org/bundle-features', |
| 108 | __( 'Extensions' ), |
| 109 | /* translators: accessibility text */ |
| 110 | __( '(opens in a new window)' ) |
| 111 | ); |
| 112 | } ?> |
| 113 | </div> |
| 114 | </div> |
| 115 | <?php |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Display the WordPress news feeds. |
| 120 | * |
| 121 | * @since 1.4.9 |
| 122 | */ |
| 123 | public function oceanwp_news_output() { |
| 124 | $this->oceanwp_rss_output( self::$feed ); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Display the RSS entries in a list. |
| 129 | * |
| 130 | * @since 1.4.9 |
| 131 | */ |
| 132 | public function oceanwp_rss_output( $rss, $args = array() ) { |
| 133 | if ( is_string( $rss ) ) { |
| 134 | $rss = fetch_feed($rss); |
| 135 | } elseif ( is_array($rss) && isset($rss['url']) ) { |
| 136 | $args = $rss; |
| 137 | $rss = fetch_feed($rss['url']); |
| 138 | } elseif ( !is_object($rss) ) { |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | if ( is_wp_error($rss) ) { |
| 143 | if ( is_admin() || current_user_can('manage_options') ) |
| 144 | echo '<p><strong>' . __( 'RSS Error:', 'ocean-extra' ) . '</strong> ' . $rss->get_error_message() . '</p>'; |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | $default_args = array( 'show_summary' => 1, 'items' => 4 ); |
| 149 | $args = wp_parse_args( $args, $default_args ); |
| 150 | |
| 151 | $items = (int) $args['items']; |
| 152 | $show_summary = (int) $args['show_summary']; |
| 153 | |
| 154 | if ( !$rss->get_item_quantity() ) { |
| 155 | echo '<ul><li>' . __( 'An error has occurred, which probably means the feed is down. Try again later.', 'ocean-extra' ) . '</li></ul>'; |
| 156 | $rss->__destruct(); |
| 157 | unset($rss); |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | echo '<ul>'; |
| 162 | foreach ( $rss->get_items( 0, $items ) as $item ) { |
| 163 | $link = $item->get_link(); |
| 164 | while ( stristr( $link, 'http' ) != $link ) { |
| 165 | $link = substr( $link, 1 ); |
| 166 | } |
| 167 | $link = esc_url( strip_tags( $link ) ); |
| 168 | |
| 169 | $title = esc_html( trim( strip_tags( $item->get_title() ) ) ); |
| 170 | if ( empty( $title ) ) { |
| 171 | $title = __( 'Untitled' ); |
| 172 | } |
| 173 | |
| 174 | $desc = @html_entity_decode( $item->get_description(), ENT_QUOTES, get_option( 'blog_charset' ) ); |
| 175 | $desc = esc_attr( wp_trim_words( $desc, 30, '…' ) ); |
| 176 | |
| 177 | $summary = ''; |
| 178 | if ( $show_summary ) { |
| 179 | $summary = $desc; |
| 180 | $summary = '<div class="rssSummary">' . esc_html( $summary ) . '</div>'; |
| 181 | } |
| 182 | |
| 183 | echo "<li><a class='rsswidget' href='$link?utm_source=wp-news-widget&utm_medium=wp-dash&utm_campaign=news-feed' target='_blank'>$title</a>{$summary}</li>"; |
| 184 | } |
| 185 | echo '</ul>'; |
| 186 | $rss->__destruct(); |
| 187 | unset($rss); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Checks to see if all of the feed url in $check_urls are cached. |
| 192 | * |
| 193 | * @since 1.4.9 |
| 194 | * |
| 195 | * @param string $widget_id |
| 196 | * @param callable $callback |
| 197 | * @param array $check_urls RSS feeds |
| 198 | * @return bool False on failure. True on success. |
| 199 | */ |
| 200 | function oceanwp_cached_rss_widget( $widget_id, $callback ) { |
| 201 | $loading = '<p class="widget-loading hide-if-no-js">' . __( 'Loading…' ) . '</p><div class="hide-if-js notice notice-error inline"><p>' . __( 'This widget requires JavaScript.', 'ocean-extra' ) . '</p></div>'; |
| 202 | $check_urls = self::$feed; |
| 203 | |
| 204 | $locale = get_user_locale(); |
| 205 | $cache_key = 'owp_feed_data_' . md5( $widget_id . '_' . $locale ); |
| 206 | if ( false !== ( $output = get_transient( $cache_key ) ) ) { |
| 207 | echo $output; |
| 208 | return true; |
| 209 | } |
| 210 | |
| 211 | if ( empty( $check_urls ) ) { |
| 212 | echo $loading; |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | if ( $callback && is_callable( $callback ) ) { |
| 217 | $args = array_slice( func_get_args(), 3 ); |
| 218 | array_unshift( $args, $widget_id, $check_urls ); |
| 219 | ob_start(); |
| 220 | call_user_func_array( $callback, $args ); |
| 221 | set_transient( $cache_key, ob_get_flush(), 12 * HOUR_IN_SECONDS ); // Default lifetime in cache of 12 hours (same as the feeds) |
| 222 | } |
| 223 | |
| 224 | return true; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Script |
| 229 | * |
| 230 | * @since 1.4.9 |
| 231 | */ |
| 232 | public function script( $hook ) { |
| 233 | $screen = get_current_screen(); |
| 234 | if ( 'dashboard' === $screen->id ) { |
| 235 | wp_enqueue_style( 'oceanwp-news', plugins_url( '/assets/css/admin.min.css', dirname( __FILE__ ) ) ); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | } |
| 240 | |
| 241 | endif; |
| 242 | |
| 243 | new OceanWP_Dashboard_News(); |