PluginProbe
Document Gallery / trunk
Document Gallery vtrunk
trunk 0.8 0.8.5 1.0 1.0.1 1.0.2 1.0.3 1.0.4 1.1 1.2 1.2.1 1.3 1.3.1 1.4 1.4.1 1.4.2 1.4.3 2.0 2.0.1 2.0.10 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 94 releases
document-gallery / src / admin / class-feature-pointers.php

class-feature-pointers.php in Document Gallery trunk, at src/admin/class-feature-pointers.php

168 lines 7.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // PHP 5.4+ supports the more efficient DEBUG_BACKTRACE_IGNORE_ARGS flag
84 $trace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 2 );
85 $pointer_id = self::getFeaturePointerIdFromMethodName( $trace[1]['function'] );
86
87 // NOTE: If below JS is modified, *ensure* that minified is also updated
88 ?>
89 <script>
90 <?php if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ): ?>
91 (function($){
92 var options = <?php echo wp_json_encode( $args ); ?>, setup;
93
94 if ( ! options )
95 return;
96
97 options = $.extend( options, {
98 close: function() {
99 $.post( ajaxurl, {
100 pointer: '<?php echo $pointer_id; ?>',
101 action: 'dismiss-wp-pointer'
102 });
103 }
104 });
105
106 setup = function() {
107 $('<?php echo $selector; ?>').first().pointer( options ).pointer('open');
108 };
109
110 if ( options.position && options.position.defer_loading ) {
111 var hdlr = function () {
112 setup();
113 $('<?php echo $selector; ?>').off('ready.dg', hdlr);
114 };
115 $('<?php echo $selector; ?>').on('ready.dg', hdlr);
116 } else {
117 $(document).ready(setup);
118 }
119
120 })( jQuery );
121 <?php else: ?>
122 (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);
123 <?php endif; ?>
124 </script>
125 <?php
126 }
127
128 /**
129 * @param string $method The method name.
130 * @return string The feature pointer ID.
131 */
132 private static function getFeaturePointerIdFromMethodName( $method ) {
133 return rtrim( substr( $method, 0, strlen( $method ) - strlen( self::$feature_pointer_method_suffix ) ), '_' );
134 }
135
136 /**
137 * Reflectively retrieves all of the feature pointer methods, which must end with the feature pointer method suffix.
138 * @return string[] The names of all public class methods matching the feature pointer pattern.
139 */
140 private static function getFeaturePointerMethods() {
141 if ( ! isset( self::$feature_pointer_methods ) ) {
142 $reflect = new ReflectionClass( __CLASS__ );
143 $methods = $reflect->getMethods( ReflectionMethod::IS_STATIC | ReflectionMethod::IS_PUBLIC );
144 self::$feature_pointer_methods =
145 array_map(
146 array( __CLASS__, 'getNameFromMethod' ),
147 array_filter( $methods, array( __CLASS__, 'isFilterPointerMethod' ) ) );
148 }
149
150 return self::$feature_pointer_methods;
151 }
152
153 /**
154 * @param $method ReflectionMethod The method to extract name from.
155 */
156 private static function getNameFromMethod( $method ) {
157 return $method->name;
158 }
159
160 /**
161 * @param $method ReflectionMethod The method name.
162 * @return bool Whether the method name matches the filter pointer pattern.
163 */
164 private static function isFilterPointerMethod( $method ) {
165 // NOTE: ReflectionClass returns methods that are static OR public -- must reduce to an AND
166 return $method->isPublic() && $method->isStatic() && DG_Util::endsWith( $method->name, self::$feature_pointer_method_suffix );
167 }
168 }