From 353ee5ecb0ea253bf39c467a07f4a9c3e13dcc20 Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Tue, 4 Nov 2014 21:09:12 -0600 Subject: [PATCH] Register styles in main plugin Enqueue styles only if needed --- lib/LatestRidesWidget.class.php | 8 +++++++- lib/RideShortcode.class.php | 11 +---------- lib/Settings.class.php | 4 ---- lib/Strava.class.php | 7 +++++++ wp-strava.php | 12 ------------ 5 files changed, 15 insertions(+), 27 deletions(-) diff --git a/lib/LatestRidesWidget.class.php b/lib/LatestRidesWidget.class.php index 40e8cdd..a4f50b7 100644 --- a/lib/LatestRidesWidget.class.php +++ b/lib/LatestRidesWidget.class.php @@ -8,7 +8,13 @@ class WPStrava_LatestRidesWidget extends WP_Widget { public function __construct() { $widget_ops = array( 'classname' => 'LatestRidesWidget', 'description' => __( 'Will publish your latest rides activity from strava.com.' ) ); parent::__construct( 'wp-strava', $name = 'Strava Latest Rides', $widget_ops ); - wp_enqueue_style( 'wp-strava' ); //TODO only load this when wigit is loaded + add_action( 'wp_enqueue_scripts', array( $this, 'maybe_enqueue' ) ); + } + + public function maybe_enqueue() { + if ( is_active_widget( false, false, $this->id_base ) ) { + wp_enqueue_style( 'wp-strava-style' ); //only load this when wigit is loaded + } } /** @see WP_Widget::widget */ diff --git a/lib/RideShortcode.class.php b/lib/RideShortcode.class.php index 5c755da..8113310 100644 --- a/lib/RideShortcode.class.php +++ b/lib/RideShortcode.class.php @@ -5,13 +5,11 @@ class WPStrava_RideShortcode { static function init() { add_shortcode('ride', array(__CLASS__, 'handler')); - - add_action('init', array(__CLASS__, 'registerScripts')); add_action('wp_footer', array(__CLASS__, 'printScripts')); } // Shortcode handler function - // [ride id=id som=metric efforts=false threshold=5 map-width="100%" map-height="400px"] tag + // [ride id=id som=metric map_width="100%" map_height="400px"] function handler($atts) { self::$add_script = true; @@ -72,13 +70,6 @@ class WPStrava_RideShortcode { } } // handler - static function registerScripts() { - wp_register_style('wp-strava-style', WPSTRAVA_PLUGIN_URL . 'css/wp-strava.css' ); - - //wp_register_script('wp-strava-script', WPSTRAVA_PLUGIN_URL . 'js/wp-strava.js', array( 'jquery' ), '1.0', true); - //wp_register_script('google-maps', 'http://maps.google.com/maps/api/js?sensor=false'); - } - static function printScripts() { if (self::$add_script) { wp_enqueue_style('wp-strava-style'); diff --git a/lib/Settings.class.php b/lib/Settings.class.php index 5ff7f55..f1d7a42 100644 --- a/lib/Settings.class.php +++ b/lib/Settings.class.php @@ -203,10 +203,6 @@ class WPStrava_Settings { } } - public function print_options_label() { - ?>

Options

diff --git a/lib/Strava.class.php b/lib/Strava.class.php index 86589be..cc807f4 100644 --- a/lib/Strava.class.php +++ b/lib/Strava.class.php @@ -19,6 +19,8 @@ class WPStrava { if ( is_admin() ) { $this->settings->hook(); + } else { + add_action( 'wp_enqueue_scripts', array( $this, 'register_scripts' ) ); } // Register StravaLatestRidesWidget widget @@ -66,4 +68,9 @@ class WPStrava { return $this->rides; } + + public function register_scripts() { + // Register a personalized stylesheet + wp_register_style( 'wp-strava-style', WPSTRAVA_PLUGIN_URL . 'css/wp-strava.css' ); + } } \ No newline at end of file diff --git a/wp-strava.php b/wp-strava.php index 954f67a..f0546d3 100755 --- a/wp-strava.php +++ b/wp-strava.php @@ -37,15 +37,3 @@ if( file_exists( WPSTRAVA_PLUGIN_DIR . 'lang/' . get_locale() . '.mo' ) ) { require_once WPSTRAVA_PLUGIN_DIR . 'lib/Strava.class.php'; $wpstrava = WPStrava::get_instance(); - -//@TODO only load these when needed using is_active_widget() -function wpstrava_load_scripts_and_styles() { - // Register a personalized stylesheet - wp_register_style('wp-strava-style', WPSTRAVA_PLUGIN_URL . 'css/wp-strava.css' ); - wp_enqueue_style('wp-strava'); - - // Load required javascript libraries - wp_enqueue_script('jquery'); - //wp_enqueue_script('google-maps', 'http://maps.google.com/maps/api/js?sensor=false'); -} -add_action('wp_enqueue_script', 'wpstrava_load_scripts_and_styles');