-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Daniel Hendricks
committed
Apr 1, 2019
1 parent
4711b23
commit 3211c74
Showing
8 changed files
with
201 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) : ''; | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) : ''; | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.