| 1 |
<?php |
| 2 |
|
| 3 |
if( !function_exists('add_action') ) |
| 4 |
die("access denied."); |
| 5 |
|
| 6 |
define('POWERPRESS_FEED_HIGHLIGHTED', 'https://blubrry.com/podcast-insider/category/highlighted/feed/?order=ASC'); |
| 7 |
define('POWERPRESS_FEED_NEWS', 'https://blubrry.com/podcast-insider/feed/'); |
| 8 |
|
| 9 |
function powerpress_get_news($feed_url, $limit=10) |
| 10 |
{ |
| 11 |
include_once(ABSPATH . WPINC . '/feed.php'); |
| 12 |
$rss = fetch_feed( $feed_url ); |
| 13 |
|
| 14 |
// If feed doesn't work... |
| 15 |
if ( is_wp_error($rss) ) |
| 16 |
{ |
| 17 |
require_once( ABSPATH . WPINC . '/class-feed.php' ); |
| 18 |
// Try fetching the feed using CURL directly... |
| 19 |
$content = powerpress_remote_fopen($feed_url, false, array(), 3, false, true); |
| 20 |
if( !$content ) { |
| 21 |
return false; |
| 22 |
} |
| 23 |
// Load the content in a fetch_feed object... |
| 24 |
$rss = new SimplePie(); |
| 25 |
|
| 26 |
$rss->set_sanitize_class( 'WP_SimplePie_Sanitize_KSES' ); |
| 27 |
// We must manually overwrite $feed->sanitize because SimplePie's |
| 28 |
// constructor sets it before we have a chance to set the sanitization class |
| 29 |
$rss->sanitize = new WP_SimplePie_Sanitize_KSES(); |
| 30 |
|
| 31 |
$rss->set_cache_class( 'WP_Feed_Cache' ); |
| 32 |
$rss->set_file_class( 'WP_SimplePie_File' ); |
| 33 |
$rss->set_raw_data($content); |
| 34 |
$rss->set_cache_duration( apply_filters( 'wp_feed_cache_transient_lifetime', 12 * HOUR_IN_SECONDS, $feed_url ) ); |
| 35 |
do_action_ref_array( 'wp_feed_options', array( &$rss, $feed_url ) ); |
| 36 |
$rss->init(); |
| 37 |
$rss->set_output_encoding( get_option( 'blog_charset' ) ); |
| 38 |
$rss->handle_content_type(); |
| 39 |
|
| 40 |
if ( $rss->error() ) |
| 41 |
return false; |
| 42 |
} |
| 43 |
|
| 44 |
$rss_items = $rss->get_items( 0, $rss->get_item_quantity( $limit ) ); |
| 45 |
|
| 46 |
// If the feed was erroneously |
| 47 |
if ( !$rss_items ) { |
| 48 |
$md5 = md5( $feed_url ); // This is from simple-pie, look at member variable ->cache_name_function |
| 49 |
delete_transient( 'feed_' . $md5 ); |
| 50 |
delete_transient( 'feed_mod_' . $md5 ); |
| 51 |
$rss->__destruct(); |
| 52 |
unset($rss); |
| 53 |
$rss = fetch_feed( $feed_url ); |
| 54 |
$rss_items = $rss->get_items( 0, $rss->get_item_quantity( $num ) ); |
| 55 |
$rss->__destruct(); |
| 56 |
unset($rss); |
| 57 |
} |
| 58 |
|
| 59 |
return $rss_items; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
function powerpress_dashboard_head() |
| 64 |
{ |
| 65 |
powerpress_enqueue_assets([ |
| 66 |
// styles |
| 67 |
'powerpress-variables' => ['path' => 'css/variables'], |
| 68 |
'powerpress-admin-css' => ['path' => 'css/admin'], |
| 69 |
'powerpress-program-card-css' => ['path' => 'css/components/program-card'], |
| 70 |
'powerpress-stats-widget' => ['path' => 'css/components/stats-widget'], |
| 71 |
'powerpress-news-widget' => ['path' => 'css/components/news-widget'], |
| 72 |
'powerpress-bootstrap-grid' => ['path' => 'css/bootstrap-grid'], |
| 73 |
|
| 74 |
// scripts (player.min.js and chart.min.js always have .min in filename) |
| 75 |
'powerpress-player' => ['type' => 'script', 'path' => 'player.min', 'no_suffix' => true, 'footer' => false], |
| 76 |
'chartjs' => ['type' => 'script', 'path' => '3rdparty/chart.min', 'no_suffix' => true, 'footer' => false], |
| 77 |
'powerpress-program-card' => ['type' => 'script', 'path' => 'js/program-card', 'deps' => ['chartjs'], 'module' => true], |
| 78 |
]); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Prints Blubrry Stats Widget to the WordPress dashboard using the program card class |
| 83 |
*/ |
| 84 |
function powerpress_dashboard_stats_content() |
| 85 |
{ |
| 86 |
require_once 'powerpressadmin-program-card.class.php'; |
| 87 |
$programCard = new PowerPressProgramCard(); |
| 88 |
$programCard->render_stats_widget(true); |
| 89 |
} |
| 90 |
|
| 91 |
function powerpress_dashboard_news_content() |
| 92 |
{ |
| 93 |
powerpressadmin_community_news(); |
| 94 |
} |
| 95 |
|
| 96 |
function powerpress_feed_text_limit( $text, $limit, $finish = '…') { |
| 97 |
if( strlen( $text ) > $limit ) { |
| 98 |
//$text = (function_exists('mb_substr')?mb_substr($text, 0, $limit):substr($text, 0, $limit) ); |
| 99 |
$text = substr( $text, 0, $limit ); |
| 100 |
$text = substr( $text, 0, - ( strlen( strrchr( $text,' ') ) ) ); |
| 101 |
$text .= $finish; |
| 102 |
} |
| 103 |
return $text; |
| 104 |
} |
| 105 |
|
| 106 |
function powerpress_dashboard_setup() |
| 107 |
{ |
| 108 |
if( !function_exists('wp_add_dashboard_widget') ) |
| 109 |
return; // We are not in the dashboard! |
| 110 |
|
| 111 |
if( !empty($_GET['powerpressdash']) && $_GET['powerpressdash'] == 1 ) |
| 112 |
return; |
| 113 |
|
| 114 |
$Settings = get_option('powerpress_general'); |
| 115 |
$StatsDashboard = true; |
| 116 |
$NewsDashboard = true; |
| 117 |
|
| 118 |
if( !empty($Settings['disable_dashboard_stats']) ) |
| 119 |
$StatsDashboard = false; // Lets not do anything to the dashboard for PowerPress Statistics |
| 120 |
|
| 121 |
if( !empty($Settings['disable_dashboard_news']) ) |
| 122 |
$NewsDashboard = false; // Lets not do anything to the dashboard for PowerPress News |
| 123 |
|
| 124 |
if( !empty($Settings['use_caps']) && !current_user_can('view_podcast_stats') ) |
| 125 |
$StatsDashboard = false; |
| 126 |
|
| 127 |
$user = wp_get_current_user(); |
| 128 |
|
| 129 |
if( !empty($_GET['powerpressdash']) && $_GET['powerpressdash'] == 2 ) { |
| 130 |
return; |
| 131 |
} |
| 132 |
|
| 133 |
//if( $NewsDashboard ) |
| 134 |
wp_add_dashboard_widget( 'powerpress_dashboard_news', __( 'Podcast Insider by Blubrry', 'powerpress'), 'powerpress_dashboard_news_content' ); |
| 135 |
|
| 136 |
if( !empty($_GET['powerpressdash']) && $_GET['powerpressdash'] == 3 ) { |
| 137 |
return; |
| 138 |
} |
| 139 |
|
| 140 |
if( $StatsDashboard ) |
| 141 |
wp_add_dashboard_widget( 'powerpress_dashboard_stats', __( 'Blubrry Podcast Statistics', 'powerpress'), 'powerpress_dashboard_stats_content' ); |
| 142 |
|
| 143 |
if( !empty($_GET['powerpressdash']) && $_GET['powerpressdash'] == 4 ) { |
| 144 |
return; |
| 145 |
} |
| 146 |
|
| 147 |
if( !empty( $user ) ) { |
| 148 |
$user_options = get_user_option('powerpress_user'); |
| 149 |
if( empty($user_options) || empty($user_options['dashboard_installed']) || $user_options['dashboard_installed'] < 2 ) |
| 150 |
{ |
| 151 |
if( !is_array($user_options) ) |
| 152 |
$user_options = array(); |
| 153 |
|
| 154 |
if( !empty($_GET['powerpressdash']) && $_GET['powerpressdash'] == 5 ) { |
| 155 |
return; |
| 156 |
} |
| 157 |
|
| 158 |
// First time we've seen this setting, so must be first time we've added the widgets, lets stack them at the top for convenience. |
| 159 |
powerpressadmin_add_dashboard_widgets($user->ID); |
| 160 |
$user_options['dashboard_installed'] = 2; // version of PowerPress |
| 161 |
update_user_option($user->ID, "powerpress_user", $user_options, true); |
| 162 |
} |
| 163 |
else |
| 164 |
{ |
| 165 |
if( !empty($_GET['powerpressdash']) && $_GET['powerpressdash'] == 6 ) { |
| 166 |
return; |
| 167 |
} |
| 168 |
|
| 169 |
powerpressadmin_add_dashboard_widgets(false); |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
function powerpressadmin_add_dashboard_notice_widget($user_id, $notice_id) |
| 175 |
{ |
| 176 |
$user_options = get_user_option('meta-box-order_dashboard', $user_id); |
| 177 |
if( $user_options ) |
| 178 |
{ |
| 179 |
$save = false; |
| 180 |
if( !preg_match('/powerpress_dashboard_notice_'.$notice_id.'/', $user_options['normal']) && !preg_match('/powerpress_dashboard_notice_'.$notice_id.'/', $user_options['side']) && !preg_match('/powerpress_dashboard_notice_'.$notice_id.'/', $user_options['column3']) && !preg_match('/powerpress_dashboard_notice_'.$notice_id.'/', $user_options['column4']) ) |
| 181 |
{ |
| 182 |
$save = true; |
| 183 |
$user_options['normal'] = 'powerpress_dashboard_notice_'.$notice_id.','.$user_options['normal']; |
| 184 |
} |
| 185 |
|
| 186 |
if( $save ) |
| 187 |
{ |
| 188 |
update_user_option($user_id, "meta-box-order_dashboard", $user_options, true); |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
function powerpressadmin_add_dashboard_widgets( $check_user_id = false) |
| 194 |
{ |
| 195 |
// Only re-order the powerpress widgets if they aren't already on the dashboard: |
| 196 |
if( $check_user_id ) |
| 197 |
{ |
| 198 |
$user_options = get_user_option('meta-box-order_dashboard', $check_user_id); |
| 199 |
if( $user_options ) |
| 200 |
{ |
| 201 |
$save = false; |
| 202 |
if( !preg_match('/powerpress_dashboard_stats/', $user_options['normal']) && !preg_match('/powerpress_dashboard_stats/', $user_options['side']) && !preg_match('/powerpress_dashboard_stats/', $user_options['column3']) && !preg_match('/powerpress_dashboard_stats/', $user_options['column4']) ) |
| 203 |
{ |
| 204 |
$save = true; |
| 205 |
if( !empty($user_options['side']) ) |
| 206 |
$user_options['side'] = 'powerpress_dashboard_stats,'.$user_options['side']; |
| 207 |
else |
| 208 |
$user_options['normal'] = 'powerpress_dashboard_stats,'.$user_options['normal']; |
| 209 |
} |
| 210 |
|
| 211 |
if( !preg_match('/powerpress_dashboard_news/', $user_options['normal']) && !preg_match('/powerpress_dashboard_news/', $user_options['side']) && !preg_match('/powerpress_dashboard_news/', $user_options['column3']) && !preg_match('/powerpress_dashboard_news/', $user_options['column4']) ) |
| 212 |
{ |
| 213 |
$save = true; |
| 214 |
$user_options['normal'] = 'powerpress_dashboard_news,'.$user_options['normal']; |
| 215 |
} |
| 216 |
|
| 217 |
if( $save ) |
| 218 |
{ |
| 219 |
update_user_option($check_user_id, "meta-box-order_dashboard", $user_options, true); |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
// Reorder for all future users |
| 225 |
global $wp_meta_boxes; |
| 226 |
$dashboard_current = $wp_meta_boxes['dashboard']['normal']['core']; |
| 227 |
|
| 228 |
$dashboard_powerpress = array(); |
| 229 |
for( $i = 0; $i < 20; $i++ ) |
| 230 |
{ |
| 231 |
if( isset( $dashboard_current['powerpress_dashboard_notice_' . $i] ) ) |
| 232 |
{ |
| 233 |
$dashboard_powerpress['powerpress_dashboard_notice_' . $i] = $dashboard_current['powerpress_dashboard_notice_' . $i]; |
| 234 |
unset($dashboard_current['powerpress_dashboard_notice_' . $i]); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
if( isset( $dashboard_current['powerpress_dashboard_news'] ) ) |
| 239 |
{ |
| 240 |
$dashboard_powerpress['powerpress_dashboard_news'] = $dashboard_current['powerpress_dashboard_news']; |
| 241 |
unset($dashboard_current['powerpress_dashboard_news']); |
| 242 |
} |
| 243 |
|
| 244 |
if( isset( $dashboard_current['powerpress_dashboard_stats'] ) ) |
| 245 |
{ |
| 246 |
$dashboard_powerpress['powerpress_dashboard_stats'] = $dashboard_current['powerpress_dashboard_stats']; |
| 247 |
unset($dashboard_current['powerpress_dashboard_stats']); |
| 248 |
} |
| 249 |
|
| 250 |
if( count($dashboard_powerpress) > 0 ) |
| 251 |
{ |
| 252 |
$wp_meta_boxes['dashboard']['normal']['core'] = array_merge($dashboard_powerpress, $dashboard_current); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
add_action('admin_head-index.php', 'powerpress_dashboard_head'); |
| 257 |
add_action('wp_dashboard_setup', 'powerpress_dashboard_setup'); |