Skip to content

Commit 28fdbcd

Browse files
committed
Jetpack: Introduce helper for sharing code between modules
- Shared code will live in /lib - It'll be loadable via 'require_lib()' calls
1 parent e69af29 commit 28fdbcd

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

class.jetpack.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,15 @@ private function Jetpack() {
226226
add_action( 'customize_controls_enqueue_scripts', array( $this, 'devicepx' ) );
227227
add_action( 'admin_enqueue_scripts', array( $this, 'devicepx' ) );
228228

229+
add_filter( 'jetpack_require_lib_dir', array( $this, 'require_lib_dir' ) );
230+
229231
// add_action( 'jetpack_admin_menu', array( $this, 'admin_menu_modules' ) );
230232

231233
add_action( 'jetpack_activate_module', array( $this, 'activate_module_actions' ) );
232234

233235
/**
234236
* These actions run checks to load additional files.
235-
* They check for external files or plugins, so thef need to run as late as possible.
237+
* They check for external files or plugins, so they need to run as late as possible.
236238
*/
237239
add_action( 'plugins_loaded', array( $this, 'check_open_graph' ), 999 );
238240
add_action( 'plugins_loaded', array( $this, 'check_rest_api_compat' ), 1000 );
@@ -299,6 +301,16 @@ function devicepx() {
299301
wp_enqueue_script( 'devicepx', ( is_ssl() ? 'https' : 'http' ) . '://s0.wp.com/wp-content/js/devicepx-jetpack.js', array(), gmdate( 'oW' ), true );
300302
}
301303

304+
/*
305+
* Returns the location of Jetpack's lib directory. This filter is applied
306+
* in require_lib().
307+
*
308+
* @filter require_lib_dir
309+
*/
310+
function require_lib_dir( $lib_dir ) {
311+
return JETPACK__PLUGIN_DIR . 'lib';
312+
}
313+
302314
/**
303315
* Is Jetpack active?
304316
*/

jetpack.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
require_once( JETPACK__PLUGIN_DIR . 'functions.compat.php' );
5151
require_once( JETPACK__PLUGIN_DIR . 'functions.gallery.php' );
5252
require_once( JETPACK__PLUGIN_DIR . 'functions.twitter-cards.php' );
53+
require_once( JETPACK__PLUGIN_DIR . 'require-lib.php' );
5354

5455
register_activation_hook( __FILE__, array( 'Jetpack', 'plugin_activation' ) );
5556
register_deactivation_hook( __FILE__, array( 'Jetpack', 'plugin_deactivation' ) );

readme.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
=== Jetpack by WordPress.com ===
2-
Contributors: automattic, alternatekev, andy, apeatling, azaozz, barry, beaulebens, blobaugh, cfinke, chellycat, danielbachhuber, daniloercoli, designsimply, eoigal, ethitter, gibrown, georgestephanis, hew, hugobaeta, iammattthomas, jblz, jeherve, jkudish, Joen, johnjamesjacoby, jshreve, lancewillett, martinremy, matt, matveb, mdawaffe, migueluy, nickmomrik, pento, stephdau, tmoorewp, Viper007Bond, westi, yoavf
2+
Contributors: automattic, alternatekev, andy, apeatling, azaozz, barry, beaulebens, blobaugh, cfinke, chellycat, danielbachhuber, daniloercoli, designsimply, eoigal, ethitter, gibrown, georgestephanis, hew, hugobaeta, iammattthomas, jblz, jeherve, jkudish, Joen, johnjamesjacoby, jshreve, lancewillett, martinremy, matt, matveb, mcsf, mdawaffe, migueluy, nickmomrik, pento, stephdau, tmoorewp, Viper007Bond, westi, yoavf
33
Tags: WordPress.com, statistics, stats, views, tweets, twitter, widget, gravatar, hovercards, profile, equations, latex, math, maths, youtube, shortcode, archives, audio, blip, bliptv, dailymotion, digg, flickr, googlevideo, google, googlemaps, kyte, kytetv, livevideo, redlasso, rockyou, rss, scribd, slide, slideshare, soundcloud, vimeo, shortlinks, wp.me, subscriptions, notifications, notes, json, api, rest, mosaic, gallery, slideshow
44
Requires at least: 3.5
55
Tested up to: 3.6

require-lib.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
function jetpack_require_lib( $slug ) {
4+
if ( !preg_match( '|^[a-z0-9/_.-]+$|i', $slug ) ) {
5+
trigger_error( "Cannot load a library with invalid slug $slug.", E_USER_ERROR );
6+
return;
7+
}
8+
$basename = basename( $slug );
9+
$lib_dir = WP_CONTENT_DIR . '/lib';
10+
$lib_dir = apply_filters( 'jetpack_require_lib_dir', $lib_dir );
11+
$choices = array(
12+
"$lib_dir/$slug.php",
13+
"$lib_dir/$slug/0-load.php",
14+
"$lib_dir/$slug/$basename.php",
15+
);
16+
foreach( $choices as $file_name ) {
17+
if ( is_readable( $file_name ) ) {
18+
require_once $file_name;
19+
return;
20+
}
21+
}
22+
trigger_error( "Cannot find a library with slug $slug.", E_USER_ERROR );
23+
}

0 commit comments

Comments
 (0)