Craft\RetourPlugin::init PHP Method

init() public method

public init ( ) : mixed
return mixed
    public function init()
    {
        /* -- Listen for exceptions */
        craft()->onException = function (\CExceptionEvent $event) {
            if ($event->exception instanceof \CHttpException && $event->exception->statusCode == 404) {
                if (craft()->request->isSiteRequest() && !craft()->request->isLivePreview()) {
                    /* -- See if we should redirect */
                    $url = urldecode(craft()->request->getRequestUri());
                    $noQueryUrl = UrlHelper::stripQueryString($url);
                    /* -- Redirect if we find a match, otherwise let Craft handle it */
                    $redirect = craft()->retour->findRedirectMatch($url);
                    if (isset($redirect)) {
                        craft()->retour->incrementStatistics($url, true);
                        $event->handled = true;
                        RetourPlugin::log("Redirecting " . $url . " to " . $redirect['redirectDestUrl'], LogLevel::Info, false);
                        craft()->request->redirect($redirect['redirectDestUrl'], true, $redirect['redirectHttpCode']);
                    } else {
                        /* -- Now try it without the query string, too, otherwise let Craft handle it */
                        $redirect = craft()->retour->findRedirectMatch($noQueryUrl);
                        if (isset($redirect)) {
                            craft()->retour->incrementStatistics($url, true);
                            $event->handled = true;
                            RetourPlugin::log("Redirecting " . $url . " to " . $redirect['redirectDestUrl'], LogLevel::Info, false);
                            craft()->request->redirect($redirect['redirectDestUrl'], true, $redirect['redirectHttpCode']);
                        } else {
                            craft()->retour->incrementStatistics($url, false);
                        }
                    }
                }
            }
        };
        /* -- Listen for structure changes so we can regenerated our FieldType's URLs */
        craft()->on('structures.onMoveElement', function (Event $e) {
            $element = $e->params['element'];
            $elemType = $element->getElementType();
            if ($element) {
                if ($elemType == ElementType::Entry) {
                    /* -- Check the field layout, so that we only do this for FieldLayouts that have our Retour fieldtype in them */
                    $fieldLayouts = $element->fieldLayout->getFields();
                    foreach ($fieldLayouts as $fieldLayout) {
                        $field = craft()->fields->getFieldById($fieldLayout->fieldId);
                        if ($field->type == "Retour") {
                            craft()->elements->saveElement($element);
                            RetourPlugin::log("Resaved moved structure element", LogLevel::Info, false);
                            break;
                        }
                    }
                }
            }
        });
        /* -- Listen for entries whose slug changes */
        craft()->on('entries.onBeforeSaveEntry', function (Event $e) {
            $this->originalUris = array();
            if (!$e->params['isNewEntry']) {
                $entry = $e->params['entry'];
                $thisSection = $entry->getSection();
                if ($thisSection->hasUrls) {
                    $this->originalUris = craft()->retour->getLocalizedUris($entry);
                }
            }
        });
        craft()->on('entries.onSaveEntry', function (Event $e) {
            if (!$e->params['isNewEntry']) {
                $entry = $e->params['entry'];
                $newUris = craft()->retour->getLocalizedUris($entry);
                foreach ($newUris as $newUri) {
                    $oldUri = current($this->originalUris);
                    next($this->originalUris);
                    if (strcmp($oldUri, $newUri) != 0 && $oldUri != "") {
                        $record = new Retour_StaticRedirectsRecord();
                        /* -- Set the record attributes for our new auto-redirect */
                        $record->locale = $entry->locale;
                        $record->redirectMatchType = 'exactmatch';
                        $record->redirectSrcUrl = $oldUri;
                        if ($record->redirectMatchType == "exactmatch" && $record->redirectSrcUrl != "") {
                            $record->redirectSrcUrl = '/' . ltrim($record->redirectSrcUrl, '/');
                        }
                        $record->redirectSrcUrlParsed = $record->redirectSrcUrl;
                        $record->redirectDestUrl = $newUri;
                        if ($record->redirectMatchType == "exactmatch" && $record->redirectDestUrl != "") {
                            $record->redirectDestUrl = '/' . ltrim($record->redirectDestUrl, '/');
                        }
                        $record->redirectHttpCode = '301';
                        $record->hitLastTime = DateTimeHelper::currentUTCDateTime();
                        $record->associatedElementId = 0;
                        $result = craft()->retour->saveStaticRedirect($record);
                    }
                }
            }
        });
    }