| 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 |
* Feature pointer for visual editor when post contains MS Office files. |
| 61 |
*/ |
| 62 |
public static function dg424_FeaturePointer() { |
| 63 |
$title = '<h3>' . __( 'More Thumbnails!', 'document-gallery' ) . '</h3>'; |
| 64 |
$body = '<p>' . sprintf( __( 'It looks like your gallery includes Word, PowerPoint, or some other Microsoft Office ' . |
| 65 |
'files. Did you know that Document Gallery can generate thumbanils for these too? ' . |
| 66 |
'<a href="%s">Learn more.</a>', 'document-gallery' ), |
| 67 |
'options-general.php?page=' . DG_OPTION_NAME . '&tab=thumber-co-tab' ) . '</p>'; |
| 68 |
$position = array( 'edge' => 'top', 'align' => 'left', 'defer_loading' => true ); |
| 69 |
self::printFeaturePointer( '#insert-media-button', array( 'content' => $title . $body, 'position' => $position ) ); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Print the pointer JavaScript data. |
| 74 |
* NOTE: Taken from WP_Internal_Pointers. |
| 75 |
* |
| 76 |
* @param string $selector The HTML elements, on which the pointer should be attached. |
| 77 |
* @param mixed[] $args Arguments to be passed to the pointer JS (see wp-pointer.js). |
| 78 |
*/ |
| 79 |
private static function printFeaturePointer( $selector, $args ) { |
| 80 |
if ( empty( $selector ) || empty( $args ) || empty( $args['content'] ) ) |
| 81 |
return; |
| 82 |
|
| 83 |
// optimize version_compare as much as possible based on PHP version |
| 84 |
$trace = version_compare( PHP_VERSION, '5.4', '>=' ) |
| 85 |
? debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 2 ) |
| 86 |
: debug_backtrace( false ); |
| 87 |
$pointer_id = self::getFeaturePointerIdFromMethodName( $trace[1]['function'] ); |
| 88 |
|
| 89 |
// NOTE: If below JS is modified, *ensure* that minified is also updated |
| 90 |
?> |
| 91 |
<script> |
| 92 |
<?php if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ): ?> |
| 93 |
(function($){ |
| 94 |
var options = <?php echo wp_json_encode( $args ); ?>, setup; |
| 95 |
|
| 96 |
if ( ! options ) |
| 97 |
return; |
| 98 |
|
| 99 |
options = $.extend( options, { |
| 100 |
close: function() { |
| 101 |
$.post( ajaxurl, { |
| 102 |
pointer: '<?php echo $pointer_id; ?>', |
| 103 |
action: 'dismiss-wp-pointer' |
| 104 |
}); |
| 105 |
} |
| 106 |
}); |
| 107 |
|
| 108 |
setup = function() { |
| 109 |
$('<?php echo $selector; ?>').first().pointer( options ).pointer('open'); |
| 110 |
}; |
| 111 |
|
| 112 |
if ( options.position && options.position.defer_loading ) { |
| 113 |
var hdlr = function () { |
| 114 |
setup(); |
| 115 |
$('<?php echo $selector; ?>').off('ready.dg', hdlr); |
| 116 |
}; |
| 117 |
$('<?php echo $selector; ?>').on('ready.dg', hdlr); |
| 118 |
} else { |
| 119 |
$(document).ready(setup); |
| 120 |
} |
| 121 |
|
| 122 |
})( jQuery ); |
| 123 |
<?php else: ?> |
| 124 |
(function(a){var b=<?php echo wp_json_encode( $args ); ?>,c;if(b)if(b=a.extend(b,{close:function(){a.post(ajaxurl,{pointer:"<?php echo $pointer_id; ?>",action:"dismiss-wp-pointer"})}}),c=function(){a("<?php echo $selector; ?>").first().pointer(b).pointer("open")},b.position&&b.position.defer_loading){var d=function(){c();a("<?php echo $selector; ?>").off("ready.dg",d)};a("<?php echo $selector; ?>").on("ready.dg",d)}else a(document).ready(c)})(jQuery); |
| 125 |
<?php endif; ?> |
| 126 |
</script> |
| 127 |
<?php |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* @param string $method The method name. |
| 132 |
* @return string The feature pointer ID. |
| 133 |
*/ |
| 134 |
private static function getFeaturePointerIdFromMethodName( $method ) { |
| 135 |
return rtrim( substr( $method, 0, strlen( $method ) - strlen( self::$feature_pointer_method_suffix ) ), '_' ); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Reflectively retrieves all of the feature pointer methods, which must end with the feature pointer method suffix. |
| 140 |
* @return string[] The names of all public class methods matching the feature pointer pattern. |
| 141 |
*/ |
| 142 |
private static function getFeaturePointerMethods() { |
| 143 |
if ( ! isset( self::$feature_pointer_methods ) ) { |
| 144 |
$reflect = new ReflectionClass( __CLASS__ ); |
| 145 |
$methods = $reflect->getMethods( ReflectionMethod::IS_STATIC | ReflectionMethod::IS_PUBLIC ); |
| 146 |
self::$feature_pointer_methods = |
| 147 |
array_map( |
| 148 |
array( __CLASS__, 'getNameFromMethod' ), |
| 149 |
array_filter( $methods, array( __CLASS__, 'isFilterPointerMethod' ) ) ); |
| 150 |
} |
| 151 |
|
| 152 |
return self::$feature_pointer_methods; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* @param $method ReflectionMethod The method to extract name from. |
| 157 |
*/ |
| 158 |
private static function getNameFromMethod( $method ) { |
| 159 |
return $method->name; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* @param $method ReflectionMethod The method name. |
| 164 |
* @return bool Whether the method name matches the filter pointer pattern. |
| 165 |
*/ |
| 166 |
private static function isFilterPointerMethod( $method ) { |
| 167 |
// NOTE: ReflectionClass returns methods that are static OR public -- must reduce to an AND |
| 168 |
return $method->isPublic() && $method->isStatic() && DG_Util::endsWith( $method->name, self::$feature_pointer_method_suffix ); |
| 169 |
} |
| 170 |
} |