| 1 |
<?php |
| 2 |
defined( 'WPINC' ) OR exit; |
| 3 |
|
| 4 |
class DG_FeaturePointers { |
| 5 |
/** |
| 6 |
* @var string Each method used to output a feature pointer must end in this suffix. |
| 7 |
*/ |
| 8 |
private static $feature_pointer_method_suffix = 'FeaturePointer'; |
| 9 |
|
| 10 |
/** |
| 11 |
* @var string[] The cached pointer methods to avoid reflecting multiple times in a given session. |
| 12 |
*/ |
| 13 |
private static $feature_pointer_methods; |
| 14 |
|
| 15 |
/** |
| 16 |
* Enqueues the wp-pointer CSS/JS along with any specific feature pointers. |
| 17 |
*/ |
| 18 |
public static function enqueueScripts() { |
| 19 |
$seen = explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) ); |
| 20 |
|
| 21 |
$do_add_script = false; |
| 22 |
foreach ( self::getFeaturePointerMethods() as $method ) { |
| 23 |
$fp_id = self::getFeaturePointerIdFromMethodName( $method ); |
| 24 |
if ( ! in_array( $fp_id, $seen ) ) { |
| 25 |
$do_add_script = true; |
| 26 |
add_action( 'admin_print_footer_scripts', array( __CLASS__, $method ) ); |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
// enqueue WP core CSS/JS if we've got pointers |
| 31 |
if ( $do_add_script ) { |
| 32 |
wp_enqueue_script( 'wp-pointer' ); |
| 33 |
wp_enqueue_style( 'wp-pointer' ); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Feature pointer for DG settings. |
| 39 |
*/ |
| 40 |
public static function dg41_1FeaturePointer() { |
| 41 |
$title = '<h3>' . __( 'Configure Document Gallery', 'document-gallery' ) . '</h3>'; |
| 42 |
$body = '<p>' . __( 'Did you know Document Gallery has lots of configurable settings allowing you to fine tune ' . |
| 43 |
'what your users experience when viewing a gallery? <em>Click the <strong>Settings</strong> ' . |
| 44 |
'link above to see for yourself!</em>', 'document-gallery' ) . '</p>'; |
| 45 |
self::printFeaturePointer( '#the-list #document-gallery .row-actions', array( 'content' => $title . $body, 'position' => 'top' ) ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Feature pointer for Thumber.co tab in DG settings. |
| 50 |
*/ |
| 51 |
public static function dg41_2FeaturePointer() { |
| 52 |
$title = '<h3>' . __( 'More Thumbnails!', 'document-gallery' ) . '</h3>'; |
| 53 |
$body = '<p>' . __( 'If you need to generate thumbnails for Word documents, PowerPoints, and more then you ' . |
| 54 |
'need to check out Thumber.co. Free 1-week trial for a limited time! <em>Click the ' . |
| 55 |
'<strong>Thumber.co</strong> tab above to get started.</em>', 'document-gallery' ) . '</p>'; |
| 56 |
self::printFeaturePointer( '#thumber-co-tab-header', array( 'content' => $title . $body, 'position' => 'top' ) ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Print the pointer JavaScript data. |
| 61 |
* NOTE: Taken from WP_Internal_Pointers. |
| 62 |
* |
| 63 |
* @param string $selector The HTML elements, on which the pointer should be attached. |
| 64 |
* @param mixed[] $args Arguments to be passed to the pointer JS (see wp-pointer.js). |
| 65 |
*/ |
| 66 |
private static function printFeaturePointer( $selector, $args ) { |
| 67 |
if ( empty( $selector ) || empty( $args ) || empty( $args['content'] ) ) |
| 68 |
return; |
| 69 |
|
| 70 |
$trace = debug_backtrace(); |
| 71 |
$pointer_id = self::getFeaturePointerIdFromMethodName( $trace[1]['function'] ); |
| 72 |
?> |
| 73 |
<script>(function(b){var a=<?php echo wp_json_encode( $args ); ?>,c;a&&(a=b.extend(a,{close:function(){b.post(ajaxurl,{pointer:"<?php echo $pointer_id; ?>",action:"dismiss-wp-pointer"})}}),c=function(){b("<?php echo $selector; ?>").first().pointer(a).pointer("open")},a.position&&a.position.defer_loading?b(window).bind("load.wp-pointers",c):b(document).ready(c))})(jQuery);</script> |
| 74 |
<?php |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* @param string $method The method name. |
| 79 |
* @return string The feature pointer ID. |
| 80 |
*/ |
| 81 |
private static function getFeaturePointerIdFromMethodName( $method ) { |
| 82 |
return substr( $method, 0, strlen( $method ) - strlen( self::$feature_pointer_method_suffix ) ); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Reflectively retrieves all of the feature pointer methods, which must end with the feature pointer method suffix. |
| 87 |
* @return string[] The names of all public class methods matching the feature pointer pattern. |
| 88 |
*/ |
| 89 |
private static function getFeaturePointerMethods() { |
| 90 |
if ( ! isset( self::$feature_pointer_methods ) ) { |
| 91 |
$reflect = new ReflectionClass( __CLASS__ ); |
| 92 |
$methods = $reflect->getMethods( ReflectionMethod::IS_STATIC | ReflectionMethod::IS_PUBLIC ); |
| 93 |
self::$feature_pointer_methods = |
| 94 |
array_map( |
| 95 |
array( __CLASS__, 'getNameFromMethod' ), |
| 96 |
array_filter( $methods, array( __CLASS__, 'isFilterPointerMethod' ) ) ); |
| 97 |
} |
| 98 |
|
| 99 |
return self::$feature_pointer_methods; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* @param $method ReflectionMethod The method to extract name from. |
| 104 |
*/ |
| 105 |
private static function getNameFromMethod( $method ) { |
| 106 |
return $method->name; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* @param $method ReflectionMethod The method name. |
| 111 |
* @return bool Whether the method name matches the filter pointer pattern. |
| 112 |
*/ |
| 113 |
private static function isFilterPointerMethod( $method ) { |
| 114 |
// NOTE: ReflectionClass returns methods that are static OR public -- must reduce to an AND |
| 115 |
return $method->isPublic() && $method->isStatic() && DG_Util::endsWith( $method->name, self::$feature_pointer_method_suffix ); |
| 116 |
} |
| 117 |
} |