<?php
namespace SabaiApps\Directories\Component\FrontendSubmit;

use SabaiApps\Directories\Component\AbstractComponent;
use SabaiApps\Directories\Component\System;
use SabaiApps\Directories\Component\Field;
use SabaiApps\Directories\Component\Display;
use SabaiApps\Directories\Component\Entity;
use SabaiApps\Directories\Component\CSV;
use SabaiApps\Framework\User\AbstractIdentity;
use SabaiApps\Directories\Context;
use SabaiApps\Directories\Application;
use SabaiApps\Directories\Request;
use SabaiApps\Directories\Exception;
use SabaiApps\Directories\Response;

class FrontendSubmitComponent extends AbstractComponent implements
	System\IMainRouter,
	System\IAdminRouter,
	Field\ITypes,
	Field\IWidgets,
	System\ISlugs,
	System\IScheduledActions,
	Display\IButtons,
	CSV\IExporters,
	CSV\IImporters,
	IRestrictors {

	const VERSION = '2.0.6', PACKAGE = 'directories-pro';

	const REGISTER_FORM_FIELD_WEIGHT_USER_NAME    = 1, REGISTER_FORM_FIELD_WEIGHT_EMAIL = 5, REGISTER_FORM_FIELD_WEIGHT_PASSWORD = 90,
		REGISTER_FORM_FIELD_WEIGHT_PRIVACY_POLICY = 96, REGISTER_FORM_FIELD_WEIGHT_SUBMIT = 99;

	public static function description() {
		return 'Enables registered and non-registered users to submit content from the frontend.';
	}

	public static function interfaces() {
		return array(
			'Notification\IEvents',
		);
	}

	public function systemMainRoutes( $lang = null ) {
		$routes = array();
		if ( $this->hasSlug( 'login', $lang ) ) {
			$login_slug                                      = $this->getSlug( 'login', $lang );
			$routes[ '/' . $login_slug ]                     = array(
				'controller'      => 'LoginOrRegister',
				'access_callback' => true,
				'callback_path'   => 'login',
				'title_callback'  => true,
			);
			$routes[ '/' . $login_slug . '/register' ]       = array(
				'controller'      => 'Register',
				'callback_path'   => 'register',
				'title_callback'  => true,
				'access_callback' => true,
			);
			$routes[ '/' . $login_slug . '/lost_password' ]  = array(
				'controller'     => 'LostPassword',
				'callback_path'  => 'lost_password',
				'title_callback' => true,
			);
			$routes[ '/' . $login_slug . '/reset_password' ] = array(
				'controller'      => 'ResetPassword',
				'access_callback' => true,
				'callback_path'   => 'reset_password',
				'title_callback'  => true,
			);
			$routes[ '/' . $login_slug . '/verify_account' ] = array(
				'controller'      => 'VerifyAccount',
				'access_callback' => true,
				'callback_path'   => 'verify_account',
			);
			$routes[ '/' . $login_slug . '/resend_verify_account_key' ] = array(
				'controller'      => 'ResendVerifyAccountKey',
				'access_callback' => true,
				'callback_path'   => 'resend_verify_account_key',
			);
		}
		foreach ( $this->_application->Entity_Bundles() as $bundle ) {
			if ( $bundle->entitytype_name !== 'post'
				|| $this->_application->Entity_BundleTypeInfo( $bundle->type, 'frontendsubmit_disable' )
			) {
				continue;
			}

			if ( empty( $bundle->info['parent'] )
				&& empty( $bundle->info['parent_field'] )
			) {
				$routes[ '/' . $this->_application->FrontendSubmit_AddEntitySlug( $bundle, $lang ) ] = array(
					'controller'      => 'AddEntity',
					'access_callback' => true,
					'title_callback'  => true,
					'callback_path'   => 'add_entity',
					'data'            => array(
						'bundle_type' => $bundle->type,
					),
				);
			} else {
				$routes[ '/' . $this->_application->FrontendSubmit_AddEntitySlug( $bundle, $lang ) ] = array(
					'controller'      => 'AddChildEntity',
					'access_callback' => true,
					'title_callback'  => true,
					'callback_path'   => 'add_child_entity',
					'data'            => array(
						'bundle_type' => $bundle->type,
						'group'       => $bundle->group,
					),
				);
			}
		}

		return $routes;
	}

	protected function _getBundle( array $route ) {
		return $this->_application->Entity_Bundle( $route['data']['bundle_type'], $route['data']['group'] );
	}

	public function systemOnAccessMainRoute( Context $context, $path, $accessType, array &$route ) {
		switch ( $path ) {
			case 'add_child_entity':
				if ( ! isset( $context->child_bundle ) ) {
					if ( ( ! $bundle = $this->_getBundle( $route ) )
						|| empty( $bundle->info['parent'] )
					) {
						return false;
					}

					$context->child_bundle = $bundle;
				}
				if ( $accessType === Application::ROUTE_ACCESS_LINK ) {
					if ( ( ! $entity_id = $context->getRequest()->asInt( 'entity_id' ) )
						|| ( ! $entity = $this->_application->Entity_Entity( $context->child_bundle->entitytype_name, $entity_id ) )
						|| $entity->getBundleName() !== $context->child_bundle->info['parent']
					) {
						return false;
					}

					$context->entity = $entity;
					return true;
				}
				$GLOBALS['drts_entity'] = $context->entity; // global var required by entity parent field widget
				return true;
			case 'add_entity':
				if ( $accessType === Application::ROUTE_ACCESS_LINK ) {
					$context->bundle_type = $route['data']['bundle_type'];
					return true;
				}
				if ( $directory = $context->getRequest()->asStr( 'directory' ) ) {
					if ( $bundle = $this->_application->Entity_Bundle( $context->bundle_type, $directory ) ) {
						$context->setRedirect( $this->_application->Url( '/' . $this->_application->FrontendSubmit_AddEntitySlug( $bundle->type ), array( 'bundle' => $bundle->name ) ) );
						return false;
					}
				}
				return true;
			case 'login':
				if ( ! $this->_application->getUser()->isAnonymous() ) {
					// Redirect to frontend dashboard if enabled
					if ( $dashboard_slug = $this->_application->getComponent( 'Dashboard' )->getSlug( 'dashboard' ) ) {
						$context->setRedirect( '/' . $dashboard_slug );
					}
					return false;
				}
				return true;
			case 'verify_account':
			case 'resend_verify_account_key':
				if ( $accessType === Application::ROUTE_ACCESS_LINK ) {
					if ( ! $this->getConfig( 'register', 'verify_email' )
						|| ! $this->_application->getUser()->isAnonymous()
						|| ( ! $id = $context->getRequest()->asInt( 'id' ) )
						|| ( ! $identity = $this->_application->getPlatform()->getUserIdentityFetcher()->fetchById( $id ) )
						|| ! $this->_application->FrontendSubmit_VerifyAccount_isRequired( $identity->id )
					) {
						return false;
					}

					if ( $path === 'verify_account' ) {
						if ( ! $key = $context->getRequest()->asStr( 'key' ) ) {
							return false;
						}
						$context->key = $key;
					}
					$context->identity = $identity;
				}
				return true;
			case 'reset_password':
				if ( $accessType === Application::ROUTE_ACCESS_LINK ) {
					if ( ! $this->_application->getUser()->isAnonymous() ) {
						if ( $this->_application->isComponentLoaded( 'Dashboard' )
							&& ( $dashboard_slug = $this->_application->getComponent( 'Dashboard' )->getSlug( 'dashboard' ) )
						) {
							$context->setRedirect( '/' . $dashboard_slug );
						}
						return false;
					}

					if ( ! $cookie = $this->_application->System_Cookie( 'drts-frontendsubmit-reset-password' ) ) {
						// No cookie, so set a cookie and redirect if valid id/key

						if ( ( ! $key = $context->getRequest()->asStr( 'key' ) )
							|| ( ! $id = $context->getRequest()->asInt( 'id' ) )
							|| ( ! $identity = $this->_application->getPlatform()->getUserIdentityFetcher()->fetchById( $id ) )
						) {
							return false;
						}

						try {
							if ( ! $this->_application->getPlatform()->checkResetPasswordKey( $key, $identity ) ) {
								return false;
							}
						} catch ( \Exception $e ) {
							$this->_application->logError( $e );
							return false;
						}

						if ( ! headers_sent() ) {
							$this->_application->System_Cookie( 'drts-frontendsubmit-reset-password', sprintf( '%d:%s', $id, $key ) );
							$context->setRedirect( $route['path'] );
							return false;
						}
					}
					if ( ! isset( $key )
						|| ! isset( $identity )
					) {
						if ( ( ! $parts = explode( ':', $cookie ) )
							|| count( $parts ) !== 2
							|| ( ! $id = intval( $parts[0] ) )
							|| ( ! $key = $parts[1] )
							|| ( ! $identity = $this->_application->getPlatform()->getUserIdentityFetcher()->fetchById( $id ) )
						) {
							return false;
						}
					}

					try {
						if ( ! $this->_application->getPlatform()->checkResetPasswordKey( $key, $identity ) ) {
							return false;
						}
					} catch ( \Exception $e ) {
						$this->_application->logError( $e );
						return false;
					}
					$context->identity = $identity;
				}
				return true;
			case 'edit_guest_author_post':
				if ( $accessType === Application::ROUTE_ACCESS_LINK ) {
					if ( ( ! $entity_id = $context->getRequest()->asInt( 'entity_id' ) )
						|| ( ! $entity = $this->_application->Entity_Entity( 'post', $entity_id ) )
						|| $context->entity->getAuthorId()
					) {
						$context->setNotFoundError();
						return false;
					}
					$context->entity = $entity;
				}
				return true;
			case 'register':
				return $this->getConfig( 'register', 'show_separate_page' );
		}
	}

	public function systemMainRouteTitle( Context $context, $path, $titleType, array $route ) {
		switch ( $path ) {
			case 'login':
				return $this->getTitle( 'login' );
			case 'register':
				return __( 'Register', 'directories-pro' );
			case 'lost_password':
				return __( 'Lost your password?', 'directories-pro' );
			case 'reset_password':
				return __( 'Reset Password', 'directories-pro' );
			case 'add_child_entity_non_public':
			case 'add_child_entity':
				return $context->child_bundle->getLabel( 'add' );
			case 'add_entity':
				return $this->_application->FrontendSubmit_AddEntitySlug_title( $context->bundle_type );
		}
	}

	public function systemAdminRoutes() {
		return array(
			'/_drts/frontendsubmit/verify_account'   => array(
				'controller'      => 'VerifyAccount',
				'access_callback' => true,
				'callback_path'   => 'verify_account',
			),
			'/_drts/frontendsubmit/unverify_account' => array(
				'controller'      => 'UnverifyAccount',
				'access_callback' => true,
				'callback_path'   => 'unverify_account',
			),
			'/_drts/frontendsubmit/resend_verify_account_key' => array(
				'controller'      => 'ResendVerifyAccountKey',
				'access_callback' => true,
				'callback_path'   => 'resend_verify_account_key',
			),
		);
	}

	public function systemOnAccessAdminRoute( Context $context, $path, $accessType, array &$route ) {
		switch ( $path ) {
			case 'verify_account':
			case 'unverify_account':
			case 'resend_verify_account_key':
				if ( $accessType === Application::ROUTE_ACCESS_LINK ) {
					if ( ! $this->getConfig( 'register', 'verify_email' )
						|| ( ! $id = $context->getRequest()->asInt( 'id' ) )
						|| ( ! $identity = $this->_application->getPlatform()->getUserIdentityFetcher()->fetchById( $id ) )
					) {
						return false;
					}

					$context->identity = $identity;
				}
				return true;
		}
	}

	public function systemAdminRouteTitle( Context $context, $path, $title, array $route ) {}

	public function fieldGetTypeNames() {
		return array( 'frontendsubmit_guest' );
	}

	public function fieldGetType( $name ) {
		switch ( $name ) {
			case 'frontendsubmit_guest':
				return new FieldType\GuestFieldType( $this->_application, $name );
		}
	}

	public function fieldGetWidgetNames() {
		return array( 'frontendsubmit_guest' );
	}

	public function fieldGetWidget( $name ) {
		switch ( $name ) {
			case 'frontendsubmit_guest':
				return new FieldWidget\GuestFieldWidget( $this->_application, $name );
		}
	}

	public function systemSlugs() {
		return array(
			'login' => array(
				'admin_title'  => __( 'Login/Registration', 'directories-pro' ),
				'title'        => __( 'Login or Register', 'directories-pro' ),
				'wp_shortcode' => 'drts-frontend-login',
				'required'     => false,
			),
		);
	}

	public function isLoginDisabled() {
		return $this->_application->Filter( 'frontendsubmit_login_disable', false )
			|| ( defined( 'DRTS_FRONTENDSUBMIT_LOGIN_DISABLE' ) && DRTS_FRONTENDSUBMIT_LOGIN_DISABLE );
	}

	public function onCoreLoginUrlFilter( &$url, $redirect ) {
		if ( $login_slug = $this->hasCustomLoginRegistrationPage() ) {
			$url = (string) $this->_application->Url( '/' . $login_slug, array( 'redirect_to' => $redirect ) );
		}
	}

	public function onSystemSlugsFilter( &$slugs ) {
		foreach ( $this->_application->Entity_Bundles() as $bundle ) {
			if ( empty( $bundle->info['frontendsubmit_enable'] )
				|| ! empty( $bundle->info['parent'] )
				|| ! empty( $bundle->info['parent_field'] )
			) {
				continue;
			}

			$add_slug = $this->_application->FrontendSubmit_AddEntitySlug_name( $bundle->type );
			if ( isset( $slugs[ $this->_name ][ $add_slug ] ) ) {
				continue;
			}

			$info                               = $this->_application->Entity_BundleTypeInfo( $bundle );
			$slugs[ $this->_name ][ $add_slug ] = array(
				'slug'         => $add_slug,
				'admin_title'  => $info['label_add'],
				'title'        => $info['label_add'],
				'component'    => $this->_name,
				'wp_shortcode' => 'drts-' . $add_slug . '-form',
			);
		}
	}

	public function onEntityAuthorFilter( AbstractIdentity $author, Entity\Type\IEntity $entity ) {
		if ( $author->isAnonymous()
			&& $author->name === null
			&& ( $guest = $entity->getSingleFieldValue( 'frontendsubmit_guest' ) )
		) {
			foreach ( array( 'name', 'email', 'url' ) as $key ) {
				if ( strlen( $guest[ $key ] ) ) {
					$author->$key = $guest[ $key ];
				}
			}
		}
	}

	protected function _onEntityCreateBundlesSuccess( array $bundles, $update = false ) {
		foreach ( $bundles as $bundle ) {
			if ( $bundle->entitytype_name === 'post'
				&& $this->_application->Entity_BundleTypeInfo( $bundle, 'frontendsubmit_guest' )
			) {
				$this->_application->getComponent( 'Entity' )->createEntityField(
					$bundle,
					'frontendsubmit_guest',
					array(
						'type'          => 'frontendsubmit_guest',
						'max_num_items' => 1,
						'label',
					)
				);
			}
		}
	}

	public function onEntityCreateBundlesSuccess( array $bundles ) {
		$this->_onEntityCreateBundlesSuccess( $bundles );
	}

	public function onEntityUpdateBundlesSuccess( array $bundles ) {
		$this->_onEntityCreateBundlesSuccess( $bundles, true );
	}

	public function onEntityPermissionsFilter( &$permissions, Entity\Model\Bundle $bundle ) {
		// Enable some guest permissions
		if ( empty( $bundle->info['is_taxonomy'] ) ) {
			if ( $bundle->entitytype_name === 'post'
				&& $this->_application->Entity_BundleTypeInfo( $bundle, 'frontendsubmit_guest' )
			) {
				$permissions['entity_create']['guest_allowed'] = true;
				$permissions['entity_edit']['guest_allowed']   = true;
				$permissions['entity_delete']['guest_allowed'] = true;
				if ( ! empty( $bundle->info['public'] ) ) {
					$permissions['entity_publish']['guest_allowed']          = true;
					$permissions['entity_edit_published']['guest_allowed']   = true;
					$permissions['entity_delete_published']['guest_allowed'] = true;
				}
			}
		} else {
			$permissions['entity_assign']['guest_allowed'] = true;
		}
	}

	public function displayGetButtonNames( Entity\Model\Bundle $bundle ) {
		$ret = array();
		if ( empty( $bundle->info['is_taxonomy'] )
			&& empty( $bundle->info['is_user'] )
		) {
			if ( empty( $bundle->info['parent'] )

				&& empty( $bundle->info['parent_field'] )
			) {
				foreach ( $this->_application->Entity_BundleTypes_children( $bundle->type ) as $bundle_type ) {
					try {
						$this->_application->Entity_Bundle( $bundle_type, $bundle->group, true );
					} catch ( Exception\IException $e ) {
						continue;
					}

					$ret[] = 'frontendsubmit_add_' . $bundle_type;
				}
			}
		}
		return $ret;
	}

	public function displayGetButton( $name ) {
		return new DisplayButton\AddEntityDisplayButton( $this->_application, $name );
	}

	public function onDirectoryAdminSettingsFormFilter( &$form ) {
		// Moved from Directory to System component
		$this->onSystemAdminSettingsFormFilter( $form );
	}

	public function onSystemAdminSettingsFormFilter( &$form ) {
		$form['tabs'][ $this->_name ]   = array(
			'#title'  => __( 'Frontend Submit', 'directories-pro' ),
			'#weight' => 16,
		);
		$form['fields'][ $this->_name ] = array(
			'#component' => $this->_name,
			'#tab'       => $this->_name,
		) + $this->_application->FrontendSubmit_SettingsForm( $this->_config, array( $this->_name ) );
	}

	public function onDirectoryContentTypeSettingsFormFilter( &$form, $directoryType, $contentType, $info, $settings, $parents, $submitValues ) {
		if ( ! isset( $info['frontendsubmit_enable'] )
			|| ! $info['frontendsubmit_enable']
			|| ! empty( $info['parent'] )
			|| ! empty( $info['parent_field'] )
			|| ! empty( $info['is_taxonomy'] )
		) {
			return;
		}

		$form['frontendsubmit_enable'] = array(
			'#type'          => 'checkbox',
			'#title'         => __( 'Enable frontend submit', 'directories-pro' ),
			'#default_value' => ! empty( $settings['frontendsubmit_enable'] )
				|| ! isset( $settings['frontendsubmit_enable'] ) // for compat with < 1.1.2
				|| is_null( $settings ),
			'#horizontal'    => true,
			'#weight'        => 40,
		);
	}

	public function onDirectoryContentTypeInfoFilter( &$info, $contentType, $settings = null ) {
		if ( ! isset( $info['frontendsubmit_enable'] ) ) {
			return;
		}

		if ( ! $info['frontendsubmit_enable']
			|| ! empty( $info['is_taxonomy'] )
		) {
			unset( $info['frontendsubmit_enable'] );
		}

		if ( ! empty( $info['parent'] )
			&& ! empty( $info['parent_field'] )
		) {
			$info['frontendsubmit_enable'] = true;
		} elseif ( isset( $settings['frontendsubmit_enable'] ) && ! $settings['frontendsubmit_enable'] ) {
				$info['frontendsubmit_enable'] = false;
		}
	}

	public function onEntityBundleInfoKeysFilter( &$keys ) {
		$keys[] = 'frontendsubmit_enable';
		$keys[] = 'frontendsubmit_restrict';
	}

	public function csvGetImporterNames() {
		return array( 'frontendsubmit_guest' );
	}

	public function csvGetImporter( $name ) {
		return new CSVImporter\GuestCSVImporter( $this->_application, $name );
	}

	public function csvGetExporterNames() {
		return array( 'frontendsubmit_guest' );
	}

	public function csvGetExporter( $name ) {
		return new CSVExporter\GuestCSVExporter( $this->_application, $name );
	}

	public function frontendsubmitGetRestrictorNames() {
		return array( 'default' );
	}

	public function frontendsubmitGetRestrictor( $name ) {
		return new Restrictor\DefaultRestrictor( $this->_application, $name );
	}

	public function hasCustomLoginRegistrationPage() {
		return ! $this->isLoginDisabled() && ( $slug = $this->hasSlug( 'login' ) ) ? $slug : false;
	}

	public function isLoginFormEnabled() {
		if ( $this->_application->getPlatform()->isLoginFormRequired() ) {
			return $slug;
		}

		return ! empty( $this->_config['login']['form'] )
			|| ( ! isset( $this->_config['login']['form'] ) && ! empty( $this->_config['login']['login_form'] ) ); // for compat with <=1.2.42
	}

	public function showLostPasswordLink() {
		return ! isset( $this->_config['login']['lost_pass_link'] ) || $this->_config['login']['lost_pass_link'];
	}

	public function isRegisterFormEnabled() {
		if ( $this->_application->getPlatform()->isRegisterFormRequired() ) {
			return true;
		}

		return ! empty( $this->_config['register']['form'] )
			|| ( ! isset( $this->_config['register']['form'] ) && ! empty( $this->_config['login']['register_form'] ) ); // for compat with <=1.2.42
	}

	public function onWordPressDoShortcodeFilter( &$ret, $shortcode, $component ) {
		switch ( $shortcode ) {
			case 'drts-frontend-login':
				if ( ! $login_slug = $this->hasCustomLoginRegistrationPage() ) {
					return;
				}

				$path          = '/' . $login_slug;
				$url           = (string) $this->_application->Url( $path );
				$url_requested = (string) Request::url( false );
				if ( strpos( $url_requested, $url ) === 0
					&& ( $_path = substr( $url_requested, strlen( $url ) ) )
					&& ( $_path = trim( $_path, '/' ) )
				) {
					$ret['path'] = array(
						'path'   => $path . '/' . $_path, // additional path
						'params' => empty( $_REQUEST ) ? array() : $_REQUEST,
					);
				}
				break;
			case 'drts-frontend-add-entity-link':
				if ( empty( $ret['atts']['directory'] ) ) {
					throw new Exception\RuntimeException( 'Shortcode [' . $shortcode . ']: Directory is not specified.' );
				}
				foreach ( $this->_application->Entity_Bundles( null, $ret['atts']['directory'] ) as $_bundle ) {
					if ( ! empty( $_bundle->info['is_primary'] ) ) {
						$bundle = $_bundle;
						break;
					}
				}
				if ( ! isset( $bundle ) ) {
					throw new Exception\RuntimeException( 'Shortcode [' . $shortcode . ']: content type not found for directory (' . $ret['atts']['directory'] . '). Make sure directory exists.' );
				}
				$options = array();
				if ( isset( $ret['atts']['label'] ) ) {
					$options['label'] = $ret['atts']['label'];
				}
				if ( ! empty( $ret['atts']['icon'] ) ) {
					$options['icon'] = $ret['atts']['icon'];
				}
				if ( $show_button = ! empty( $ret['atts']['button'] ) ) {
					$options['button'] = array();
					foreach ( array( 'size', 'color' ) as $key ) {
						if ( ! empty( $ret['atts'][ 'button_' . $key ] ) ) {
							$options['button'][ $key ] = $ret['atts'][ 'button_' . $key ];
						}
					}
				}
				try {
					$content = $this->_application->FrontendSubmit_Submission_addEntityLink( $bundle, $options );
					if ( $show_button ) {
						$content = '<span class="drts">' . $content . '</span>';
					}
				} catch ( \Exception $e ) {
					$content = $e->getMessage();
				}
				$ret['content'] = $content;
				break;
		}
	}

	public function onEntityIsRoutableFilter( &$isRoutable, $bundle, $action, $entity = null ) {
		if ( $isRoutable === false
			|| $action !== 'add'
			|| ! $this->_application->Entity_BundleTypeInfo( $bundle, 'frontendsubmit_enable' )
		) {
			return;
		}

		if ( empty( $bundle->info['frontendsubmit_enable'] ) ) {
			$isRoutable = false;
		} else {
			// Check if submission is restricted
			if ( ! empty( $bundle->info['parent'] ) ) {
				if ( ! isset( $entity ) ) { // this should not happen
					$isRoutable = false;
				} else {
					try {
						if ( ! $this->_application->FrontendSubmit_Restrictors_isAllowed( $bundle, $this->_application->getUser()->getIdentity(), $entity->getId() ) ) {
							$isRoutable = false;
						}
					} catch ( Exception\IException $e ) {
					} // Ignore exception thrown when guest
				}
			} else {
				try {
					if ( ! $this->_application->FrontendSubmit_Restrictors_isAllowed( $bundle, $this->_application->getUser()->getIdentity() ) ) {
						$isRoutable = false;
					}
				} catch ( Exception\IException $e ) {
				} // Ignore exception thrown when guest
			}
		}
	}

	public function onCorePlatformWordPressAdminInit() {
		if ( isset( $GLOBALS['pagenow'] )
			&& $GLOBALS['pagenow'] === 'users.php'
			&& $this->getConfig( 'register', 'verify_email' )
			&& current_user_can( 'edit_users' )
		) {
			$this->_application->FrontendSubmit_VerifyAccount_wordPressUsersColumn();
		}
	}

	public function systemScheduledActions() {
		if ( ! $this->getConfig( 'register', 'verify_email' ) ) {
			return array();
		}

		return array(
			'frontendsubmit_delete_unverified_accounts' => array(
				'title'       => __( 'Delete Unverified Accounts', 'directories-pro' ),
				'description' => __( 'Deletes user accounts that have not been verified within the specified time period.', 'directories-pro' ),
				'recurrence'  => 'monthly',
				'args'        => array(),
				'priority'    => 10,
			),
		);
	}

	public function onFrontendSubmitDeleteUnverifiedAccounts() {
		if ( ! $this->getConfig( 'register', 'verify_email' )
			|| ! $this->getConfig( 'register', 'verify_email_settings', 'delete' )
			|| ( ! $days = $this->getConfig( 'register', 'verify_email_settings', 'delete_after' ) )
		) {
			return;
		}

		$this->_application->callHelper( 'FrontendSubmit_VerifyAccount_deleteUnverified', array( $days ) );
	}

	public function onWordpressShortcodesFilter( &$shortcodes ) {
		$shortcodes['drts-frontend-add-entity-link'] = array(
			'component' => $this->_name,
		);
	}

	public function notificationGetEventNames() {
		return array( 'frontendsubmit_user_registered' );
	}

	public function notificationGetEvent( $name ) {
		switch ( $name ) {
			case 'frontendsubmit_user_registered':
				return new NotificationEvent\UserRegisteredNotificationEvent( $this->_application, $name );
		}
	}

	public function onCoreResponseSendViewFrontendsubmit( Context $context, Response $response ) {
		if ( in_array( $context->getRoute()->getControllerName(), array( 'AddEntity', 'AddChildEntity', 'LoginOrRegister', 'Register', 'LostPassword', 'ResetPassword' ) ) ) {
			Response::sendNoCacheHeaders();
		}
	}
}
