NF_VersionSwitcher

The Ninja Forms NF VersionSwitcher class.

Defined (1)

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

/lib/NF_VersionSwitcher.php  
  1. final class NF_VersionSwitcher 
  2. public function __construct() 
  3. $this->ajax_check(); 
  4.  
  5. add_action( 'init', array( $this, 'version_bypass_check' ) ); 
  6.  
  7. add_action( 'admin_init', array( $this, 'listener' ) ); 
  8.  
  9. add_filter( 'ninja_forms_admin_notices', array( $this, 'upgrade_complete_notice' ) ); 
  10.  
  11. if( defined( 'NF_DEV' ) && NF_DEV ) { 
  12. add_action('admin_bar_menu', array( $this, 'admin_bar_menu'), 999); 
  13.  
  14. public function ajax_check() 
  15. $nf2to3 = isset( $_POST[ 'nf2to3' ] ); 
  16. $doing_ajax = ( defined( 'DOING_AJAX' ) && DOING_AJAX ); 
  17. if( $nf2to3 && ! $doing_ajax ) { 
  18. wp_die( 
  19. __( 'You do not have permission.', 'ninja-forms' ),  
  20. __( 'Permission Denied', 'ninja-forms' ) 
  21. ); 
  22.  
  23. public function version_bypass_check() 
  24. if( ! isset( $_POST[ 'nf2to3' ] ) ) return TRUE; 
  25.  
  26. $capability = apply_filters( 'ninja_forms_admin_version_bypass_capabilities', 'manage_options' ); 
  27. $current_user_can = current_user_can( $capability ); 
  28.  
  29. if( $current_user_can ) return TRUE; 
  30.  
  31. wp_die( 
  32. __( 'You do not have permission.', 'ninja-forms' ),  
  33. __( 'Permission Denied', 'ninja-forms' ) 
  34. ); 
  35.  
  36. public function listener() 
  37. if( ! current_user_can( apply_filters( 'ninja_forms_admin_version_switcher_capabilities', 'manage_options' ) ) ) return; 
  38.  
  39. if( isset( $_GET[ 'nf-switcher' ] ) ) { 
  40.  
  41. $notice = ''; 
  42.  
  43. switch( $_GET[ 'nf-switcher' ] ) { 
  44. case 'upgrade': 
  45. update_option( 'ninja_forms_load_deprecated', FALSE ); 
  46. update_option( 'ninja_forms_upgrade_complete', true ); 
  47. do_action( 'ninja_forms_upgrade' ); 
  48. $notice = '&nf-upgrade=complete'; 
  49. break; 
  50. case 'rollback': 
  51. update_option( 'ninja_forms_load_deprecated', TRUE ); 
  52. update_option( 'ninja_forms_upgrade_complete', false ); 
  53. $this->rollback_activation(); 
  54. do_action( 'ninja_forms_rollback' ); 
  55. $notice = '&nf-rollback=complete'; 
  56. break; 
  57.  
  58. header( 'Location: ' . admin_url( 'admin.php?page=ninja-forms' . $notice ) ); 
  59.  
  60. public function admin_bar_menu( $wp_admin_bar ) 
  61. $args = array( 
  62. 'id' => 'nf',  
  63. 'title' => __( 'Ninja Forms Dev', 'ninja-forms' ),  
  64. 'href' => '#',  
  65. ); 
  66. $wp_admin_bar->add_node( $args ); 
  67. $args = array( 
  68. 'id' => 'nf_switcher',  
  69. 'href' => admin_url(),  
  70. 'parent' => 'nf' 
  71. ); 
  72. if( ! get_option( 'ninja_forms_load_deprecated' ) ) { 
  73. $args[ 'title' ] = __( 'DEBUG: Switch to 2.9.x', 'ninja-forms' ); 
  74. $args[ 'href' ] .= '?nf-switcher=rollback'; 
  75. } else { 
  76. $args[ 'title' ] = __( 'DEBUG: Switch to 3.0.x', 'ninja-forms' ); 
  77. $args[ 'href' ] .= '?nf-switcher=upgrade'; 
  78. $wp_admin_bar->add_node($args); 
  79.  
  80. public function rollback_activation() 
  81. global $wpdb; 
  82.  
  83. $table_name = $wpdb->prefix . 'nf_objects'; 
  84.  
  85. if( $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s", $table_name ) ) == $table_name ) return; 
  86.  
  87. if ( ! is_multisite() ) { // This is a single-site activation. 
  88.  
  89. require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); 
  90.  
  91. if( ! defined( 'NINJA_FORMS_FAV_FIELDS_TABLE_NAME' ) ) { 
  92. define( 'NINJA_FORMS_FAV_FIELDS_TABLE_NAME', $wpdb->prefix . 'ninja_forms_fav_fields' ); 
  93.  
  94. if( ! defined( 'NINJA_FORMS_FIELDS_TABLE_NAME' ) ) { 
  95. define( 'NINJA_FORMS_FIELDS_TABLE_NAME', $wpdb->prefix . 'ninja_forms_fields' ); 
  96.  
  97. if( ! defined( 'NF_OBJECT_META_TABLE_NAME' ) ) { 
  98. define( 'NF_OBJECT_META_TABLE_NAME', $wpdb->prefix .'nf_objectmeta' ); 
  99.  
  100. if( ! defined( 'NF_OBJECTS_TABLE_NAME' ) ) { 
  101. define( 'NF_OBJECTS_TABLE_NAME', $wpdb->prefix .'nf_objects' ); 
  102.  
  103. if( ! defined( 'NF_OBJECT_RELATIONSHIPS_TABLE_NAME' ) ) { 
  104. define( 'NF_OBJECT_RELATIONSHIPS_TABLE_NAME', $wpdb->prefix .'nf_relationships' ); 
  105.  
  106. if( ! defined( 'NF_PLUGIN_VERSION' ) ) { 
  107. define( 'NF_PLUGIN_VERSION', Ninja_Forms::VERSION ); 
  108.  
  109. $opt = get_option( 'ninja_forms_settings' ); 
  110.  
  111. $sql = "CREATE TABLE IF NOT EXISTS ".NINJA_FORMS_FAV_FIELDS_TABLE_NAME." ( 
  112. `id` int(11) NOT NULL AUTO_INCREMENT,  
  113. `row_type` int(11) NOT NULL,  
  114. `type` varchar(255) CHARACTER SET utf8 NOT NULL,  
  115. `order` int(11) NOT NULL,  
  116. `data` longtext CHARACTER SET utf8 NOT NULL,  
  117. `name` varchar(255) CHARACTER SET utf8 NOT NULL,  
  118. PRIMARY KEY (`id`) 
  119. ) DEFAULT CHARSET=utf8;"; 
  120.  
  121. dbDelta($sql); 
  122.  
  123. $state_dropdown = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM ".NINJA_FORMS_FAV_FIELDS_TABLE_NAME." WHERE name = %s AND row_type = 0", 'State Dropdown' ), ARRAY_A ); 
  124. if( !isset($state_dropdown['id']) ) { 
  125. $sql = 'INSERT INTO `'.NINJA_FORMS_FAV_FIELDS_TABLE_NAME.'` (`id`, `row_type`, `type`, `order`, `data`, `name`) VALUES 
  126. (2, 0, \'_list\', 0, \'a:10:{s:5:\"label\";s:14:\"State Dropdown\";s:9:\"label_pos\";s:4:\"left\";s:9:\"list_type\";s:8:\"dropdown\";s:10:\"multi_size\";s:1:\"5\";s:15:\"list_show_value\";s:1:\"1\";s:4:\"list\";a:1:{s:7:\"options\";a:51:{i:0;a:3:{s:5:\"label\";s:7:\"Alabama\";s:5:\"value\";s:2:\"AL\";s:8:\"selected\";s:1:\"0\";}i:1;a:3:{s:5:\"label\";s:6:\"Alaska\";s:5:\"value\";s:2:\"AK\";s:8:\"selected\";s:1:\"0\";}i:2;a:3:{s:5:\"label\";s:7:\"Arizona\";s:5:\"value\";s:2:\"AZ\";s:8:\"selected\";s:1:\"0\";}i:3;a:3:{s:5:\"label\";s:8:\"Arkansas\";s:5:\"value\";s:2:\"AR\";s:8:\"selected\";s:1:\"0\";}i:4;a:3:{s:5:\"label\";s:10:\"California\";s:5:\"value\";s:2:\"CA\";s:8:\"selected\";s:1:\"0\";}i:5;a:3:{s:5:\"label\";s:8:\"Colorado\";s:5:\"value\";s:2:\"CO\";s:8:\"selected\";s:1:\"0\";}i:6;a:3:{s:5:\"label\";s:11:\"Connecticut\";s:5:\"value\";s:2:\"CT\";s:8:\"selected\";s:1:\"0\";}i:7;a:3:{s:5:\"label\";s:8:\"Delaware\";s:5:\"value\";s:2:\"DE\";s:8:\"selected\";s:1:\"0\";}i:8;a:3:{s:5:\"label\";s:20:\"District of Columbia\";s:5:\"value\";s:2:\"DC\";s:8:\"selected\";s:1:\"0\";}i:9;a:3:{s:5:\"label\";s:7:\"Florida\";s:5:\"value\";s:2:\"FL\";s:8:\"selected\";s:1:\"0\";}i:10;a:3:{s:5:\"label\";s:7:\"Georgia\";s:5:\"value\";s:2:\"GA\";s:8:\"selected\";s:1:\"0\";}i:11;a:3:{s:5:\"label\";s:6:\"Hawaii\";s:5:\"value\";s:2:\"HI\";s:8:\"selected\";s:1:\"0\";}i:12;a:3:{s:5:\"label\";s:5:\"Idaho\";s:5:\"value\";s:2:\"ID\";s:8:\"selected\";s:1:\"0\";}i:13;a:3:{s:5:\"label\";s:8:\"Illinois\";s:5:\"value\";s:2:\"IL\";s:8:\"selected\";s:1:\"0\";}i:14;a:3:{s:5:\"label\";s:7:\"Indiana\";s:5:\"value\";s:2:\"IN\";s:8:\"selected\";s:1:\"0\";}i:15;a:3:{s:5:\"label\";s:4:\"Iowa\";s:5:\"value\";s:2:\"IA\";s:8:\"selected\";s:1:\"0\";}i:16;a:3:{s:5:\"label\";s:6:\"Kansas\";s:5:\"value\";s:2:\"KS\";s:8:\"selected\";s:1:\"0\";}i:17;a:3:{s:5:\"label\";s:8:\"Kentucky\";s:5:\"value\";s:2:\"KY\";s:8:\"selected\";s:1:\"0\";}i:18;a:3:{s:5:\"label\";s:9:\"Louisiana\";s:5:\"value\";s:2:\"LA\";s:8:\"selected\";s:1:\"0\";}i:19;a:3:{s:5:\"label\";s:5:\"Maine\";s:5:\"value\";s:2:\"ME\";s:8:\"selected\";s:1:\"0\";}i:20;a:3:{s:5:\"label\";s:8:\"Maryland\";s:5:\"value\";s:2:\"MD\";s:8:\"selected\";s:1:\"0\";}i:21;a:3:{s:5:\"label\";s:13:\"Massachusetts\";s:5:\"value\";s:2:\"MA\";s:8:\"selected\";s:1:\"0\";}i:22;a:3:{s:5:\"label\";s:8:\"Michigan\";s:5:\"value\";s:2:\"MI\";s:8:\"selected\";s:1:\"0\";}i:23;a:3:{s:5:\"label\";s:9:\"Minnesota\";s:5:\"value\";s:2:\"MN\";s:8:\"selected\";s:1:\"0\";}i:24;a:3:{s:5:\"label\";s:11:\"Mississippi\";s:5:\"value\";s:2:\"MS\";s:8:\"selected\";s:1:\"0\";}i:25;a:3:{s:5:\"label\";s:8:\"Missouri\";s:5:\"value\";s:2:\"MO\";s:8:\"selected\";s:1:\"0\";}i:26;a:3:{s:5:\"label\";s:7:\"Montana\";s:5:\"value\";s:2:\"MT\";s:8:\"selected\";s:1:\"0\";}i:27;a:3:{s:5:\"label\";s:8:\"Nebraska\";s:5:\"value\";s:2:\"NE\";s:8:\"selected\";s:1:\"0\";}i:28;a:3:{s:5:\"label\";s:6:\"Nevada\";s:5:\"value\";s:2:\"NV\";s:8:\"selected\";s:1:\"0\";}i:29;a:3:{s:5:\"label\";s:13:\"New Hampshire\";s:5:\"value\";s:2:\"NH\";s:8:\"selected\";s:1:\"0\";}i:30;a:3:{s:5:\"label\";s:10:\"New Jersey\";s:5:\"value\";s:2:\"NJ\";s:8:\"selected\";s:1:\"0\";}i:31;a:3:{s:5:\"label\";s:10:\"New Mexico\";s:5:\"value\";s:2:\"NM\";s:8:\"selected\";s:1:\"0\";}i:32;a:3:{s:5:\"label\";s:8:\"New York\";s:5:\"value\";s:2:\"NY\";s:8:\"selected\";s:1:\"0\";}i:33;a:3:{s:5:\"label\";s:14:\"North Carolina\";s:5:\"value\";s:2:\"NC\";s:8:\"selected\";s:1:\"0\";}i:34;a:3:{s:5:\"label\";s:12:\"North Dakota\";s:5:\"value\";s:2:\"ND\";s:8:\"selected\";s:1:\"0\";}i:35;a:3:{s:5:\"label\";s:4:\"Ohio\";s:5:\"value\";s:2:\"OH\";s:8:\"selected\";s:1:\"0\";}i:36;a:3:{s:5:\"label\";s:8:\"Oklahoma\";s:5:\"value\";s:2:\"OK\";s:8:\"selected\";s:1:\"0\";}i:37;a:3:{s:5:\"label\";s:6:\"Oregon\";s:5:\"value\";s:2:\"OR\";s:8:\"selected\";s:1:\"0\";}i:38;a:3:{s:5:\"label\";s:12:\"Pennsylvania\";s:5:\"value\";s:2:\"PA\";s:8:\"selected\";s:1:\"0\";}i:39;a:3:{s:5:\"label\";s:12:\"Rhode Island\";s:5:\"value\";s:2:\"RI\";s:8:\"selected\";s:1:\"0\";}i:40;a:3:{s:5:\"label\";s:14:\"South Carolina\";s:5:\"value\";s:2:\"SC\";s:8:\"selected\";s:1:\"0\";}i:41;a:3:{s:5:\"label\";s:12:\"South Dakota\";s:5:\"value\";s:2:\"SD\";s:8:\"selected\";s:1:\"0\";}i:42;a:3:{s:5:\"label\";s:9:\"Tennessee\";s:5:\"value\";s:2:\"TN\";s:8:\"selected\";s:1:\"0\";}i:43;a:3:{s:5:\"label\";s:5:\"Texas\";s:5:\"value\";s:2:\"TX\";s:8:\"selected\";s:1:\"0\";}i:44;a:3:{s:5:\"label\";s:4:\"Utah\";s:5:\"value\";s:2:\"UT\";s:8:\"selected\";s:1:\"0\";}i:45;a:3:{s:5:\"label\";s:7:\"Vermont\";s:5:\"value\";s:2:\"VT\";s:8:\"selected\";s:1:\"0\";}i:46;a:3:{s:5:\"label\";s:8:\"Virginia\";s:5:\"value\";s:2:\"VA\";s:8:\"selected\";s:1:\"0\";}i:47;a:3:{s:5:\"label\";s:10:\"Washington\";s:5:\"value\";s:2:\"WA\";s:8:\"selected\";s:1:\"0\";}i:48;a:3:{s:5:\"label\";s:13:\"West Virginia\";s:5:\"value\";s:2:\"WV\";s:8:\"selected\";s:1:\"0\";}i:49;a:3:{s:5:\"label\";s:9:\"Wisconsin\";s:5:\"value\";s:2:\"WI\";s:8:\"selected\";s:1:\"0\";}i:50;a:3:{s:5:\"label\";s:7:\"Wyoming\";s:5:\"value\";s:2:\"WY\";s:8:\"selected\";s:1:\"0\";}}}s:3:\"req\";s:1:\"0\";s:5:\"class\";s:0:\"\";s:9:\"show_help\";s:1:\"0\";s:9:\"help_text\";s:0:\"\";}\', \'State Dropdown\')'; 
  127. $wpdb->query($sql); 
  128.  
  129. $anti_spam = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM ".NINJA_FORMS_FAV_FIELDS_TABLE_NAME." WHERE name = %s AND row_type = 0", 'Anti-Spam' ), ARRAY_A ); 
  130. if( !isset($anti_spam['id']) ) { 
  131. $sql = 'INSERT INTO `'.NINJA_FORMS_FAV_FIELDS_TABLE_NAME.'` (`id`, `row_type`, `type`, `order`, `data`, `name`) VALUES 
  132. (3, 0, \'_spam\', 0, \'a:6:{s:9:"label_pos";s:4:"left";s:5:"label";s:18:"Anti-Spam Question";s:6:"answer";s:16:"Anti-Spam Answer";s:5:"class";s:0:"";s:9:"show_help";s:1:"0";s:9:"help_text";s:0:"";}\', \'Anti-Spam\')'; 
  133. $wpdb->query($sql); 
  134.  
  135. $submit = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM ".NINJA_FORMS_FAV_FIELDS_TABLE_NAME." WHERE name = %s AND row_type = 0", 'Submit' ), ARRAY_A ); 
  136. if( !isset($submit['id']) ) { 
  137. $sql = 'INSERT INTO `'.NINJA_FORMS_FAV_FIELDS_TABLE_NAME.'` (`id`, `row_type`, `type`, `order`, `data`, `name`) VALUES 
  138. (4, 0, \'_submit\', 0, \'a:4:{s:5:\"label\";s:6:\"Submit\";s:5:\"class\";s:0:\"\";s:9:\"show_help\";s:1:\"0\";s:9:\"help_text\";s:0:\"\";}\', \'Submit\');'; 
  139. $wpdb->query($sql); 
  140.  
  141. $sql = "CREATE TABLE IF NOT EXISTS ".NINJA_FORMS_FIELDS_TABLE_NAME." ( 
  142. `id` int(11) NOT NULL AUTO_INCREMENT,  
  143. `form_id` int(11) NOT NULL,  
  144. `type` varchar(255) CHARACTER SET utf8 NOT NULL,  
  145. `order` int(11) NOT NULL,  
  146. `data` longtext CHARACTER SET utf8 NOT NULL,  
  147. `fav_id` int(11) DEFAULT NULL,  
  148. `def_id` int(11) DEFAULT NULL,  
  149. PRIMARY KEY (`id`) 
  150. ) DEFAULT CHARSET=utf8 ;"; 
  151.  
  152. dbDelta($sql); 
  153.  
  154. /** 
  155. * Add our table structure for version 2.8. 
  156. */ 
  157.  
  158. // Create our object meta table 
  159. $sql = "CREATE TABLE IF NOT EXISTS ". NF_OBJECT_META_TABLE_NAME . " ( 
  160. `id` bigint(20) NOT NULL AUTO_INCREMENT,  
  161. `object_id` bigint(20) NOT NULL,  
  162. `meta_key` varchar(255) NOT NULL,  
  163. `meta_value` longtext NOT NULL,  
  164. PRIMARY KEY (`id`) 
  165. ) DEFAULT CHARSET=utf8;"; 
  166.  
  167. dbDelta( $sql ); 
  168.  
  169. // Create our object table 
  170. $sql = "CREATE TABLE IF NOT EXISTS " . NF_OBJECTS_TABLE_NAME . " ( 
  171. `id` bigint(20) NOT NULL AUTO_INCREMENT,  
  172. `type` varchar(255) NOT NULL,  
  173. PRIMARY KEY (`id`) 
  174. ) DEFAULT CHARSET=utf8;"; 
  175.  
  176. dbDelta( $sql ); 
  177.  
  178. // Create our object relationships table 
  179.  
  180. $sql = "CREATE TABLE IF NOT EXISTS " . NF_OBJECT_RELATIONSHIPS_TABLE_NAME . " ( 
  181. `id` bigint(20) NOT NULL AUTO_INCREMENT,  
  182. `child_id` bigint(20) NOT NULL,  
  183. `parent_id` bigint(20) NOT NULL,  
  184. `child_type` varchar(255) NOT NULL,  
  185. `parent_type` varchar(255) NOT NULL,  
  186. PRIMARY KEY (`id`) 
  187. ) DEFAULT CHARSET=utf8;"; 
  188.  
  189. dbDelta( $sql ); 
  190.  
  191. $title = apply_filters( 'ninja_forms_preview_page_title', 'ninja_forms_preview_page' ); 
  192. $preview_page = get_page_by_title( $title ); 
  193. if( !$preview_page ) { 
  194. // Create preview page object 
  195. $preview_post = array( 
  196. 'post_title' => $title,  
  197. 'post_content' => 'This is a preview of how this form will appear on your website',  
  198. 'post_status' => 'draft',  
  199. 'post_type' => 'page' 
  200. ); 
  201.  
  202. // Insert the page into the database 
  203. $page_id = wp_insert_post( $preview_post ); 
  204. }else{ 
  205. $page_id = $preview_page->ID; 
  206.  
  207. $opt['preview_id'] = $page_id; 
  208.  
  209. $current_settings = get_option( 'ninja_forms_settings', false ); 
  210.  
  211. if ( ! $current_settings ) { 
  212. update_option( 'nf_convert_notifications_complete', true ); 
  213. update_option( 'nf_convert_subs_step', 'complete' ); 
  214. update_option( 'nf_upgrade_notice', 'closed' ); 
  215. update_option( 'nf_update_email_settings_complete', true ); 
  216. update_option( 'nf_email_fav_updated', true ); 
  217. update_option( 'nf_convert_forms_complete', true ); 
  218. update_option( 'nf_database_migrations', true ); 
  219.  
  220. update_option( "ninja_forms_settings", $opt ); 
  221. update_option( 'ninja_forms_version', '2.9.56.2' ); 
  222.  
  223. } else { // We're network activating. 
  224. header( 'Location: ' . network_admin_url( 'plugins.php?deactivate=true&nf_action=network_activation_error' ) ); 
  225. exit; 
  226.  
  227.  
  228. public function upgrade_complete_notice( $notices ) 
  229. if( get_option( 'ninja_forms_upgrade_complete', false ) ) { 
  230.  
  231. // Persistance notice, until dismissed. 
  232. $notices[ 'upgrade_compelte_notice' ] = array( 
  233. 'title' => __( 'How do I look?', 'ninja-forms' ),  
  234. 'msg' => __( 'Your forms were upgraded. Take a look around and make sure everything looks right.', 'ninja-forms' ),  
  235. 'link' => '<li><span class="dashicons dashicons-welcome-learn-more"></span><a target="_blank" href="https://ninjaforms.com/documentation/?utm_medium=plugin&utm_source=admin-notice&utm_campaign=Ninja+Forms+Upsell&utm_content=Ninja+Forms+Docs">' . __( 'Learn More', 'ninja-forms' ) . '</a></li> 
  236. <li><span class="dashicons dashicons-sos"></span><a target="_blank" href="https://ninjaforms.com/docs/rollback/">' . __( 'Something is wrong...', 'ninja-forms' ) . '</a></li> 
  237. <li><span class="dashicons dashicons-thumbs-up"></span><a href="' . add_query_arg( array( 'nf_admin_notice_ignore' => __( 'upgrade_compelte_notice', 'ninja-forms' ) ) ) . '">' . __( 'Looks Good!' , 'ninja-forms' ) . '</a></li>',  
  238. 'int' => 0,  
  239. 'pages' => array( 'ninja-forms' ) 
  240. ); 
  241. return $notices; 
  242.