ninja-forms.php 24.9 KB
Newer Older
1
<?php
Kevin Stover's avatar
Kevin Stover committed
2 3
/*
Plugin Name: Ninja Forms
4
Plugin URI: http://ninjaforms.com/
Kevin Stover's avatar
Kevin Stover committed
5
Description: Ninja Forms is a webform builder with unparalleled ease of use and features.
Kevin Stover's avatar
Kevin Stover committed
6
Version: 3.1.4
Kevin Stover's avatar
Kevin Stover committed
7
Author: The WP Ninjas
8
Author URI: http://ninjaforms.com
9 10
Text Domain: ninja-forms
Domain Path: /lang/
11

Kevin Stover's avatar
Kevin Stover committed
12
Copyright 2016 WP Ninjas.
Kevin Stover's avatar
Kevin Stover committed
13
*/
Kathy's avatar
Kathy committed
14

15
require_once dirname( __FILE__ ) . '/lib/NF_VersionSwitcher.php';
16
require_once dirname( __FILE__ ) . '/lib/NF_Tracking.php';
17
require_once dirname( __FILE__ ) . '/lib/NF_Conversion.php';
18
require_once dirname( __FILE__ ) . '/lib/NF_ExceptionHandlerJS.php';
19
require_once dirname( __FILE__ ) . '/lib/Conversion/Calculations.php';
Dwarve's avatar
Dwarve committed
20

21 22 23 24 25 26
function ninja_forms_three_table_exists(){
    global $wpdb;
    $table_name = $wpdb->prefix . 'nf3_forms';
    return ( $wpdb->get_var("SHOW TABLES LIKE '$table_name'") == $table_name );
}

