Skip to content

Commit

Permalink
Added [device_is*] shortcodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Hendricks committed Apr 1, 2019
1 parent 4711b23 commit 3211c74
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 16 deletions.
21 changes: 5 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,28 @@

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
- PHP 5.6 or higher

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.
Expand All @@ -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 );
```

Expand All @@ -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:

Expand Down
21 changes: 21 additions & 0 deletions app/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

}

}
3 changes: 3 additions & 0 deletions app/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public function load_plugin() {
// Load core plugin logic
Core::init();

// Load shortcodes
Shortcodes\Shortcode_Loader::init();

}

/**
Expand Down
Empty file removed app/Shortcodes/.gitkeep
Empty file.
63 changes: 63 additions & 0 deletions app/Shortcodes/Device_Matches_Shortcode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
namespace CloudVerve\Detect_Mobile_Device\Shortcodes;
use CloudVerve\Detect_Mobile_Device\Plugin;
use CloudVerve\Detect_Mobile_Device\Helpers;

final class Device_Matches_Shortcode extends Plugin {

private static $class;

public static function load() {

if ( !isset( self::$class ) && !( self::$class instanceof Device_Matches_Shortcode ) ) {

self::$class = new Device_Matches_Shortcode();

// [device_is]
if ( !shortcode_exists( 'device_is' ) ) {
add_shortcode( 'device_is', array( self::$class, 'device_match_shortcode' ) );
}

// [device_is_not]
if ( !shortcode_exists( 'device_is_not' ) ) {
add_shortcode( 'device_is_not', array( self::$class, 'device_match_shortcode' ) );
}

}

return self::$class;

}

/**
* Shortcodes: [device_is], [device_is_not]
*
* @since 1.0.0
*/
public static function device_match_shortcode( $atts = [], $content = '', $shortcode_name ) {

// Set default attributes
$atts = shortcode_atts( array(
'type' => 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 ) : '';

}

}
72 changes: 72 additions & 0 deletions app/Shortcodes/Device_Type_Shortcodes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
namespace CloudVerve\Detect_Mobile_Device\Shortcodes;
use CloudVerve\Detect_Mobile_Device\Plugin;

final class Device_Type_Shortcodes extends Plugin {

private static $class;

public static function load() {

if ( !isset( self::$class ) && !( self::$class instanceof Device_Type_Shortcodes ) ) {

self::$class = new Device_Type_Shortcodes();

// [device_is_mobile]
if ( !shortcode_exists( 'device_is_mobile' ) ) {
add_shortcode( 'device_is_mobile', array( self::$class, 'device_is_type_shortcode' ) );
}

// [device_is_phone]
if ( !shortcode_exists( 'device_is_phone' ) ) {
add_shortcode( 'device_is_phone', array( self::$class, 'device_is_type_shortcode' ) );
}

// [device_is_mobile]
if ( !shortcode_exists( 'device_is_tablet' ) ) {
add_shortcode( 'device_is_tablet', array( self::$class, 'device_is_type_shortcode' ) );
}

// [device_is_desktop]
if ( !shortcode_exists( 'device_is_desktop' ) ) {
add_shortcode( 'device_is_desktop', array( self::$class, 'device_is_type_shortcode' ) );
}

}

return self::$class;

}

/**
* Shortcodes: [device_is_{type}]
*
* @since 1.0.0
*/
public static function device_is_type_shortcode( $atts = [], $content = '', $shortcode_name ) {

if( empty( $content ) ) return $content;

$device = new \Mobile_Detect;
$device_match = false;

switch( true ) {
case $shortcode_name == 'device_is_mobile' && $device->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 ) : '';

}

}
37 changes: 37 additions & 0 deletions app/Shortcodes/Shortcode_Loader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
namespace CloudVerve\Detect_Mobile_Device\Shortcodes;
use CloudVerve\Detect_Mobile_Device\Plugin;

final class Shortcode_Loader extends Plugin {

/**
* Shortcode loader
*
* @since 1.0.0
*/

private static $class;
private static $shortcodes;

public static function init() {

if ( !isset( self::$class ) && !( self::$class instanceof Shortcode_Loader ) ) {

self::$class = new Shortcode_Loader();

self::$shortcodes = [
Device_Type_Shortcodes::class,
Device_Matches_Shortcode::class
];

foreach( self::$shortcodes as $shortcodeClass ) {
if( class_exists( $shortcodeClass ) ) $shortcodeClass::load();
}

}

return self::$class;

}

}
Empty file removed assets/.gitkeep
Empty file.

0 comments on commit 3211c74

Please sign in to comment.