# ivyforms/0.2.1/backend/src/load.php

The Innovative Form Builder – IvyForms, version 0.2.1. 266 lines.

- Page: https://pluginprobe.com/plugins/ivyforms/0.2.1/code/backend/src/load.php
- Raw: https://pluginprobe.com/plugins/ivyforms/0.2.1/raw/backend/src/load.php
- Modified: 2025-08-08T07:58:08+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/ivyforms/0.2.1/code/backend/src/load.php#L10-L20`.

```php
<?php

/**
 * Plugin Loader.
 *
 * @package IvyForms
 * @since 0.0.1
 */

namespace IvyForms\Load;

// phpcs:disable PSR1.Files.SideEffects
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}

use DI\Container;
use DI\DependencyException;
use DI\NotFoundException;
use Exception;
use IvyForms\Common\Exceptions\InvalidArgumentException;
use IvyForms\Routes\Routes;
use IvyForms\Services\InstallActions\ActivationDatabaseHook;
use IvyForms\Services\InstallActions\ActivationMultisite;
use IvyForms\Services\InstallActions\ActivationNewSiteMultisite;
use IvyForms\Services\InstallActions\ActivationSettingsHook;
use IvyForms\Services\InstallActions\DeleteDatabaseHook;
use IvyForms\Services\InstallActions\DeletionMultisite;
use IvyForms\Services\Menu\MenuManager;
use IvyForms\Services\Preview\PreviewService;
use IvyForms\Services\Settings\SettingsService;
use IvyForms\Services\Shortcode\ShortcodeService;

/**
 * Plugin
 */
class Plugin
{
    /**
     * Instance
     */
    private static ?object $instance = null;

    /**
     * Container
     */
    public ?Container $container = null;

    /**
     * Load plugin
     *
     * @return object initialized object of class.
     * @throws Exception
     */
    public static function getInstance(Container $container): ?object
    {
        if (null === self::$instance) {
            self::$instance = new self($container);
        }
        return self::$instance;
    }

    /**
     * Constructor
     *
     * @throws Exception
     * @since 0.0.1
     */
    public function __construct(Container $container)
    {
        $this->container = $container;

        /**
         * The code that runs during plugin activation
         */
        register_activation_hook(
            IVYFORMS_FILE,
            [ self::class, 'activation' ]
        );
        register_activation_hook(
            IVYFORMS_FILE,
            [ self::class, 'activationSettings' ]
        );

        /**
         * The code that runs during plugin deactivation
         */
        register_uninstall_hook(
            IVYFORMS_FILE,
            [ self::class, 'deletion']
        );

        /** Init load */
        add_action('plugins_loaded', [ $this, 'init' ]);

        /** Register API calls */
        add_action('rest_api_init', [$this, 'registerRoutes']);

        /** Isolate API calls */
        add_action('wp_ajax_ivyforms_api', [ $this, 'registerRoutes' ]);
        add_action('wp_ajax_nopriv_ivyforms_api', [ $this, 'registerRoutes' ]);

        /** Activation hook for new site on multisite setup */
        add_action('wpmu_new_blog', [ $this, 'initNewSite' ]);

        add_filter('wp_script_attributes', ['IvyForms\Services\Shortcode\ShortcodeService', 'addScriptAttribute']);

        add_action('admin_init', [$this, 'adminInit']);
    }

    /**
     * Handle admin initialization tasks
     *
     * @return void
     */
    public function adminInit(): void
    {
        $this->checkVersionAndHandleActivation();

        $this->removeAdminNotices();
    }

    /**
     * Check a plugin version and handle activation/deactivation if needed
     *
     * @return void
     */
    private function checkVersionAndHandleActivation(): void
    {
        if (!function_exists('activate_plugin')) {
            // @phpstan-ignore requireOnce.fileNotFound
            require_once ABSPATH . 'wp-admin/includes/plugin.php';
        }
        $settingsService = new SettingsService();

        $currentVersion = IVYFORMS_VERSION;
        $savedVersion = $settingsService->getSetting('general', 'version');

        if ($savedVersion !== $currentVersion) {
            deactivate_plugins(IVYFORMS_PLUGIN_SLUG); // @phpstan-ignore argument.type
            activate_plugin(IVYFORMS_PLUGIN_SLUG); // @phpstan-ignore argument.type

            $settingsService->setSetting('general', 'version', $currentVersion);
        }
    }

    /**
     * @throws DependencyException
     * @throws NotFoundException
     */
    public function registerRoutes(): void
    {
        // Register REST API routes after the container is built
        Routes::registerRoutes($this->container);
    }

    /**
     * Remove admin notices on IvyForms admin pages
     */
    public function removeAdminNotices(): void
    {
        // Check if we're on an IvyForms admin page
        if ($this->isIvyFormsAdminPage()) {
            // Remove all admin notices
            remove_all_actions('admin_notices');
            remove_all_actions('all_admin_notices');
            // For plugins using user_admin_notices or network_admin_notices
            remove_all_actions('user_admin_notices');
            remove_all_actions('network_admin_notices');
        }
    }

    /**
     * Check if current page is an IvyForms admin page
     *
     * @SuppressWarnings(PHPMD)
     *
     * @return bool
     */
    private function isIvyFormsAdminPage(): bool
    {
        if (!is_admin()) {
            return false;
        }
        // Adjust this based on your page's actual slug/identifier
        return (isset($_GET['page']) && strpos($_GET['page'], 'ivyforms') !== false);
    }

    /**
     * @param bool $networkWide
     */
    public static function activation(bool $networkWide): void
    {
        //Network activation
        if ($networkWide && function_exists('is_multisite') && is_multisite()) {
            ActivationMultisite::init();
        }

        ActivationDatabaseHook::init();
    }

    /**
     * @param int $siteId
     */
    public static function initNewSite(int $siteId): void
    {
        ActivationNewSiteMultisite::init($siteId);
    }

    /**
     * @throws Exception
     */
    public static function activationSettings(): void
    {
        ActivationSettingsHook::init();
    }

    /**
     * Uninstall function when plugin is deleted
     * @throws InvalidArgumentException
     */
    public static function deletion(): void
    {
        $settingsService = new SettingsService();

        if ($settingsService->getSetting('activation', 'deleteTables')) {
            //Network deletion
            if (
                function_exists('is_multisite') &&
                is_multisite()
            ) {
                DeletionMultisite::delete();
            }

            DeleteDatabaseHook::delete();
        }
    }

    /**
     * Init
     * @return void
     */
    public function init(): void
    {

        new PreviewService();

        try {
            /** @var MenuManager $menuManager*/
            $menuManager = $this->container->get(MenuManager::class);
            // Register the admin menus and submenus using the 'admin_menu' hook
            $menuManager->registerMenus();
        } catch (DependencyException | NotFoundException $e) {
            if (defined('WP_DEBUG') && WP_DEBUG) {
                error_log('Menu error: ' . $e->getMessage());
            }
        }

        if (!is_admin()) {
            add_shortcode('ivyforms', function ($atts = []) {
                return ShortcodeService::shortcodeHandler($atts);
            });
        }
    }
}

```