27
if( get_option( 'ninja_forms_load_deprecated', FALSE ) && ! ( isset( $_POST[ 'nf2to3' ] ) && ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) ){
28 29 30 31

    include 'deprecated/ninja-forms.php';

    register_activation_hook( __FILE__, 'ninja_forms_activation_deprecated' );
32

33 34
    function ninja_forms_activation_deprecated( $network_wide ){
        include_once 'deprecated/includes/activation.php';
35

36 37 38 39 40
        ninja_forms_activation( $network_wide );
    }

} else {

41
    include_once 'lib/NF_Upgrade.php';
42
    include_once 'lib/NF_AddonChecker.php';
43

44
    include_once plugin_dir_path( __FILE__ ) . 'includes/deprecated.php';
45 46 47 48 49 50 51 52 53 54

    /**
     * Class Ninja_Forms
     */
    final class Ninja_Forms
    {

        /**
         * @since 3.0
         */
Kevin Stover's avatar
Kevin Stover committed
55
        const VERSION = '3.1.4';
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

        /**
         * @var Ninja_Forms
         * @since 2.7
         */
        private static $instance;

        /**
         * Plugin Directory
         *
         * @since 3.0
         * @var string $dir
         */
        public static $dir = '';

        /**
         * Plugin URL
         *
         * @since 3.0
         * @var string $url
         */
        public static $url = '';

        /**
         * Admin Menus
         *
         * @since 3.0
         * @var array
         */
        public $menus = array();

        /**
         * AJAX Controllers
         *
         * @since 3.0
         * @var array
         */
        public $controllers = array();

        /**
         * Form Fields
         *
         * @since 3.0
         * @var array
         */
        public $fields = array();

        /**
         * Form Actions
         *
         * @since 3.0
         * @var array
         */
        public $actions = array();

Kyle B. Johnson's avatar
Kyle B. Johnson committed
111 112 113 114 115 116 117 118
        /**
         * Merge Tags
         *
         * @since 3.0
         * @var array
         */
        public $merge_tags = array();

119 120 121 122 123 124 125 126
        /**
         * Metaboxes
         *
         * @since 3.0
         * @var array
         */
        public $metaboxes = array();

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
        /**
         * Model Factory
         *
         * @var object
         */
        public $factory = '';

        /**
         * Logger
         *
         * @var string
         */
        protected $_logger = '';

        /**
         * @var NF_Session
         */
        protected $session = '';

146 147 148 149 150
        /**
         * @var NF_Tracking
         */
        public $tracking;

151 152 153 154 155 156 157 158
        /**
         * Plugin Settings
         *
         * @since 3.0
         * @var array
         */
        protected $settings = array();

159 160
        protected $requests = array();

161 162
        protected $processes = array();

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
        /**
         * Main Ninja_Forms Instance
         *
         * Insures that only one instance of Ninja_Forms exists in memory at any one
         * time. Also prevents needing to define globals all over the place.
         *
         * @since 2.7
         * @static
         * @staticvar array $instance
         * @return Ninja_Forms Highlander Instance
         */
        public static function instance()
        {
            if ( ! isset( self::$instance ) && ! ( self::$instance instanceof Ninja_Forms ) ) {
                self::$instance = new Ninja_Forms;

                self::$dir = plugin_dir_path( __FILE__ );
180 181

                // Define old constants for backwards compatibility.
182 183
                if( ! defined( 'NF_PLUGIN_DIR' ) ){
                    define( 'NF_PLUGIN_DIR', self::$dir );
184
                    define( 'NINJA_FORMS_DIR', self::$dir . 'deprecated' );
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
                }

                self::$url = plugin_dir_url( __FILE__ );
                if( ! defined( 'NF_PLUGIN_URL' ) ){
                    define( 'NF_PLUGIN_URL', self::$url );
                }

                update_option( 'ninja_forms_version', self::VERSION );

                /*
                 * Register our autoloader
                 */
                spl_autoload_register( array( self::$instance, 'autoloader' ) );

                /*
                 * Admin Menus
                 */
                self::$instance->menus[ 'forms' ]           = new NF_Admin_Menus_Forms();
203
                self::$instance->menus[ 'dashboard' ]       = new NF_Admin_Menus_Dashboard();
204 205 206
                self::$instance->menus[ 'add-new' ]         = new NF_Admin_Menus_AddNew();
                self::$instance->menus[ 'submissions']      = new NF_Admin_Menus_Submissions();
                self::$instance->menus[ 'import-export']    = new NF_Admin_Menus_ImportExport();
Kyle B. Johnson's avatar
Kyle B. Johnson committed
207
                self::$instance->menus[ 'settings' ]        = new NF_Admin_Menus_Settings();
208
                self::$instance->menus[ 'licenses']         = new NF_Admin_Menus_Licenses();
Kyle B. Johnson's avatar
Kyle B. Johnson committed
209 210
                self::$instance->menus[ 'system_status']    = new NF_Admin_Menus_SystemStatus();
                self::$instance->menus[ 'add-ons' ]         = new NF_Admin_Menus_Addons();
211
                self::$instance->menus[ 'divider']          = new NF_Admin_Menus_Divider();
212 213 214 215 216 217 218 219 220 221
                self::$instance->menus[ 'mock-data']        = new NF_Admin_Menus_MockData();

                /*
                 * AJAX Controllers
                 */
                self::$instance->controllers[ 'form' ]        = new NF_AJAX_Controllers_Form();
                self::$instance->controllers[ 'preview' ]     = new NF_AJAX_Controllers_Preview();
                self::$instance->controllers[ 'submission' ]  = new NF_AJAX_Controllers_Submission();
                self::$instance->controllers[ 'savedfields' ] = new NF_AJAX_Controllers_SavedFields();

222 223 224 225 226 227
                /*
                 * REST Controllers
                 */
                self::$instance->controllers[ 'REST' ][ 'forms' ] = new NF_AJAX_REST_Forms();
                self::$instance->controllers[ 'REST' ][ 'new-form-templates' ] = new NF_AJAX_REST_NewFormTemplates();

228 229 230 231 232 233
                /*
                 * Async Requests
                 */
                require_once Ninja_Forms::$dir . 'includes/Libraries/BackgroundProcessing/classes/wp-async-request.php';
                self::$instance->requests[ 'delete-field' ] = new NF_AJAX_Requests_DeleteField();

234 235 236 237 238 239
                /*
                 * Background Processes
                 */
                require_once Ninja_Forms::$dir . 'includes/Libraries/BackgroundProcessing/wp-background-processing.php';
                self::$instance->requests[ 'update-fields' ] = new NF_AJAX_Processes_UpdateFields();

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
                /*
                 * WP-CLI Commands
                 */
                if( class_exists( 'WP_CLI_Command' ) ) {
                    WP_CLI::add_command('ninja-forms', 'NF_WPCLI_NinjaFormsCommand');
                }

                /*
                 * Preview Page
                 */
                self::$instance->preview = new NF_Display_Preview();

                /*
                 * Shortcodes
                 */
                self::$instance->shortcodes = new NF_Display_Shortcodes();

                /*
                 * Submission CPT
                 */
                new NF_Admin_CPT_Submission();
261 262
                new NF_Admin_CPT_DownloadAllSubmissions();
                require_once Ninja_Forms::$dir . 'lib/StepProcessing/menu.php';
KR Moorhouse's avatar
KR Moorhouse committed
263 264 265 266 267
                
                /*
                 * Submission Metabox
                 */
                new NF_Admin_Metaboxes_Calculations();
268 269 270 271 272 273 274 275 276

                /*
                 * Logger
                 */
                self::$instance->_logger = new NF_Database_Logger();

                /*
                 * Merge Tags
                 */
Kevin Stover's avatar
Kevin Stover committed
277
                self::$instance->merge_tags[ 'wp' ] = new NF_MergeTags_WP();
278 279
                self::$instance->merge_tags[ 'fields' ] = new NF_MergeTags_Fields();
                self::$instance->merge_tags[ 'calcs' ] = new NF_MergeTags_Calcs();
Kyle B. Johnson's avatar
Kyle B. Johnson committed
280
                self::$instance->merge_tags[ 'form' ] = new NF_MergeTags_Form();
281
                self::$instance->merge_tags[ 'other' ] = new NF_MergeTags_Other();
Kevin Stover's avatar
Kevin Stover committed
282
                self::$instance->merge_tags[ 'deprecated' ] = new NF_MergeTags_Deprecated();
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298

                /*
                 * Add Form Modal
                 */
                self::$instance->add_form_modal = new NF_Admin_AddFormModal();

                /*
                 * EOS Parser
                 */
                self::$instance->_eos[ 'parser' ] = require_once 'includes/Libraries/EOS/Parser.php';

                self::$instance->session = new NF_Session();

                /*
                 * Plugin Settings
                 */
299
                self::$instance->settings = apply_filters( 'ninja_forms_settings', get_option( 'ninja_forms_settings' ) );
300 301 302 303 304 305

                /*
                 * Admin Notices System
                 */
                self::$instance->notices = new NF_Admin_Notices();

Kyle B. Johnson's avatar
Kyle B. Johnson committed
306 307
                self::$instance->widgets[] = new NF_Widget();

308 309 310 311 312
                /*
                 * Opt-In Tracking
                 */
                self::$instance->tracking = new NF_Tracking();

313 314
                /*
                 * JS Exception Handler
315 316
                 *
                 * TODO: Review PR#2492 for improvements.
317
                 */
318
                // self::$instance->exception_handler_js = new NF_ExceptionHandlerJS();
319

320 321 322 323 324 325
                /*
                 * Activation Hook
                 * TODO: Move to a permanent home.
                 */
                register_activation_hook( __FILE__, array( self::$instance, 'activation' ) );

326
                self::$instance->metaboxes[ 'append-form' ] = new NF_Admin_Metaboxes_AppendAForm();
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341

                /*
                 * Require EDD auto-update file
                 */
                if( ! class_exists( 'EDD_SL_Plugin_Updater' ) ) {
                    // Load our custom updater if it doesn't already exist
                    require_once( self::$dir . 'includes/Integrations/EDD/EDD_SL_Plugin_Updater.php');
                }
                require_once self::$dir . 'includes/Integrations/EDD/class-extension-updater.php';
            }

            add_action( 'admin_notices', array( self::$instance, 'admin_notices' ) );

            add_action( 'plugins_loaded', array( self::$instance, 'plugins_loaded' ) );

342 343
            add_action( 'ninja_forms_available_actions', array( self::$instance, 'scrub_available_actions' ) );

344 345 346
            add_action( 'init', array( self::$instance, 'init' ), 5 );
            add_action( 'admin_init', array( self::$instance, 'admin_init' ), 5 );

347 348 349
            return self::$instance;
        }

350 351 352 353 354 355 356 357 358 359
        public function init()
        {
            do_action( 'nf_init', self::$instance );
        }

        public function admin_init()
        {
            do_action( 'nf_admin_init', self::$instance );
        }

360 361 362 363 364 365 366 367 368
        public function scrub_available_actions( $actions )
        {
            foreach( $actions as $key => $action ){
                if ( ! is_plugin_active( $action[ 'plugin_path' ] ) )  continue;
                unset( $actions[ $key ] );
            }
            return $actions;
        }

369 370 371 372 373 374 375 376 377
        public function admin_notices()
        {
            // Notices filter and run the notices function.
            $admin_notices = Ninja_Forms()->config( 'AdminNotices' );
            self::$instance->notices->admin_notice( apply_filters( 'nf_admin_notices', $admin_notices ) );
        }

        public function plugins_loaded()
        {
378 379
            load_plugin_textdomain( 'ninja-forms', false, basename( dirname( __FILE__ ) ) . '/lang' );

380 381 382 383 384
            /*
             * Field Class Registration
             */
            self::$instance->fields = apply_filters( 'ninja_forms_register_fields', self::load_classes( 'Fields' ) );

385 386 387 388 389 390 391 392 393
            if( ! apply_filters( 'ninja_forms_enable_credit_card_fields', false ) ){
                unset( self::$instance->fields[ 'creditcard' ] );
                unset( self::$instance->fields[ 'creditcardcvc' ] );
                unset( self::$instance->fields[ 'creditcardexpiration' ] );
                unset( self::$instance->fields[ 'creditcardfullname' ] );
                unset( self::$instance->fields[ 'creditcardnumber' ] );
                unset( self::$instance->fields[ 'creditcardzip' ] );
            }

394 395 396 397 398
            /*
             * Form Action Registration
             */
            self::$instance->actions = apply_filters( 'ninja_forms_register_actions', self::load_classes( 'Actions' ) );

Kyle B. Johnson's avatar
Kyle B. Johnson committed
399 400 401 402 403
            /*
             * Merge Tag Registration
             */
            self::$instance->merge_tags = apply_filters( 'ninja_forms_register_merge_tags', self::$instance->merge_tags );

404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
            /*
             * It's Ninja Time: Hook for Extensions
             */
            do_action( 'ninja_forms_loaded' );

        }

        /**
         * Autoloader
         *
         * Autoload Ninja Forms classes
         *
         * @param $class_name
         */
        public function autoloader( $class_name )
        {
            if( class_exists( $class_name ) ) return;

            /* Ninja Forms Prefix */
            if (false !== strpos($class_name, 'NF_')) {
                $class_name = str_replace('NF_', '', $class_name);
                $classes_dir = realpath(plugin_dir_path(__FILE__)) . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR;
                $class_file = str_replace('_', DIRECTORY_SEPARATOR, $class_name) . '.php';
                if (file_exists($classes_dir . $class_file)) {
                    require_once $classes_dir . $class_file;
                }
            }

            /* WP Ninjas Prefix */
            if (false !== strpos($class_name, 'WPN_')) {
                $class_name = str_replace('WPN_', '', $class_name);
                $classes_dir = realpath(plugin_dir_path(__FILE__)) . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR;
                $class_file = str_replace('_', DIRECTORY_SEPARATOR, $class_name) . '.php';
                if (file_exists($classes_dir . $class_file)) {
                    require_once $classes_dir . $class_file;
                }
            }
        }

        /*
         * PUBLIC API WRAPPERS
         */

        /**
         * Form Model Factory Wrapper
         *
         * @param $id
         * @return NF_Abstracts_ModelFactory
         */
        public function form( $id = '' )
        {
            global $wpdb;
456

Kevin Stover's avatar
Kevin Stover committed
457 458 459 460
            static $forms;
            if ( isset ( $forms[ $id ] ) ) {
                return $forms[ $id ];
            }
461

Kevin Stover's avatar
Kevin Stover committed
462 463
            $forms[ $id ] = new NF_Abstracts_ModelFactory( $wpdb, $id );
            return $forms[ $id ];
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
        }

        /**
         * Logger Class Wrapper
         *
         * Example Use:
         * Ninja_Forms()->logger()->log( 'debug', "Hello, {name}!", array( 'name' => 'world' ) );
         * Ninja_Forms()->logger()->debug( "Hello, {name}!", array( 'name' => 'world' ) );
         *
         * @return string
         */
        public function logger()
        {
            return $this->_logger;
        }

        public function eos()
        {
            return new Parser();
        }

        public function session()
        {
            return $this->session;
        }

490 491 492 493 494 495 496
        public function request( $action )
        {
            if( ! isset( $this->requests[ $action ] ) ) return new NF_AJAX_Requests_NullRequest();

            return $this->requests[ $action ];
        }

497 498 499 500 501 502 503
        public function background_process( $action )
        {
            if( ! isset( $this->requests[ $action ] ) ) return new NF_AJAX_Processes_NullProcess();

            return $this->requests[ $action ];
        }

504 505 506 507 508 509 510 511
	    /**
	     * Get a setting
	     *
	     * @param string     $key
	     * @param bool|false $default
	     * @return bool
	     */
        public function get_setting( $key = '', $default = false )
512
        {
513
            if( empty( $key ) || ! isset( $this->settings[ $key ] ) ) return $default;
514 515 516 517

            return $this->settings[ $key ];
        }

518 519 520 521 522
	    /**
	     * Get all the settings
	     *
	     * @return array
	     */
523 524 525 526 527
        public function get_settings()
        {
            return ( is_array( $this->settings ) ) ? $this->settings : array();
        }

528 529 530 531 532 533 534 535
	    /**
	     * Update a setting
	     *
	     * @param string           $key
	     * @param mixed           $value
	     * @param bool|false $defer_update Defer the database update of all settings
	     */
        public function update_setting( $key, $value, $defer_update = false )
536
        {
537 538 539 540
	        $this->settings[ $key ] = $value;
	        if ( ! $defer_update ) {
		        $this->update_settings();
	        }
541 542
        }

543 544 545 546 547
	    /**
	     * Save settings to database
	     *
	     * @param array $settings
	     */
548 549
        public function update_settings( $settings = array() )
        {
550
            if( ! is_array( $this->settings ) ) $this->settings = array();
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627

            if( $settings && is_array( $settings ) ) {
                $this->settings = array_merge($this->settings, $settings);
            }

            update_option( 'ninja_forms_settings', $this->settings );
        }


        /**
         * Display Wrapper
         *
         * @param $form_id
         */
        public function display( $form_id, $preview = FALSE )
        {
            if( ! $form_id ) return;

            $noscript_message = __( 'Notice: JavaScript is required for this content.', 'ninja-forms' );
            $noscript_message = apply_filters( 'ninja_forms_noscript_message', $noscript_message );

            Ninja_Forms()->template( 'display-noscript-message.html.php', array( 'message' => $noscript_message ) );

            if( ! $preview ) {
                NF_Display_Render::localize($form_id);
            } else {
                NF_Display_Render::localize_preview($form_id);
            }
        }



        /*
         * PRIVATE METHODS
         */

        /**
         * Load Classes from Directory
         *
         * @param string $prefix
         * @return array
         */
        private static function load_classes( $prefix = '' )
        {
            $return = array();

            $subdirectory = str_replace( '_', DIRECTORY_SEPARATOR, str_replace( 'NF_', '', $prefix ) );

            $directory = 'includes/' . $subdirectory;

            foreach (scandir( self::$dir . $directory ) as $path) {

                $path = explode( DIRECTORY_SEPARATOR, str_replace( self::$dir, '', $path ) );
                $filename = str_replace( '.php', '', end( $path ) );

                $class_name = 'NF_' . $prefix . '_' . $filename;

                if( ! class_exists( $class_name ) ) continue;

                $return[ strtolower( $filename ) ] = new $class_name;
            }

            return $return;
        }



        /*
         * STATIC METHODS
         */

        /**
         * Template
         *
         * @param string $file_name
         * @param array $data
         */
628
        public static function template( $file_name = '', array $data = array(), $return = FALSE )
629
        {
630
            if( ! $file_name ) return FALSE;
631 632 633

            extract( $data );

634 635 636 637 638 639 640
            $path = self::$dir . 'includes/Templates/' . $file_name;

            if( ! file_exists( $path ) ) return FALSE;

            if( $return ) return file_get_contents( $path );

            include $path;
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
        }

        /**
         * Config
         *
         * @param $file_name
         * @return mixed
         */
        public static function config( $file_name )
        {
            return include self::$dir . 'includes/Config/' . $file_name . '.php';
        }

        /**
         * Activation
         */
        public function activation() {
            $migrations = new NF_Database_Migrations();
            $migrations->migrate();
660

661 662
            if( Ninja_Forms()->form()->get_forms() ) return;

663 664
            $form = Ninja_Forms::template( 'formtemplate-contactform.nff', array(), TRUE );
            Ninja_Forms()->form()->import_form( $form );
665 666
        }

667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
        /**
         * Deprecated Notice
         *
         * Example: Ninja_Forms::deprecated_hook( 'ninja_forms_old', '3.0', 'ninja_forms_new', debug_backtrace() );
         *
         * @param $deprecated
         * @param $version
         * @param null $replacement
         * @param null $backtrace
         */
        public static function deprecated_notice( $deprecated, $version, $replacement = null, $backtrace = null )
        {
            do_action( 'ninja_forms_deprecated_call', $deprecated, $replacement, $version );

            $show_errors = current_user_can( 'manage_options' );

            // Allow plugin to filter the output error trigger
            if ( WP_DEBUG && apply_filters( 'ninja_forms_deprecated_function_trigger_error', $show_errors ) ) {
                if ( ! is_null( $replacement ) ) {
                    trigger_error( sprintf( __( '%1$s is <strong>deprecated</strong> since Ninja Forms version %2$s! Use %3$s instead.', 'ninja-forms' ), $deprecated, $version, $replacement ) );
                    // trigger_error(  print_r( $backtrace, 1 ) ); // Limited to previous 1028 characters, but since we only need to move back 1 in stack that should be fine.
                    // Alternatively we could dump this to a file.
                } else {
                    trigger_error( sprintf( __( '%1$s is <strong>deprecated</strong> since Ninja Forms version %2$s.', 'ninja-forms' ), $deprecated, $version ) );
                    // trigger_error( print_r( $backtrace, 1 ) );// Limited to previous 1028 characters, but since we only need to move back 1 in stack that should be fine.
                    // Alternatively we could dump this to a file.
                }
            }
        }

697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
    } // End Class Ninja_Forms



    /**
     * The main function responsible for returning The Highlander Ninja_Forms
     * Instance to functions everywhere.
     *
     * Use this function like you would a global variable, except without needing
     * to declare the global.
     *
     * Example: <?php $nf = Ninja_Forms(); ?>
     *
     * @since 2.7
     * @return Ninja_Forms Highlander Instance
     */
    function Ninja_Forms()
    {
        return Ninja_Forms::instance();
    }

    Ninja_Forms();

720 721 722 723 724 725
    /*
    |--------------------------------------------------------------------------
    | Uninstall Hook
    |--------------------------------------------------------------------------
    */

Kyle B. Johnson's avatar
Kyle B. Johnson committed
726
    register_uninstall_hook( __FILE__, 'ninja_forms_uninstall' );
727 728

    function ninja_forms_uninstall(){
729 730 731 732 733 734 735

        if( Ninja_Forms()->get_setting( 'delete_on_uninstall ') ) {
            require_once plugin_dir_path(__FILE__) . '/includes/Database/Migrations.php';
            $migrations = new NF_Database_Migrations();
            $migrations->nuke(TRUE, TRUE);
        }
    }
736

737 738 739 740 741 742 743 744
    // Scheduled Action Hook
    function nf_optin_send_admin_email( ) {
        /*
         * If we aren't opted in, or we've specifically opted out, then return false.
         */
        if ( ! Ninja_Forms()->tracking->is_opted_in() || Ninja_Forms()->tracking->is_opted_out() ) {
            return false;
        }
745

746 747 748
        /*
         * If we haven't already submitted our email to api.ninjaforms.com, submit it and set an option saying we have.
         */
Kyle Johnson's avatar
Kyle Johnson committed
749

750 751 752
        if ( get_option ( 'ninja_forms_optin_admin_email', false ) ) {
            return false;
        }
753

754 755 756
        /*
         * Ping api.ninjaforms.com
         */
Kyle Johnson's avatar
Kyle Johnson committed
757

758 759 760 761 762 763 764 765 766 767 768 769 770
        $admin_email = get_option('admin_email');
        $url = home_url();
        $response = wp_remote_post(
            'http://api.ninjaforms.com',
            array(
                'body' => array( 'admin_email' => $admin_email, 'url' => $url ),
            )
        );

        if( is_array($response) ) {
            $header = $response['headers']; // array of http header lines
            $body = $response['body']; // use the content
        }
771

772 773
        update_option( 'ninja_forms_optin_admin_email', true );
    }
774

775
    add_action( 'nf_optin_cron', 'nf_optin_send_admin_email' );
776

777 778 779 780 781 782 783 784 785
    // Custom Cron Recurrences
    function nf_custom_cron_job_recurrence( $schedules ) {
        $schedules['nf-monthly'] = array(
            'display' => __( 'Once per month', 'textdomain' ),
            'interval' => 2678400,
        );
        return $schedules;
    }
    add_filter( 'cron_schedules', 'nf_custom_cron_job_recurrence' );
786

787 788 789 790 791 792
    // Schedule Cron Job Event
    function nf_optin_send_admin_email_cron_job() {
        if ( ! wp_next_scheduled( 'nf_optin_cron' ) ) {
            nf_optin_send_admin_email();
            wp_schedule_event( current_time( 'timestamp' ), 'nf-monthly', 'nf_optin_cron' );
        }
793
    }
794 795

    add_action( 'wp', 'nf_optin_send_admin_email_cron_job' );
Kyle Johnson's avatar
Kyle Johnson committed
796
}