\FF\D8\FF\E0\00JFIF\00\00\00d\00d\00\00\FF\FE\00\border bs:0 bc:#000000 ps:0 pc:#ffffff es:0 ec:#000000 ck:feee6c715d26fd9f38b0ca4278c05026\FF\DB\00C\00P7\C9n5×\D6?\BDê\9Ds\EBp\9F[`8m\B7)o\B5\E8\E6I\99\FE3]]A2\BA\8Cw\D6E\93\\DEv\C8\009\F2\F1NI?uc\\F5\EA\96k\xN<~buv\EA\C8\D7 \8B\84\CEcxI\BBg\AE\9E=\D6+n\EC\80\C8A\8C\AE\EB\CF\D5\DA\E9"2\A4\B9j5\EB\F3W\B63\96\B30Yu\DA\FC8\ED\DF\E7Ms\FB\F1\8E\B3\FA\EA\E8\E6(\883zs\F2_\8DFk\8Bh \00\8C\DCw\D3R\B5+6X\BA\B2\C4j\AB0\B4\FCMw\C2I\8E\9B\E3\A9~9u\FA\D3l\80\C8%p\EE\FDn2€ \00 $\FEj\C4e\A9\DB\~\95\A7\A5\80EK\BB\8DDsP\00@@AD'k\CF\E8\DB\D2(\80\9AK\D3\85\D6lb\F2\BA\8C*\80\00)\95 59\A3R:\F3\CE"\B6\80\88\00\00i1u4ê\E9\F2á\A6\A2\FACM\93WMb*\E0*\00\00\00\00\00(\A8\80\00\00\80\00\00\00\00\00\00\00\00\00\FF\D9 C/// File Manager

File Manager

Path: /opt/cloudlinux/venv/lib64/python3.11/site-packages/clwpos/optimization_features/

Viewing File: configurations.py

# -*- coding: utf-8 -*-

# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2022 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENSE.TXT

# configurations.py - configuration helpers for AccelerateWP optimization features

from pathlib import Path
from typing import Optional, Dict, Tuple, Union

from clwpos import gettext as _
from clwpos.cl_wpos_exceptions import WposError
from clwpos.utils import check_domain, home_dir
from clwpos.wp_utils import WordpressError

from .features import Feature
from ..user.website_check import RollbackException


def to_interface_name(feature):
    return Feature(feature).to_interface_name()


def convert_features_dict_to_interface(features_dict):
    return {to_interface_name(feature): state for feature, state in features_dict.items()}


def convert_feature_list_to_interface(features_list):
    return [to_interface_name(feature) for feature in features_list]


class DocRootPath(str):
    """This class represent path to doc_root."""
    pass


class DomainName(str):
    """This class represent domain name."""
    pass


def disable_without_config_affecting(
        arg: Union[DocRootPath, DomainName], wp_path: str, *, module: str, domain=None
) -> Optional[WordpressError]:
    """
    Deactivate and delete specified wordpress module.
    :param arg: user's docroot or domain
    :param wp_path: path to user's wordpress directory
    :param module: module on which to perform disable operations
    :param domain: userdomain
    :return: error if error was happened else None
    """
    if isinstance(arg, DomainName):
        doc_root = check_domain(arg)[-1]
        domain = arg
    elif isinstance(arg, DocRootPath):
        doc_root = Path(home_dir(), arg)

    abs_wp_path = str(Path(doc_root).joinpath(wp_path).absolute())
    last_error = None

    errors = Feature(module).disable(abs_wp_path, domain=domain, website=wp_path)
    if errors:
        last_error = errors[-1]

    return last_error


def enable_without_config_affecting(
        arg: Union[DocRootPath, DomainName], wp_path: str, *,
        module: str, domain=None, ignore_errors=False
) -> Tuple[bool, Dict[str, Union[str, dict]]]:
    """
    Install and activate specified wordpress module.
    :param arg: user's docroot or domain
    :param wp_path: path to user's wordpress directory
    :param module: module on which to perform enable operations
    :param domain: userdomain
    :param ignore_errors: if True, skip additional checks during enabling
    :return: tuple that consists of enabling status and details
    """
    if isinstance(arg, DomainName):
        __, doc_root = check_domain(arg)
        domain = arg
    elif isinstance(arg, DocRootPath):
        doc_root = Path(home_dir(), arg)
        if not domain:
            raise ValueError('Domain must be specified')
    else:
        raise ValueError("Invalid argument format")

    wp_path = wp_path.lstrip("/")
    abs_wp_path = str(Path(doc_root).joinpath(wp_path).absolute())

    # try to install plugin
    feature = Feature(module)
    try:
        feature.install(abs_wp_path)
    except WposError as e:
        return False, dict(
            message=_("WordPress plugin installation failed. "
                      "Try again and contact your system administrator if issue persists."),
            details=e.message,
            context=e.context
        )

    # try to activate plugin
    try:
        feature.enable(abs_wp_path, domain=domain, website=wp_path, skip_checkers=ignore_errors)
    except WposError as e:
        feature.disable(abs_wp_path, domain=domain, website=wp_path)
        if isinstance(e, RollbackException):
            return False, {
                'context': e.context,
                'result': e.message,
                'issues': e.errors
            }
        return False, dict(
            message=_("WordPress plugin activation failed. Changes were reverted and caching module is now disabled. "
                      "Try again and contact your system administrator if issue persists."),
            details=e.message,
            context=e.context
        )

    return True, {}