HEX
Server: Apache
System: Linux vps-3158868-x.dattaweb.com 3.10.0-1160.119.1.el7.x86_64 #1 SMP Tue Jun 4 14:43:51 UTC 2024 x86_64
User: emerlux (1185)
PHP: 8.3.1
Disabled: system, shell, exec, system_exec, shell_exec, mysql_pconnect, passthru, popen, proc_open, proc_close, proc_nice, proc_terminate, proc_get_status, escapeshellarg, escapeshellcmd, eval
Upload Files
File: /home/emerlux/public_html/wp-content/plugins/woocommerce-mercadopago/src/Helpers/CreditsEnabled.php
<?php

namespace MercadoPago\Woocommerce\Helpers;

use Exception;
use MercadoPago\Woocommerce\Hooks\Admin;
use MercadoPago\Woocommerce\Libraries\Logs\Logs;
use MercadoPago\Woocommerce\Hooks\Options;
use MercadoPago\Woocommerce\Gateways\CreditsGateway;
use MercadoPago\Woocommerce\Gateways\BasicGateway;

if (!defined('ABSPATH')) {
    exit;
}

class CreditsEnabled
{
    private const CREDITS_ACTIVATION_NEEDED = 'mercadopago_credits_activation_needed';

    private const ALREADY_ENABLE_BY_DEFAULT = 'mercadopago_already_enabled_by_default';

    private Admin $admin;

    private Logs $logs;

    private Options $options;

    /**
     * CreditsEnabled constructor
     *
     * @param Admin $admin
     * @param Logs $logs
     * @param Options $options
     */
    public function __construct(
        Admin $admin,
        Logs $logs,
        Options $options
    ) {
        $this->admin   = $admin;
        $this->logs    = $logs;
        $this->options = $options;
    }

    /**
     * Set default CreditsEnabled options when needed
     */
    public function setCreditsDefaultOptions(): void
    {
        if ($this->admin->isAdmin() && $this->options->get(self::CREDITS_ACTIVATION_NEEDED) !== 'no') {
            $this->options->set(self::CREDITS_ACTIVATION_NEEDED, 'yes');
            $this->options->set(self::ALREADY_ENABLE_BY_DEFAULT, 'no');
        }
    }

    /**
     * Enable credits on the first execution
     */
    public function enableCreditsAction(): void
    {
        $this->setCreditsDefaultOptions();

        try {
            if ($this->admin->isAdmin() && $this->options->get(self::CREDITS_ACTIVATION_NEEDED) === 'yes') {
                $this->options->set(self::CREDITS_ACTIVATION_NEEDED, 'no');

                $basicGateway   = new BasicGateway();
                $creditsGateway = new CreditsGateway();

                if ($this->options->get(self::ALREADY_ENABLE_BY_DEFAULT) === 'no') {
                    if (
                        isset($creditsGateway->settings['already_enabled_by_default']) &&
                        $this->options->getGatewayOption($creditsGateway, 'already_enabled_by_default')
                    ) {
                        return;
                    }

                    if (
                        isset($basicGateway->settings['enabled']) &&
                        $this->options->getGatewayOption($basicGateway, 'enabled')  === 'yes' &&
                        $creditsGateway->isAvailable()
                    ) {
                        $creditsGateway->activeByDefault();
                        $this->options->set(self::ALREADY_ENABLE_BY_DEFAULT, 'yes');
                    }
                }

                $this->logs->file->info('Credits was activated automatically', __METHOD__);
            }
        } catch (Exception $ex) {
            $this->logs->file->error(
                "Mercado pago gave error to enable Credits: {$ex->getMessage()}",
                __CLASS__
            );
        }
    }
}