Google_Maps_Builder_Core_Activate

Class Google_Maps_Builder_Core_Activate.

Defined (1)

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

/vendor/wordimpress/maps-builder-core/includes/class-gmc-activate.php  
  1. class Google_Maps_Builder_Core_Activate { 
  2.  
  3. /** 
  4. * API Nag Meta Tag 
  5. * @var $nag_meta_key 
  6. */ 
  7. protected $nag_meta_key; 
  8.  
  9.  
  10. /** 
  11. * Initialize the plugin by setting localization and loading public scripts. 
  12. * and styles. 
  13. * @since 1.0.0 
  14. */ 
  15. public function __construct() { 
  16.  
  17. $this->nag_meta_key = 'gmb_api_activation_ignore'; 
  18.  
  19. // Activate plugin when new blog is added. 
  20. add_action( 'wpmu_new_blog', array( $this, 'activate_new_site' ) ); 
  21.  
  22. //Activation tooltips. 
  23. add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_pointer_script_style' ) ); 
  24.  
  25. //Init CPT (after CMB2 -> hence the 10000 priority). 
  26. add_action( 'init', array( $this, 'setup_post_type' ), 10000 ); 
  27.  
  28.  
  29. //API Admin Notice for New Installs. 
  30. add_action( 'current_screen', array( $this, 'api_notice_ignore' ) ); 
  31. add_action( 'admin_notices', array( $this, 'activation_notice' ) ); 
  32.  
  33.  
  34. /** 
  35. * Maps activation banner. 
  36. * Welcome the user and provide instructions to create and add a Google Maps API key. 
  37. * @see http://stackoverflow.com/questions/2769148/whats-the-api-key-for-in-google-maps-api-v3/37994162#37994162 
  38. * @since 1.0 
  39. */ 
  40. public function activation_notice() { 
  41.  
  42. $current_user = wp_get_current_user(); 
  43.  
  44. // If the user has already dismissed our alert, bounce. 
  45. if ( get_user_meta( $current_user->ID, $this->nag_meta_key ) ) { 
  46. return; 
  47.  
  48. //Only display if an API key is not entered. 
  49. $api_key = gmb_get_option( 'gmb_maps_api_key' ); 
  50.  
  51. if ( ! empty( $api_key ) ) { 
  52. return; 
  53.  
  54. //Only display if this is a new install (no maps). 
  55. $count_maps = wp_count_posts( 'google_maps' ); 
  56. if ( $count_maps->publish != 0 ) { 
  57. return; 
  58.  
  59. //Don't show on the plugin's settings screen. 
  60. if ( isset( $_GET['page'] ) && $_GET['page'] == 'gmb_settings' ) { 
  61. return; 
  62.  
  63.  
  64. //Free pr 
  65. //@TODO: Provide support link 
  66. $support_link = 'https://wordpress.org/plugins/google-maps-builder/'; 
  67.  
  68. ?> 
  69.  
  70. <style> 
  71.  
  72. div.gmb-svg-banner { 
  73. float: left; 
  74.  
  75. div.gmb-svg-banner > svg { 
  76. width: 50px; 
  77. height: 50px; 
  78. float: left; 
  79.  
  80. div.gmb-api-alert.updated { 
  81. padding: 1em 2em; 
  82. position: relative; 
  83. border-color: #66BB6A; 
  84.  
  85. div.gmb-api-alert img { 
  86. max-width: 50px; 
  87. position: relative; 
  88. top: 1em; 
  89.  
  90. div.gmb-banner-content-wrap { 
  91. margin: 0 30px 0 70px; 
  92.  
  93. div.gmb-api-alert h3 { 
  94. font-weight: 300; 
  95. margin: 5px 0 0; 
  96.  
  97. div.gmb-api-alert h3 span { 
  98. font-weight: 600; 
  99. color: #66BB6A; 
  100.  
  101. div.gmb-api-alert .alert-actions { 
  102. position: relative; 
  103. left: 70px; 
  104.  
  105. div.gmb-api-alert a { 
  106. color: #66BB6A; 
  107.  
  108. div.gmb-api-alert .alert-actions a { 
  109. text-decoration: underline; 
  110. color: #808080; 
  111.  
  112. div.gmb-api-alert .alert-actions a:hover { 
  113. color: #555555; 
  114.  
  115. div.gmb-api-alert .alert-actions a span { 
  116. text-decoration: none; 
  117. margin-right: 5px; 
  118.  
  119. div.gmb-api-alert .dismiss { 
  120. position: absolute; 
  121. right: 15px; 
  122. height: 100%; 
  123. top: 50%; 
  124. margin-top: -10px; 
  125. outline: none; 
  126. box-shadow: none; 
  127. text-decoration: none; 
  128. color: #333; 
  129. </style> 
  130.  
  131. <div class="updated gmb-api-alert"> 
  132.  
  133. <div class="gmb-svg-banner"><?php gmb_include_view( 'admin/views/mascot-svg.php' ); ?></div> 
  134.  
  135. <div class="gmb-banner-content-wrap"> 
  136. <h3><?php 
  137. printf( 
  138. __( "Welcome to %s! Let's get started.", 'google-maps-builder' ),  
  139. '<span>' . $this->plugin_name() . '</span>' 
  140. ); 
  141. ?></h3> 
  142.  
  143. <p><?php echo sprintf( __( 'You\'re almost ready to start building awesome Google Maps! But first, in order to use %1$s you need to enter a Google Maps API key in the %2$splugin settings%5$s. An API key with Maps and Places APIs is now required by Google to access their mapping services. Don\'t worry, getting an API key is free and easy.<br> %3$sLearn How to Create a Maps API Key » %5$s | %4$s Google API Console » %5$s', 'google-maps-builder' ), $this->plugin_name(), '<a href="' . esc_url( admin_url( 'edit.php?post_type=google_maps&page=gmb_settings' ) ) . '">', '<a href="https://wordimpress.com/documentation/maps-builder-pro/creating-maps-api-key/" target="_blank">', '<a href="https://console.cloud.google.com/" target="_blank">', '</a>' ); ?></p> 
  144. </div> 
  145.  
  146. <a href="<?php 
  147. //The Dismiss Button 
  148. $nag_admin_dismiss_url = add_query_arg( array( 
  149. $this->nag_meta_key => 0 
  150. ), admin_url() ); 
  151.  
  152. echo esc_url( $nag_admin_dismiss_url ); ?>" class="dismiss"><span class="dashicons dashicons-dismiss"></span></a> 
  153.  
  154. <div class="alert-actions"> 
  155.  
  156. <a href="<?php echo esc_url( admin_url( 'edit.php?post_type=google_maps&page=gmb_settings' ) ); ?>"><span class="dashicons dashicons-admin-settings"></span><?php _e( 'Go to Settings', 'google-maps-builder' ); ?> 
  157. </a> 
  158.  
  159. <a href="https://wordimpress.com/documentation/maps-builder-pro" target="_blank" style="margin-left:30px;"><span class="dashicons dashicons-media-text"></span><?php _e( 'Plugin Documentation', 'google-maps-builder' ); ?> 
  160. </a> 
  161.  
  162. <a href="<?php echo $support_link; ?>" target="_blank" style="margin-left:30px;"> 
  163. <span class="dashicons dashicons-sos"></span><?php _e( 'Get Support', 'google-maps-builder' ); ?> 
  164. </a> 
  165.  
  166. </div> 
  167.  
  168. </div> 
  169. <?php 
  170.  
  171.  
  172. /** 
  173. * Ignore Nag. 
  174. * This is the action that allows the user to dismiss the banner it basically sets 
  175. * a tag to their user meta data. 
  176. */ 
  177. public function api_notice_ignore() { 
  178.  
  179. /** If user clicks to ignore the notice, add that to their user meta the banner then checks whether this tag exists already or not. 
  180. * See here: http://codex.wordpress.org/Function_Reference/add_user_meta 
  181. */ 
  182. if ( isset( $_GET[ $this->nag_meta_key ] ) && '0' == $_GET[ $this->nag_meta_key ] ) { 
  183.  
  184. //Get the global user 
  185. global $current_user; 
  186. $user_id = $current_user->ID; 
  187.  
  188. add_user_meta( $user_id, $this->nag_meta_key, 'true', true ); 
  189.  
  190. /** 
  191. * Fired when the plugin is activated. 
  192. * @since 1.0.0 
  193. * @param boolean $network_wide True if WPMU superadmin uses 
  194. * "Network Activate" action, false if 
  195. * WPMU is disabled or plugin is 
  196. * activated on an individual blog. 
  197. */ 
  198. public static function activate( $network_wide ) { 
  199.  
  200. //Remove Welcome Message Meta so User Sees it Again 
  201. $current_user = wp_get_current_user(); 
  202. $user_id = $current_user->ID; 
  203.  
  204. //Display Tooltip 
  205. $dismissed_pointers = explode( ', ', get_user_meta( $user_id, 'dismissed_wp_pointers', true ) ); 
  206.  
  207. // Check if our pointer is among dismissed ones and delete the meta so it displays again. 
  208. if ( in_array( 'gmb_welcome_pointer', $dismissed_pointers ) ) { 
  209. $key = array_search( 'gmb_welcome_pointer', $dismissed_pointers ); 
  210. delete_user_meta( $user_id, 'dismissed_wp_pointers', $key['gmb_welcome_pointer'] ); 
  211.  
  212.  
  213. //Multisite Checks 
  214. if ( function_exists( 'is_multisite' ) && is_multisite() ) { 
  215.  
  216. if ( $network_wide ) { 
  217.  
  218. // Get all blog ids 
  219. $blog_ids = self::get_blog_ids(); 
  220.  
  221. foreach ( $blog_ids as $blog_id ) { 
  222.  
  223. switch_to_blog( $blog_id ); 
  224. self::single_activate(); 
  225.  
  226. restore_current_blog(); 
  227.  
  228. } else { 
  229. self::single_activate(); 
  230.  
  231. } else { 
  232. self::single_activate(); 
  233.  
  234.  
  235. /** 
  236. * Fired when the plugin is deactivated. 
  237. * @since 1.0.0 
  238. * @param boolean $network_wide True if WPMU superadmin uses 
  239. * "Network Deactivate" action, false if 
  240. * WPMU is disabled or plugin is 
  241. * deactivated on an individual blog. 
  242. */ 
  243. public static function deactivate( $network_wide ) { 
  244.  
  245. if ( function_exists( 'is_multisite' ) && is_multisite() ) { 
  246.  
  247. if ( $network_wide ) { 
  248.  
  249. // Get all blog ids 
  250. $blog_ids = self::get_blog_ids(); 
  251.  
  252. foreach ( $blog_ids as $blog_id ) { 
  253.  
  254. switch_to_blog( $blog_id ); 
  255. self::single_deactivate(); 
  256.  
  257.  
  258. restore_current_blog(); 
  259.  
  260. } else { 
  261. self::single_deactivate(); 
  262.  
  263. } else { 
  264. self::single_deactivate(); 
  265.  
  266.  
  267.  
  268. /** 
  269. * ADMIN: Activation Welcome Tooltip Scripts. 
  270. * @param $hook 
  271. */ 
  272. function admin_enqueue_pointer_script_style( $hook ) { 
  273.  
  274. global $post; 
  275. global $current_screen; 
  276.  
  277. // Assume pointer shouldn't be shown 
  278. $enqueue_pointer_script_style = false; 
  279.  
  280. //For testing ONLY!: 
  281. //delete_user_meta( get_current_user_id(), 'dismissed_wp_pointers' ); 
  282.  
  283. // Get array list of dismissed pointers for current user and convert it to array 
  284. $dismissed_pointers = explode( ', ', get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) ); 
  285.  
  286. // Check if our pointer is not among dismissed ones. And we're on Google Maps settings screen. 
  287. if ( ! in_array( 'gmb_welcome_pointer', $dismissed_pointers ) && $current_screen->post_type === 'google_maps' ) { 
  288.  
  289. // $enqueue_pointer_script_style = true; 
  290.  
  291. // Add footer scripts using callback function 
  292. // add_action( 'admin_print_footer_scripts', array( $this, 'welcome_pointer_print_scripts' ) ); 
  293.  
  294. // Map Customizer Tooltip - Check if our pointer is not among dismissed ones. 
  295. if ( ! in_array( 'gmb_customizer_pointer', $dismissed_pointers ) && isset( $post->post_type ) && $post->post_type === 'google_maps' ) { 
  296.  
  297. $enqueue_pointer_script_style = true; 
  298.  
  299. // Add footer scripts using callback function. 
  300. add_action( 'admin_print_footer_scripts', array( $this, 'maps_customizer_tooltip' ) ); 
  301.  
  302. // Enqueue pointer CSS and JS files, if needed. 
  303. if ( $enqueue_pointer_script_style ) { 
  304. wp_enqueue_style( 'wp-pointer' ); 
  305. wp_enqueue_script( 'wp-pointer' ); 
  306.  
  307.  
  308. /** 
  309. * Welcome Activation Message 
  310. */ 
  311. function welcome_pointer_print_scripts() { 
  312.  
  313. $current_user = wp_get_current_user(); 
  314.  
  315. //Pointer Content 
  316. $pointer_content = '<h3>' . __( 'Welcome to', 'google-maps-builder' ) . ' ' . $this->plugin_name() . '</h3>'; 
  317. $pointer_content .= '<p>' . __( sprintf( 'Thank you for activating %s for WordPress. Sign up for the latest plugin updates, enhancements, and news.', $this->plugin_name() ), 'google-maps-builder' ) . '</p>'; 
  318.  
  319. //MailChimp Form 
  320. $pointer_content .= '<div id="mc_embed_signup" style="padding: 0 15px;"><form action="//wordimpress.us3.list-manage.com/subscribe/post?u=3ccb75d68bda4381e2f45794c&id=cf1af2563c" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" novalidate>'; 
  321. $pointer_content .= '<div class="mc-field-group" style="margin:0 0 5px">'; 
  322. $pointer_content .= '<label for="mce-EMAIL" style="float: left;width: 90px;margin: 5px 0 0;">Email Address</label>'; 
  323. $pointer_content .= '<input type="email" value="' . $current_user->user_email . '" name="EMAIL" class="required email" id="mce-EMAIL">'; 
  324. $pointer_content .= '</div>'; 
  325. $pointer_content .= '<div class="mc-field-group" style="margin: 0 0 10px;">'; 
  326. $pointer_content .= '<label for="mce-FNAME" style="float: left;width: 90px;margin: 5px 0 0;">First Name </label>'; 
  327. $pointer_content .= '<input type="text" value="' . $current_user->first_name . '" name="FNAME" class="" id="mce-FNAME">'; 
  328. $pointer_content .= '</div>'; 
  329. $pointer_content .= '<input type="radio" value="64" name="group[13857]" id="mce-group[13857]-13857-6" checked="checked" checked style="display:none;">'; 
  330. $pointer_content .= '<input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="button">'; 
  331. $pointer_content .= '</form></div>'; ?> 
  332.  
  333. <script type="text/javascript"> 
  334. //<![CDATA[ 
  335. jQuery(document).ready(function ($) { 
  336. $('#menu-posts-google_maps').pointer({ 
  337. content: '<?php echo $pointer_content; ?>',  
  338. position: { 
  339. edge: 'left', // arrow direction 
  340. align: 'center' // vertical alignment 
  341. },  
  342. pointerWidth: 350,  
  343. close: function () { 
  344. $.post(ajaxurl, { 
  345. pointer: 'gmb_welcome_pointer', // pointer ID 
  346. action: 'dismiss-wp-pointer' 
  347. }); 
  348. }).pointer('open'); 
  349. }); 
  350. //]]> 
  351. </script> 
  352.  
  353. <?php 
  354.  
  355. /** 
  356. * Get name of plugin 
  357. * @since 2.1.0 
  358. * @return string 
  359. */ 
  360. protected function plugin_name() { 
  361. return __( 'Maps Builder', 'gooogle-maps-builder' ); 
  362.  
  363. /** 
  364. * Maps Builder Customizer Tooltio 
  365. */ 
  366. function maps_customizer_tooltip() { 
  367.  
  368. $pointer_content = '<h3>' . __( 'Introducing the Map Builder', 'google-maps-builder' ) . '</h3>'; 
  369. $pointer_content .= '<p>' . sprintf( __( 'Building maps has never been easier. With the Map Builder all maps controls are within your reach and the map always stays in view. Try it out! If you like it, you can enable the view by default within the <a href="%s">plugin settings</a>. Enjoy!', 'google-maps-builder' ), admin_url( 'edit.php?post_type=google_maps&page=gmb_settings&tab=general_settings' ) ) . '</p>'; 
  370. ?> 
  371.  
  372. <script type="text/javascript"> 
  373. //<![CDATA[ 
  374. jQuery(document).ready(function ($) { 
  375. $('#map-builder').pointer({ 
  376. content: '<?php echo $pointer_content; ?>',  
  377. position: { 
  378. edge: 'right', // arrow direction 
  379. align: 'center' // vertical alignment 
  380. },  
  381. pointerWidth: 350,  
  382. close: function () { 
  383. $.post(ajaxurl, { 
  384. pointer: 'gmb_customizer_pointer', // pointer ID 
  385. action: 'dismiss-wp-pointer' 
  386. }); 
  387. }).pointer('open'); 
  388. }); 
  389. //]]> 
  390. </script> 
  391.  
  392. <?php 
  393.  
  394. /** 
  395. * Fired when a new site is activated with a WPMU environment. 
  396. * @since 1.0.0 
  397. * @param int $blog_id ID of the new blog. 
  398. */ 
  399. public function activate_new_site( $blog_id ) { 
  400.  
  401. if ( 1 !== did_action( 'wpmu_new_blog' ) ) { 
  402. return; 
  403.  
  404. switch_to_blog( $blog_id ); 
  405. self::single_activate(); 
  406. restore_current_blog(); 
  407.  
  408.  
  409. /** 
  410. * Get all blog ids of blogs in the current network that are: 
  411. * - not archived 
  412. * - not spam 
  413. * - not deleted 
  414. * @since 1.0.0 
  415. * @return array|false The blog ids, false if no matches. 
  416. */ 
  417. private static function get_blog_ids() { 
  418.  
  419. global $wpdb; 
  420.  
  421. // get an array of blog ids 
  422. $sql = "SELECT blog_id FROM $wpdb->blogs 
  423. WHERE archived = '0' AND spam = '0' 
  424. AND deleted = '0'"; 
  425.  
  426. return $wpdb->get_col( $sql ); 
  427.  
  428.  
  429. /** 
  430. * Fired for each blog when the plugin is activated. 
  431. * @since 1.0.0 
  432. */ 
  433. private static function single_activate() { 
  434. // @TODO: Define activation functionality here 
  435.  
  436. /** 
  437. * Fired for each blog when the plugin is deactivated. 
  438. * @since 1.0.0 
  439. */ 
  440. private static function single_deactivate() { 
  441. // @TODO: Define deactivation functionality here 
  442.  
  443. /** 
  444. * Flush Rewrite Rules 
  445. * @description Ensures single post type views don't 404 and more 
  446. * @since 2.0 
  447. */ 
  448. public function activation_flush_rewrites() { 
  449.  
  450. // call CPT registration function here (it should also be hooked into 'init') 
  451. $this->setup_post_type(); 
  452. flush_rewrite_rules( false ); 
  453.  
  454.  
  455. /** 
  456. * Register Post Type 
  457. * @description Registers and sets up the Maps Builder custom post type 
  458. * @since 1.0 
  459. * @return void 
  460. */ 
  461. public function setup_post_type() { 
  462.  
  463. $settings = get_option( 'gmb_settings' ); 
  464. $post_slug = isset( $settings['gmb_custom_slug'] ) ? $settings['gmb_custom_slug'] : ''; 
  465. $menu_position = isset( $settings['gmb_menu_position'] ) ? $settings['gmb_menu_position'] : ''; 
  466. $has_archive = isset( $settings['gmb_has_archive'] ) ? filter_var( $settings['gmb_has_archive'], FILTER_VALIDATE_BOOLEAN ) : ''; 
  467.  
  468. $labels = array( 
  469. 'name' => _x( 'Google Maps', 'post type general name', 'google-maps-builder' ),  
  470. 'singular_name' => _x( 'Map', 'post type singular name', 'google-maps-builder' ),  
  471. 'menu_name' => _x( 'Google Maps', 'admin menu', 'google-maps-builder' ),  
  472. 'name_admin_bar' => _x( 'Google Maps', 'add new on admin bar', 'google-maps-builder' ),  
  473. 'add_new' => _x( 'Add New', 'map', 'google-maps-builder' ),  
  474. 'add_new_item' => __( 'Add New Map', 'google-maps-builder' ),  
  475. 'new_item' => __( 'New Map', 'google-maps-builder' ),  
  476. 'edit_item' => __( 'Edit Map', 'google-maps-builder' ),  
  477. 'view_item' => __( 'View Map', 'google-maps-builder' ),  
  478. 'all_items' => __( 'All Maps', 'google-maps-builder' ),  
  479. 'search_items' => __( 'Search Maps', 'google-maps-builder' ),  
  480. 'parent_item_colon' => __( 'Parent Maps:', 'google-maps-builder' ),  
  481. 'not_found' => __( 'No Maps found.', 'google-maps-builder' ),  
  482. 'not_found_in_trash' => __( 'No Maps found in Trash.', 'google-maps-builder' ),  
  483. ); 
  484.  
  485. $args = array( 
  486. 'labels' => $labels,  
  487. 'public' => true,  
  488. 'publicly_queryable' => true,  
  489. 'show_ui' => true,  
  490. 'show_in_menu' => true,  
  491. 'query_var' => true,  
  492. 'rewrite' => array( 
  493. 'slug' => isset( $post_slug ) ? sanitize_title( $post_slug ) : 'google-maps' 
  494. ),  
  495. 'capability_type' => 'post',  
  496. 'has_archive' => isset( $has_archive ) ? $has_archive : true,  
  497. 'hierarchical' => false,  
  498. 'menu_position' => ! empty( $menu_position ) ? intval( $menu_position ) : '23.1',  
  499. 'supports' => array( 'title' ) 
  500. ); 
  501.  
  502. register_post_type( 'google_maps', $args ); 
  503.  
  504.