class-submenu.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. class NF_THREE_Submenu
  3. {
  4. /**
  5. * (required) The slug name for the parent menu (or the file name of a standard WordPress admin page)
  6. *
  7. * @var string
  8. */
  9. public $parent_slug = '';
  10. /**
  11. * (required) The text to be displayed in the title tags of the page when the menu is selected
  12. *
  13. * @var string
  14. */
  15. public $page_title = 'Ninja Forms THREE';
  16. /**
  17. * (required) The on-screen name text for the menu
  18. *
  19. * @var string
  20. */
  21. public $menu_title = 'Ninja Forms THREE';
  22. /**
  23. * (required) The capability required for this menu to be displayed to the user.
  24. *
  25. * @var string
  26. */
  27. public $capability = 'manage_options';
  28. /**
  29. * (required) The slug name to refer to this menu by (should be unique for this menu).
  30. *
  31. * @var string
  32. */
  33. public $menu_slug = 'ninja-forms-three';
  34. /**
  35. * (optional) The function that displays the page content for the menu page.
  36. *
  37. * @var string
  38. */
  39. public $function = 'display';
  40. public $priority = 9001;
  41. /**
  42. * Constructor
  43. *
  44. * Translate text and add the 'admin_menu' action.
  45. */
  46. public function __construct()
  47. {
  48. $this->menu_title = __( 'Update', 'ninja-forms' );
  49. $this->page_title = __( 'Update to Ninja Forms THREE', 'ninja-forms' );
  50. $this->capability = apply_filters( 'submenu_' . $this->menu_slug . '_capability', $this->capability );
  51. add_action( 'admin_menu', array( $this, 'register' ), $this->priority );
  52. add_action( 'wp_ajax_ninja_forms_upgrade_check', array( $this, 'upgrade_check' ) );
  53. add_filter( 'nf_general_settings_advanced', array( $this, 'settings_upgrade_button' ) );
  54. }
  55. /**
  56. * Register the menu page.
  57. */
  58. public function register()
  59. {
  60. if( ! ninja_forms_three_addons_version_check() ) return;
  61. if( ! ninja_forms_three_addons_check() ){
  62. // Hide the submenu
  63. $this->parent_slug = '';
  64. }
  65. $function = ( $this->function ) ? array( $this, $this->function ) : NULL;
  66. add_submenu_page(
  67. $this->parent_slug,
  68. $this->page_title,
  69. $this->menu_title,
  70. $this->capability,
  71. $this->menu_slug,
  72. $function
  73. );
  74. }
  75. /**
  76. * Display the menu page.
  77. */
  78. public function display()
  79. {
  80. global $ninja_forms_tabs_metaboxes;
  81. $addon_installed = false;
  82. if ( isset ( $ninja_forms_tabs_metaboxes['ninja-forms-settings']['license_settings']['license_settings']['settings'] ) ) {
  83. if ( 0 < count( $ninja_forms_tabs_metaboxes['ninja-forms-settings']['license_settings']['license_settings']['settings'] ) ) {
  84. $addon_installed = true;
  85. }
  86. }
  87. $is_opted_in = get_option( 'ninja_forms_allow_tracking', false );
  88. $is_opted_out = get_option( 'ninja_forms_do_not_allow_tracking', false );
  89. if ( ! $addon_installed && ( ! $is_opted_in || $is_opted_out ) ) {
  90. $opted_in = 0;
  91. } else {
  92. $opted_in = 1;
  93. }
  94. $all_forms = Ninja_Forms()->forms()->get_all();
  95. wp_enqueue_style( 'ninja-forms-three-upgrade-styles', plugin_dir_url(__FILE__) . 'upgrade.css' );
  96. wp_enqueue_style( 'ninja-forms-three-upgrade-jbox', plugin_dir_url(__FILE__) . 'jBox.css' );
  97. wp_enqueue_script( 'ninja-forms-three-upgrade', plugin_dir_url(__FILE__) . 'upgrade.js', array( 'jquery', 'wp-util' ), '', TRUE );
  98. wp_enqueue_script( 'ninja-forms-three-upgrade-jbox', plugin_dir_url(__FILE__) . 'jBox.min.js', array( 'jquery', 'wp-util' ), '', TRUE );
  99. wp_localize_script( 'ninja-forms-three-upgrade', 'nfThreeUpgrade', array(
  100. 'forms' => $all_forms,
  101. 'redirectURL' => admin_url( 'admin.php?page=ninja-forms&nf-switcher=upgrade' ),
  102. 'optedIn' => $opted_in,
  103. 'nonce' => wp_create_nonce( 'ninja_forms_upgrade_nonce' ),
  104. ) );
  105. include plugin_dir_path( __FILE__ ) . 'tmpl-submenu.html.php';
  106. }
  107. public function upgrade_check()
  108. {
  109. if( ! isset( $_POST[ 'formID' ] ) ) $this->respond( array( 'error' => 'Form ID not found.' ) );
  110. $form_id = absint( $_POST[ 'formID' ] );
  111. $can_upgrade = TRUE;
  112. $fields = Ninja_Forms()->form( $form_id )->fields;
  113. $settings = Ninja_Forms()->form( $form_id )->get_all_settings();
  114. foreach( $fields as $field ){
  115. if( '_calc' == $field[ 'type' ] ){
  116. // $can_upgrade = FALSE;
  117. }
  118. }
  119. $this->respond( array(
  120. 'id' => $form_id,
  121. 'title' => $settings[ 'form_title' ],
  122. 'canUpgrade' => $can_upgrade
  123. ) );
  124. }
  125. private function respond( $response = array() )
  126. {
  127. echo wp_json_encode( $response );
  128. wp_die(); // this is required to terminate immediately and return a proper response
  129. }
  130. public function settings_upgrade_button( $settings )
  131. {
  132. $settings['update_to_three'] = array(
  133. 'name' => 'update_to_three',
  134. 'type' => '',
  135. 'label' => __('Ninja Forms THREE', 'ninja-forms'),
  136. 'display_function' => array($this, 'settings_upgrade_button_display'),
  137. 'desc' => __('Upgrade to the Ninja Forms THREE.', 'ninja-forms')
  138. );
  139. return $settings;
  140. }
  141. public function settings_upgrade_button_display()
  142. {
  143. include plugin_dir_path( __FILE__ ) . 'tmpl-settings-upgrade-button.html.php';
  144. }
  145. }
  146. new NF_THREE_Submenu();