From 3211c74858c6bce2dca2f5be7c62eb66a6850c86 Mon Sep 17 00:00:00 2001 From: Daniel Hendricks Date: Sun, 31 Mar 2019 19:56:40 -0500 Subject: [PATCH] Added [device_is*] shortcodes --- README.md | 21 ++---- app/Helpers.php | 21 ++++++ app/Plugin.php | 3 + app/Shortcodes/.gitkeep | 0 app/Shortcodes/Device_Matches_Shortcode.php | 63 ++++++++++++++++++ app/Shortcodes/Device_Type_Shortcodes.php | 72 +++++++++++++++++++++ app/Shortcodes/Shortcode_Loader.php | 37 +++++++++++ assets/.gitkeep | 0 8 files changed, 201 insertions(+), 16 deletions(-) delete mode 100644 app/Shortcodes/.gitkeep create mode 100644 app/Shortcodes/Device_Matches_Shortcode.php create mode 100644 app/Shortcodes/Device_Type_Shortcodes.php create mode 100644 app/Shortcodes/Shortcode_Loader.php delete mode 100644 assets/.gitkeep diff --git a/README.md b/README.md index b469c7c..fb10640 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,6 @@ This WordPress plugin is uses the [MobileDetect](http://mobiledetect.net/) PHP library to extend `wp_is_mobile()` to exclude tablets and add device-specific filters and shortcodes. It was inspired by [Pothi Kalimuthu's](https://www.tinywp.in/?utm_source=github.com&utm_medium=campaign&utm_content=button&utm_campaign=detect-mobile-device) [Mobile Detect](https://wordpress.org/plugins/tinywp-mobile-detect/) plugin. -:bangbang: This is a work-in-progress. **Some features do not work, such as shortcodes.** - ## Requirements - WordPress 4.7 or higher @@ -17,30 +15,21 @@ This WordPress plugin is uses the [MobileDetect](http://mobiledetect.net/) PHP l If you're not sure if you meet these requirements, the plugin will tell you upon activation. -### Release TODO - -- [x] Add global functions -- [ ] Add shortcodes -- [x] Add device body classes -- [ ] Add all configuration constants -- [x] Add translation file -- [x] Modify [`wp_is_mobile()`](https://codex.wordpress.org/Function_Reference/wp_is_mobile) to return false if tablet, if enabled - ### Future Plans -- [ ] Add OS-specific global functions and shortcodes +- [ ] Add [OS-specific](https://github.com/matomo-org/device-detector) [detection](https://github.com/jenssegers/agent) - [ ] Add support for [mobile-detect.js](https://github.com/hgoebl/mobile-detect.js) - [ ] Add additional conditionals based on user agent ([examples](https://github.com/quentin389/UserAgentInfo#usage)) ### Installation -**TODO:** Download the release ZIP file (once available) and install as you normally would via the WP Admin plugins page. +:bangbang: Until I create a release, the only way to use this plugin is to clone it and run `composer install`. ### Configuration The following constants are available to modify behavior. They may be defined in your `wp-config.php`: -- `DMD_DISABLE_GLOBAL_FUNCTIONS` - If defined as true, global functions will not be created. +- `DMD_DISABLE_GLOBAL_FUNCTIONS` - If defined as true, [global functions](#option-2---global-functions) will not be created. - `DMD_DISABLE_SHORTCODES` - If defined as true, shortcodes will not be loaded. Useful if you only want this plugin to solely act as an autoloader for the [MobileDetect](http://mobiledetect.net/) PHP library. - `DMD_BODY_CLASS_PREFIX` - If defined as string, modifies the prefix added to device body classes. If false, disables addition of body classes. Defaults to `device`. - `DMD_MODIFY_WP_IS_MOBILE` - Modifies WordPress's built-in [`wp_is_mobile()`](https://codex.wordpress.org/Function_Reference/wp_is_mobile) function to return false for tablets. @@ -50,7 +39,7 @@ The following constants are available to modify behavior. They may be defined in ```php define( 'DMD_DISABLE_GLOBAL_FUNCTIONS', true ); define( 'DMD_DISABLE_SHORTCODES', false ); -define( 'DMD_BODY_CLASS_PREFIX', 'remote' ); // Resulting body classes: remote-mobile, remote-desktop, etc +define( 'DMD_BODY_CLASS_PREFIX', 'screen' ); // Resulting body classes: screen-mobile, screen-desktop, etc define( 'DMD_MODIFY_WP_IS_MOBILE', true ); ``` @@ -74,7 +63,7 @@ if( $device->isTablet() ) { :rotating_light: **NB!** The `isMobile` method returns true for both phones _and_ tablets. In my example above, I check for tablets first, else if not tablet but is mobile, it is a phone. Adjust your logic as desired. -### Option 2 - Use Global Functions +### Option 2 - Global Functions To supplement WordPress's built-in [`wp_is_mobile()`](https://codex.wordpress.org/Function_Reference/wp_is_mobile) function (which returns true for phone _and_ tablet), this plugin adds functions to specifically detect phones and tablets: diff --git a/app/Helpers.php b/app/Helpers.php index b3ed7d2..1bb1243 100644 --- a/app/Helpers.php +++ b/app/Helpers.php @@ -40,4 +40,25 @@ public static function get_json_contents( $filepath, $assoc = true ) { } + /** + * Return the device type + * + * @return string Device type + * @since 1.0.0 + */ + public static function get_remote_device_type() { + + $device = new \Mobile_Detect; + + switch( true ) { + case $device->isTablet(): + return 'tablet'; + case $device->isMobile() && !$device->isTablet(): + return 'phone'; + default: + return 'desktop'; + } + + } + } \ No newline at end of file diff --git a/app/Plugin.php b/app/Plugin.php index bbb627c..51c751b 100644 --- a/app/Plugin.php +++ b/app/Plugin.php @@ -57,6 +57,9 @@ public function load_plugin() { // Load core plugin logic Core::init(); + // Load shortcodes + Shortcodes\Shortcode_Loader::init(); + } /** diff --git a/app/Shortcodes/.gitkeep b/app/Shortcodes/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/app/Shortcodes/Device_Matches_Shortcode.php b/app/Shortcodes/Device_Matches_Shortcode.php new file mode 100644 index 0000000..1851951 --- /dev/null +++ b/app/Shortcodes/Device_Matches_Shortcode.php @@ -0,0 +1,63 @@ + null + ), $atts, $shortcode_name ); + + // Validate attributes + if( empty( $content ) || !$atts['type'] ) return $content; + $atts['type'] = explode( ',', $atts['type'] ); + + $device = new \Mobile_Detect; + $display_content = false; + + if( $shortcode_name == 'device_is' ) { + // [device_is] match + if( in_array( Helpers::get_remote_device_type(), $atts['type'] ) ) $display_content = true; + } else { + // [device_is_not] match + if( !in_array( Helpers::get_remote_device_type(), $atts['type'] ) ) $display_content = true; + } + + return $display_content ? do_shortcode( $content ) : ''; + + } + +} \ No newline at end of file diff --git a/app/Shortcodes/Device_Type_Shortcodes.php b/app/Shortcodes/Device_Type_Shortcodes.php new file mode 100644 index 0000000..826ba6d --- /dev/null +++ b/app/Shortcodes/Device_Type_Shortcodes.php @@ -0,0 +1,72 @@ +isMobile(): + $device_match = true; + break; + case $shortcode_name == 'device_is_phone' && $device->isMobile() && !$device->isTablet(): + $device_match = true; + break; + case $shortcode_name == 'device_is_tablet' && $device->isTablet(): + $device_match = true; + break; + case $shortcode_name == 'device_is_desktop' && !$device->isMobile(): + $device_match = true; + break; + } + + return $device_match ? do_shortcode( $content ) : ''; + + } + +} \ No newline at end of file diff --git a/app/Shortcodes/Shortcode_Loader.php b/app/Shortcodes/Shortcode_Loader.php new file mode 100644 index 0000000..34065ab --- /dev/null +++ b/app/Shortcodes/Shortcode_Loader.php @@ -0,0 +1,37 @@ +