Loaded modules by id. */ public $modules = array(); /** @var array Modules that failed to load: id => error. */ public $failed = array(); /** @return Plugin */ public static function instance() { if ( null === self::$instance ) { self::$instance = new self(); } return self::$instance; } /** Hook everything. */ public function init() { add_action( 'init', array( Install::class, 'maybe_upgrade' ), 1 ); add_action( 'init', array( $this, 'load_textdomain' ) ); Post_Type::init(); Fields::init(); Renderer::init(); Assets::init(); Submission::init(); Uploads::init(); Notifications::init(); Rest::init(); Analytics::init(); Embed::init(); Preview::init(); Global_Styles::init(); if ( is_admin() ) { Admin::init(); } $this->load_modules(); add_action( 'init', array( $this, 'schedule' ) ); add_action( 'fwren_daily', array( $this, 'daily_cleanup' ) ); do_action( 'fwren/loaded', $this ); } /** Load translations. */ public function load_textdomain() { // Translations load automatically from translate.wordpress.org (WP 4.6+). } /** * Feature modules. Each is a class with a static init(); a module file that is missing is * skipped, so modules can ship separately. */ private function load_modules() { $modules = apply_filters( 'fwren/modules', array( 'double_optin' => '\FWREN\Modules\Double_Optin', 'admin_approval' => '\FWREN\Modules\Admin_Approval', 'drafts' => '\FWREN\Modules\Drafts', 'quiz' => '\FWREN\Modules\Quiz', 'inventory' => '\FWREN\Modules\Inventory', 'landing_page' => '\FWREN\Modules\Landing_Page', 'front_entries' => '\FWREN\Modules\Front_Entries', 'post_creation' => '\FWREN\Modules\Post_Creation', 'user_registration' => '\FWREN\Modules\User_Registration', 'importer' => '\FWREN\Modules\Importer', 'pdf_export' => '\FWREN\Modules\Pdf_Export', 'integrations' => '\FWREN\Integrations\Manager', 'payments' => '\FWREN\Payments\Manager', 'email_builder' => '\FWREN\Email\Module', 'popups' => '\FWREN\Popups\Module', 'smtp' => '\FWREN\Smtp\Module', ) ); foreach ( $modules as $id => $class ) { // A broken module (missing class, fatal in init) must never take the site down: // it is skipped, logged and reported on the plugin screens. try { if ( class_exists( $class ) && method_exists( $class, 'init' ) ) { $class::init(); $this->modules[ $id ] = $class; } } catch ( \Throwable $e ) { $this->failed[ $id ] = $e->getMessage(); if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { error_log( 'Fieldwren: module ' . $id . ' failed: ' . $e->getMessage() . ' in ' . $e->getFile() . ':' . $e->getLine() ); // phpcs:ignore } } } if ( $this->failed && is_admin() ) { add_action( 'admin_notices', function () { if ( ! current_user_can( 'fwren_manage_settings' ) ) { return; } echo '

Fieldwren: ' . esc_html__( 'These modules could not load and are switched off:', 'fieldwren' ) . ' ' . esc_html( implode( ', ', array_keys( $this->failed ) ) ) . '

'; } ); } } /** Daily housekeeping event. */ public function schedule() { if ( ! wp_next_scheduled( 'fwren_daily' ) ) { wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', 'fwren_daily' ); } } /** Remove stale temp uploads and expired drafts; apply entry retention. */ public function daily_cleanup() { Uploads::cleanup_temp(); do_action( 'fwren/daily' ); Entries::apply_retention(); } }