Advertisement
Guest User

WP Pointers

a guest
Sep 30th, 2014
835
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.41 KB | None | 0 0
  1. // Create as a class
  2. class testWPpointers {
  3.    
  4.     // Define pointer version
  5.     const DISPLAY_VERSION = 'v1.0';
  6.    
  7.     // Initiate construct
  8.     function __construct () {
  9.         add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts'));  // Hook to admin_enqueue_scripts
  10.     }
  11.    
  12.     function admin_enqueue_scripts () {
  13.        
  14.         // Check to see if user has already dismissed the pointer tour
  15.         $dismissed = explode (',', get_user_meta (wp_get_current_user ()->ID, 'dismissed_wp_pointers', true));
  16.         $do_tour = !in_array ('test_wp_pointer', $dismissed);
  17.        
  18.         // If not, we are good to continue
  19.         if ($do_tour) {
  20.            
  21.             // Enqueue necessary WP scripts and styles
  22.             wp_enqueue_style ('wp-pointer');
  23.             wp_enqueue_script ('wp-pointer');
  24.            
  25.             // Finish hooking to WP admin areas
  26.             add_action('admin_print_footer_scripts', array($this, 'admin_print_footer_scripts'));  // Hook to admin footer scripts
  27.             add_action('admin_head', array($this, 'admin_head'));  // Hook to admin head
  28.         }
  29.     }
  30.    
  31.     // Used to add spacing between the two buttons in the pointer overlay window.
  32.     function admin_head () {
  33.         ?>
  34.         <style type="text/css" media="screen">
  35.             #pointer-primary {
  36.                 margin: 0 5px 0 0;
  37.             }
  38.         </style>
  39.         <?php
  40.     }
  41.    
  42.     // Define footer scripts
  43.     function admin_print_footer_scripts () {
  44.        
  45.         // Define global variables
  46.         global $pagenow;
  47.         global $current_user;
  48.        
  49.         //*****************************************************************************************************
  50.         // This is our array of individual pointers.
  51.         // -- The array key should be unique.  It is what will be used to 'advance' to the next pointer.
  52.         // -- The 'id' should correspond to an html element id on the page.
  53.         // -- The 'content' will be displayed inside the pointer overlay window.
  54.         // -- The 'button2' is the text to show for the 'action' button in the pointer overlay window.
  55.         // -- The 'function' is the method used to reload the window (or relocate to a new window).
  56.         //    This also creates a query variable to add to the end of the url.
  57.         //    The query variable is used to determine which pointer to display.
  58.         //*****************************************************************************************************
  59.         $tour = array (
  60.             'quick_press' => array (
  61.                 'id' => '#dashboard_quick_press',
  62.                 'content' => '<h3>' . __('Congratulations!', 'test_lang') . '</h3>'
  63.                     . '<p><strong>' . __('WP Pointers is working properly.', 'test_lang') . '</strong></p>'
  64.                     . '<p>' . __('This pointer is attached to the "Quick Draft" admin widget.', 'test_lang') . '</p>'
  65.                     . '<p>' . __('Our next pointer will take us to the "Settings" admin menu.', 'test_lang') . '</p>',
  66.                 'button2' => __('Next', 'test_lang'),
  67.                 'function' => 'window.location="' . $this->get_admin_url('options-general.php', 'site_title') . '"'  // We are relocating to "Settings" page with the 'site_title' query var
  68.                 ),
  69.             'site_title' => array (
  70.                 'id' => '#blogname',
  71.                 'content' => '<h3>' . __('Moving along to Site Title.', 'test_lang') . '</h3>'
  72.                 . '<p><strong>' . __('Another WP Pointer.', 'test_lang') . '</strong></p>'
  73.                 . '<p>' . __('This pointer is attached to the "Blog Title" input field.', 'test_lang') . '</p>',
  74.                 'button2' => __('Next', 'test_lang'),
  75.                 'function' => 'window.location="' . $this->get_admin_url('index.php', 'quick_press_last') . '"'  // We are relocating back to "Dashboard" with 'quick_press_last' query var
  76.                 ),
  77.             'quick_press_last' => array (
  78.                 'id' => '#dashboard_quick_press',
  79.                 'content' => '<h3>' . __('This concludes our WP Pointers tour.', 'test_lang') . '</h3>'
  80.                 . '<p><strong>' . __('Last WP Pointer.', 'test_lang') . '</strong></p>'
  81.                 . '<p>' . __('When closing the pointer tour; it will be saved in the users custom meta.  The tour will NOT be shown to that user again.', 'test_lang') . '</p>'
  82.                 )
  83.             );
  84.        
  85.         // Determine which tab is set in the query variable
  86.         $tab = isset($_GET['tab']) ? $_GET['tab'] : '';
  87.         // Define other variables
  88.         $function = '';
  89.         $button2 = '';
  90.         $options = array ();
  91.         $show_pointer = false;
  92.    
  93.         // *******************************************************************************************************
  94.         // This will be the first pointer shown to the user.
  95.         // If no query variable is set in the url.. then the 'tab' cannot be determined... and we start with this pointer.
  96.         // *******************************************************************************************************
  97.         if (!array_key_exists($tab, $tour)) {
  98.            
  99.             $show_pointer = true;
  100.             $file_error = true;
  101.            
  102.             $id = '#dashboard_right_now';  // Define ID used on page html element where we want to display pointer
  103.             $content = '<h3>' . sprintf (__('Test WP Pointers %s', 'test_lang'), self::DISPLAY_VERSION) . '</h3>';
  104.             $content .= __('<p>Welcome to Test WP Pointers admin tour!</p>', 'test_lang');
  105.             $content .= __('<p>This pointer is attached to the "At a Glance" dashboard widget.</p>', 'test_lang');
  106.             $content .= '<p>' . __('Click the <em>Begin Tour</em> button to get started.', 'test_lang' ) . '</p>';
  107.  
  108.             $options = array (
  109.                 'content' => $content,
  110.                 'position' => array ('edge' => 'top', 'align' => 'left')
  111.                 );
  112.             $button2 = __('Begin Tour', 'test_lang' );
  113.             $function = 'document.location="' . $this->get_admin_url('index.php', 'quick_press') . '";';
  114.         }
  115.         // Else if the 'tab' is set in the query variable.. then we can determine which pointer to display
  116.         else {
  117.            
  118.             if ($tab != '' && in_array ($tab, array_keys ($tour))) {
  119.                
  120.                 $show_pointer = true;
  121.                
  122.                 if (isset ($tour[$tab]['id'])) {
  123.                     $id = $tour[$tab]['id'];
  124.                 }
  125.                
  126.                 $options = array (
  127.                     'content' => $tour[$tab]['content'],
  128.                     'position' => array ('edge' => 'top', 'align' => 'left')
  129.                 );
  130.                    
  131.                 $button2 = false;
  132.                 $function = '';
  133.                
  134.                 if (isset ($tour[$tab]['button2'])) {
  135.                     $button2 = $tour[$tab]['button2'];
  136.                 }
  137.                 if (isset ($tour[$tab]['function'])) {
  138.                     $function = $tour[$tab]['function'];
  139.                 }
  140.             }
  141.         }
  142.        
  143.         // If we are showing a pointer... let's load the jQuery.
  144.         if ($show_pointer) {
  145.             $this->make_pointer_script ($id, $options, __('Close', 'test_lang'), $button2, $function);
  146.         }
  147.     }
  148.    
  149.     // This function is used to reload the admin page.
  150.     // -- $page = the admin page we are passing (index.php or options-general.php)
  151.     // -- $tab = the NEXT pointer array key we want to display
  152.     function get_admin_url($page, $tab) {
  153.        
  154.         $url = admin_url();
  155.         $url .= $page.'?tab='.$tab;
  156.    
  157.         return $url;
  158.     }
  159.    
  160.     // Print footer scripts
  161.     function make_pointer_script ($id, $options, $button1, $button2=false, $function='') {
  162.        
  163.         ?>
  164.         <script type="text/javascript">
  165.            
  166.             (function ($) {
  167.                
  168.                 // Define pointer options
  169.                 var wp_pointers_tour_opts = <?php echo json_encode ($options); ?>, setup;
  170.            
  171.                 wp_pointers_tour_opts = $.extend (wp_pointers_tour_opts, {
  172.                    
  173.                     // Add 'Close' button
  174.                     buttons: function (event, t) {
  175.                        
  176.                         button = jQuery ('<a id="pointer-close" class="button-secondary">' + '<?php echo $button1; ?>' + '</a>');
  177.                         button.bind ('click.pointer', function () {
  178.                             t.element.pointer ('close');
  179.                         });
  180.                         return button;
  181.                     },
  182.                     close: function () {
  183.                        
  184.                         // Post to admin ajax to disable pointers when user clicks "Close"
  185.                         $.post (ajaxurl, {
  186.                             pointer: 'test_wp_pointer',
  187.                             action: 'dismiss-wp-pointer'
  188.                         });
  189.                     }
  190.                 });
  191.                
  192.                 // This is used for our "button2" value above (advances the pointers)
  193.                 setup = function () {
  194.                    
  195.                     $('<?php echo $id; ?>').pointer(wp_pointers_tour_opts).pointer('open');
  196.                    
  197.                     <?php if ($button2) { ?>
  198.                        
  199.                         jQuery ('#pointer-close').after ('<a id="pointer-primary" class="button-primary">' + '<?php echo $button2; ?>' + '</a>');
  200.                         jQuery ('#pointer-primary').click (function () {
  201.                             <?php echo $function; ?>  // Execute button2 function
  202.                         });
  203.                         jQuery ('#pointer-close').click (function () {
  204.                            
  205.                             // Post to admin ajax to disable pointers when user clicks "Close"
  206.                             $.post (ajaxurl, {
  207.                                 pointer: 'test_wp_pointer',
  208.                                 action: 'dismiss-wp-pointer'
  209.                             });
  210.                         })
  211.                     <?php } ?>
  212.                 };
  213.            
  214.                 if (wp_pointers_tour_opts.position && wp_pointers_tour_opts.position.defer_loading) {
  215.                    
  216.                     $(window).bind('load.wp-pointers', setup);
  217.                 }
  218.                 else {
  219.                     setup ();
  220.                 }
  221.             }) (jQuery);
  222.         </script>
  223.         <?php
  224.     }
  225. }
  226. $testWPpointers = new testWPpointers();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement