YIT_Pointers

YIT Pointers.

Defined (1)

The class is defined in the following location(s).

/plugin-fw/lib/yit-pointers.php  
  1. class YIT_Pointers { 
  2.  
  3. /** 
  4. * @var YIT_Upgrade The main instance 
  5. */ 
  6. protected static $_instance; 
  7.  
  8. /** 
  9. * @var screen id where to show pointer 
  10. */ 
  11. public $screen_ids = array(); 
  12.  
  13. public $pointers = array(); 
  14.  
  15. public $special_screen = array(); 
  16.  
  17. protected $_plugins_registered = array(); 
  18.  
  19. protected $_default_pointer = array(); 
  20.  
  21. protected $_default_position = array( 'edge' => 'left', 'align' => 'center' ); 
  22.  
  23. /** 
  24. * Construct 
  25. * @author Andrea Grillo <andrea.grillo@yithemes.com> 
  26. * @since 1.0 
  27. */ 
  28. public function __construct() { 
  29.  
  30. $this->_default_pointer['plugins'] = array( 
  31. 'screen_id' => 'plugins',  
  32. 'options' => array( 
  33. 'content' => sprintf( '<h3> %s </h3> <p> %s </p> <p> %s <a href="http://yithemes.com/product-category/plugins/" target="_blank">Yithemes.com</a> %s 
  34. <a href="https://profiles.wordpress.org/yithemes/" target="_blank">Wordpress.org</a></p>',  
  35. __( 'Plugins Activated', 'yith-plugin-fw' ),  
  36. __( 'From now on, you can find all plugin options in YITH Plugins menu. 
  37. Plugin customization settings will be available as a new entry in YITH Plugins menu.', 'yith-plugin-fw' ),  
  38. __( 'Discover all our plugins available on:', 'yith-plugin-fw' ),  
  39. __( 'and', 'yith-plugin-fw' ) 
  40. ),  
  41. ),  
  42. ); 
  43.  
  44. $this->_default_pointer['update'] = array( 
  45. 'screen_id' => 'update',  
  46. 'options' => array( 
  47. 'content' => sprintf( '<h3> %s </h3> <p> %s </p> <p> %s <a href="http://yithemes.com/product-category/plugins/" target="_blank">Yithemes.com</a> %s 
  48. <a href="https://profiles.wordpress.org/yithemes/" target="_blank">Wordpress.org</a></p>',  
  49. __( 'Plugins Upgraded', 'yith-plugin-fw' ),  
  50. __( 'From now on, you can find the option panel of YITH plugins in YITH Plugins menu. 
  51. Every time one of our plugins is added, a new entry will be added to this menu. 
  52. For example, after the update, plugin options (such as for YITH WooCommerce Wishlist, YITH WooCommerce Ajax Search, etc.) 
  53. will be moved from previous location to YITH Plugins tab.', 'yith-plugin-fw' ),  
  54. __( 'Discover all our plugins available on:', 'yith-plugin-fw' ),  
  55. __( 'and', 'yith-plugin-fw' ) 
  56. ),  
  57. ),  
  58. ); 
  59.  
  60. $this->_default_pointer = $this->parse_args( $this->_default_pointer ); 
  61.  
  62. /** 
  63. * Screens that require a particular action 
  64. */ 
  65. $this->special_screen = apply_filters( 'yit-pointer-special-screen', array( 'plugins', 'update' ) ); 
  66.  
  67. add_action( 'admin_enqueue_scripts', array( $this, 'pointer_load' ) ); 
  68. add_action( 'admin_init', array( $this, 'add_pointers' ), 100 ); 
  69.  
  70. public function parse_args( $args ) { 
  71. $default = array( 
  72. 'pointer_id' => 'yith_default_pointer',  
  73. 'target' => '#toplevel_page_yit_plugin_panel',  
  74. 'init' => null 
  75. ); 
  76.  
  77. foreach ( $args as $id => $pointer ) { 
  78. $args[ $id ] = wp_parse_args( $pointer, $default ); 
  79. $args[ $id ]['options']['position'] = $this->_default_position; 
  80.  
  81. return $args; 
  82.  
  83. public function add_pointers() { 
  84. if( ! empty( $this->screen_ids ) ) { 
  85. foreach( $this->screen_ids as $screen_id ) { 
  86. add_filter( "yit_pointers-{$screen_id}", array( $this, 'pointers' ) ); 
  87.  
  88. /** 
  89. * Main plugin Instance 
  90. * @static 
  91. * @return object Main instance 
  92. * @since 1.0 
  93. * @author Andrea Grillo <andrea.grillo@yithemes.com> 
  94. */ 
  95. public static function instance() { 
  96. if ( is_null( self::$_instance ) ) { 
  97. self::$_instance = new self(); 
  98.  
  99. return self::$_instance; 
  100.  
  101. public function register( $args ) { 
  102.  
  103. foreach ( $args as $id => $pointer ) { 
  104.  
  105. extract( $pointer ); 
  106.  
  107. if ( ! isset( $screen_id ) && ! empty( $screen_id ) && ! isset( $init ) && ! empty( $init ) ) { 
  108. return; 
  109.  
  110. if ( ! in_array( $screen_id, $this->screen_ids ) ) { 
  111. $this->screen_ids[] = $screen_id; 
  112.  
  113. $this->pointers[$screen_id][$pointer_id] = array( 
  114. 'target' => $target,  
  115. 'options' => array( 
  116. 'content' => $content,  
  117. 'position' => $position,  
  118. ),  
  119. 'init' => isset( $init ) ? $init : false 
  120. ); 
  121.  
  122. public function get_plugins_init( $screen_id ) { 
  123.  
  124. $registered = array(); 
  125.  
  126. foreach( $this->pointers[ $screen_id ] as $pointer_id => $pointer ) { 
  127. $registered[ $pointer['init'] ] = $pointer_id; 
  128.  
  129. return $registered; 
  130.  
  131. public function pointer_load( $hook_suffix ) { 
  132.  
  133. /** 
  134. * Get pointers for this screen 
  135. */ 
  136. $screen = get_current_screen(); 
  137. $pointers = apply_filters( "yit_pointers-{$screen->id}", array() ); 
  138.  
  139. if ( ! $pointers || ! is_array( $pointers ) ) { 
  140. return; 
  141.  
  142. /** 
  143. * Get dismissed pointers 
  144. */ 
  145. $dismissed = explode( ', ', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) ); 
  146. $valid_pointers = array(); 
  147. //$point_id = null; 
  148.  
  149. /** 
  150. * show pointers only on plugin activate action 
  151. */ 
  152. if( in_array( $screen->id, $this->special_screen ) ) { 
  153.  
  154. $show = false; 
  155. $registered = $this->get_plugins_init( $screen->id ); 
  156. $recently_activate = get_option( 'yit_recently_activated', array() ); 
  157.  
  158. /** 
  159. * For "plugins" screen 
  160. */ 
  161. $is_single_activate = ( isset( $_GET['activate'] ) && 'true' == $_GET['activate'] ) ? true : false; 
  162. $is_multi_activate = ( isset( $_GET['activate-multi'] ) && 'true' == $_GET['activate-multi'] ) ? true : false; 
  163.  
  164. /** 
  165. * For "update" screen 
  166. * Single plugin update use GET method 
  167. * Multi update plugins with bulk action send two post args called "action" and "action2" 
  168. * action refer to first bulk action button (at the top of plugins table) 
  169. * action2 refer to last bulk action button (at the bottom of plugins table) 
  170. */ 
  171. $is_single_upgrade = ( isset( $_GET['action'] ) && 'upgrade-plugin' == $_GET['action'] ) ? true : false; 
  172. $is_multi_upgrade = ( isset( $_POST['action'] ) && 'update-selected' == $_POST['action'] ) || ( isset( $_POST['action2'] ) && 'update-selected' == $_POST['action2'] ) ? true: false; 
  173.  
  174. if( $is_single_activate || $is_single_upgrade ) { 
  175.  
  176. $point_id = ''; 
  177.  
  178. /** 
  179. * Single activation plugin 
  180. * Single update plugin 
  181. */ 
  182. foreach( $registered as $init => $p_id ) { 
  183. if ( in_array( $init, $recently_activate ) ) { 
  184. $point_id = $p_id; 
  185. $pointer = $pointers[ $point_id ]; 
  186.  
  187. /** 
  188. * Sanity check 
  189. */ 
  190. if ( ! ( in_array( $point_id, $dismissed ) || empty( $pointer ) || empty( $point_id ) || empty( $pointer['target'] ) || empty( $pointer['options'] ) ) ) { 
  191. /** 
  192. * Add the pointer to $valid_pointers array 
  193. */ 
  194. $pointer['pointer_id'] = $point_id; 
  195. $valid_pointers['pointers'][] = $pointer; 
  196. $show = true; 
  197. break; 
  198. } else if( $is_multi_activate || $is_multi_upgrade ) { 
  199.  
  200. /** 
  201. * Bulk Action: multi plugins activation 
  202. * Bulk Action: multi plugins update 
  203. */ 
  204. $point_id = array(); 
  205. $screen_id = $screen->id; 
  206.  
  207. if( $is_multi_upgrade && isset( $_POST['checked'] ) && ( count( $_POST['checked'] ) > 0 ) ) { 
  208. $recently_activate = $_POST['checked']; 
  209. $screen_id = 'update'; 
  210. $pointers = apply_filters( "yit_pointers-{$screen_id}", array() ); 
  211.  
  212. foreach ( $registered as $init => $p_id ) { 
  213. if ( in_array( $init, $recently_activate ) ) { 
  214. $point_id[] = $p_id; 
  215.  
  216. /** 
  217. * Bulk Action: Activate Plugins 
  218. * count( $point_id ) is the number of YITH plugins that have registered specific pointers 
  219. * case 0 -> No pointers -> Exit 
  220. * case 1 -> Only one pointers to show -> Use the specific plugin pointer 
  221. * defautl -> Two or more plugins need to show a pointer -> use a generic pointers 
  222. */ 
  223. switch ( count( $point_id ) ) { 
  224. case 0: 
  225. $show = false; 
  226. break; 
  227.  
  228. case 1: 
  229. $point_id = array_pop( $point_id ); 
  230. $pointer = $pointers[$point_id]; 
  231. /** 
  232. * Sanity check 
  233. */ 
  234. if ( ! ( in_array( $point_id, $dismissed ) || empty( $pointer ) || empty( $point_id ) || empty( $pointer['target'] ) || empty( $pointer['options'] ) ) ) { 
  235. /** 
  236. * Add the pointer to $valid_pointers array 
  237. */ 
  238. $pointer['pointer_id'] = $point_id; 
  239. $valid_pointers['pointers'][] = $pointer; 
  240. $show = true; 
  241. break; 
  242.  
  243. default: 
  244. $valid_pointers['pointers'][] = $this->_default_pointer[ $screen_id ]; 
  245. $show = true; 
  246. break; 
  247.  
  248. update_option( 'yit_recently_activated', array() ); 
  249.  
  250. if( ! $show ) { 
  251. return; 
  252.  
  253. } else { 
  254. /** 
  255. * Check pointers and remove dismissed ones. 
  256. */ 
  257. foreach ( $pointers as $pointer_id => $pointer ) { 
  258.  
  259. /** 
  260. * Sanity check 
  261. */ 
  262. if ( in_array( $pointer_id, $dismissed ) || empty( $pointer ) || empty( $pointer_id ) || empty( $pointer['target'] ) || empty( $pointer['options'] ) ) { 
  263. continue; 
  264.  
  265. $pointer['pointer_id'] = $pointer_id; 
  266.  
  267. /** 
  268. * Add the pointer to $valid_pointers array 
  269. */ 
  270. $valid_pointers['pointers'][] = $pointer; 
  271.  
  272. /** 
  273. * No valid pointers? Stop here. 
  274. */ 
  275. if ( empty( $valid_pointers ) ) { 
  276. return; 
  277.  
  278. $script_file = function_exists( 'yit_load_js_file' ) ? yit_load_js_file( 'yit-wp-pointer.js' ) : 'yit-wp-pointer.min.js'; 
  279.  
  280. /** 
  281. * Enqueue wp-pointer script and style 
  282. */ 
  283. wp_enqueue_style( 'wp-pointer' ); 
  284. wp_enqueue_script( 'wp-pointer' ); 
  285.  
  286. wp_enqueue_script( 'yit-wp-pointer', YIT_CORE_PLUGIN_URL . '/assets/js/' . $script_file, array( 'wp-pointer' ), false, true ); 
  287. wp_localize_script( 'yit-wp-pointer', 'custom_pointer', $valid_pointers ); 
  288.  
  289. public function pointers( $pointers ) { 
  290. $screen_id = str_replace( 'yit_pointers-', '', current_filter() ); 
  291. $pointers_to_add = $this->get_pointers( $screen_id ); 
  292.  
  293. return ! empty( $pointers_to_add ) ? array_merge( $pointers, $pointers_to_add ) : $pointers; 
  294.  
  295. public function get_pointers( $screen_id ) { 
  296. return isset( $this->pointers[ $screen_id ] ) ? $this->pointers[ $screen_id ] : array();