<commit_before><?php
/**
 * TheSSLStore plugin handler
 */
class ThesslstorePlugin extends Plugin
{
    /**
     * Init
     */
    public function __construct()
    {
        $this->loadConfig(dirname(__FILE__) . DS . 'config.json');
        Language::loadLang("thesslstore_plugin", null, dirname(__FILE__) . DS . "language" . DS);
    }

    /**
     * Returns the name of this plugin
     *
     * @return string The common name of this plugin
     */
    public function getName()
    {
        return Language::_("TheSSLStorePlugin.name", true);
    }

    /**
     * Performs any necessary bootstraping actions
     *
     * @param int $plugin_id The ID of the plugin being installed
     */
    public function install($plugin_id)
    {

        Loader::loadModels($this, array('CronTasks'));
        $this->addCronTasks($this->getCronTasks());
        // Create Email group for expiration reminder email
        Loader::loadModels($this, array("Emails", "EmailGroups", "Languages", "Permissions"));
        $group = array(
                'action' => "Thesslstore.expiration_reminder",
                'type' => "client",
                'plugin_dir' => "thesslstore",
                'tags' => "{client.first_name},{service.name},{service.id},{servicelink},{days}"
        );
        // Add the custom group
        $group_id = $this->EmailGroups->add($group);
        // Fetch all currently-installed languages for this company, for which email templates should be created for
        $languages = $this->Languages->getAll(Configure::get("Blesta.company_id"));
        // Add the email template for each language
        foreach ($languages as $language)
        {
            $data = array(
                'email_group_id' => $group_id,
                'company_id' => Configure::get('Blesta.company_id'),
                'lang' => $language->code,
                'from' => '<EMAIL>',
                'from_name' => 'My Company',
                'subject' => 'Certificate Expiration Reminder',
                'text' => 'Hello {client.first_name},
                This is a reminder that your SSL Certificate ({service.name}) with service ID#{service.id} (http://{servicelink}) is set to expire in {days} days.
                Please renew at your earliest convenience.
                Thank you!',
                'html' => '<p>Hello {client.first_name},</p>
                <p>This is a reminder that your SSL Certificate ({service.name}) with service ID#{service.id} (<a href="http://{servicelink}">{servicelink}</a>) is set to expire in {days} days.</p>
                <p>Please renew at your earliest convenience.</p>
                <p>Thank you!</p>'
            );
            $this->Emails->add($data);
        }
    }

    /**
     * Performs migration of data from $current_version (the current installed version)
     * to the given file set version
     *
     * @param string $current_version The current installed version of this plugin
     * @param int $plugin_id The ID of the plugin being upgraded
     */
    public function upgrade($current_version, $plugin_id)
    {
        // Upgrade if possible
        if (version_compare($this->getVersion(), $current_version, '>')) {
            // Handle the upgrade, set errors using $this->Input->setErrors() if any errors encountered

            // Upgrade to 1.2.2
            if (version_compare($current_version, '1.2.2', '<')) {
                $this->upgrade1_2_2();
            }

            // Upgrade to 1.3.0
            if (version_compare($current_version, '1.3.0', '<')) {
                $this->upgrade1_3_0();
            }
        }
    }

    /**
     * Upgrades to v1.2.2 of the plugin
     * Changes email template tags, but leaves existing HTML/text content alone otherwise
     */
    private function upgrade1_2_2()
    {
        // Update all email templates to change the tags
        Loader::loadComponents($this, array('Record'));
        Loader::loadModels($this, array('EmailGroups'));

        if (($email_group = $this->EmailGroups->getByAction('Thesslstore.expiration_reminder'))) {
            // Update the email template tags to change the package.name to service.name
            $tags = '{client.first_name},{service.name},{service.id},{servicelink},{days}';
            $this->Record->where('id', '=', $email_group->id)
                ->update('email_groups', array('tags' => $tags));

            $emails = $this->Record->select(array('id', 'text', 'html'))
                ->from('emails')
                ->where('email_group_id', '=', $email_group->id)
                ->fetchAll();

            // Set HTTP protocol for servicelink tag and replace the package.name tag in all emails
            foreach ($emails as $email) {
                $search = array('{servicelink}', '{package.name}', '{package.');
                $replace = array('http://{servicelink}', '{service.name}', '{service.package.');
                $vars = array(
                    'text' => str_replace($search, $replace, $email->text),
                    'html' => str_replace($search, $replace, $email->html)
                );

                if ($vars['html'] != $email->html || $vars['text'] != $email->text) {
                    $this->Record->where('id', '=', $email->id)->update('emails', $vars);
                }
            }
        }
    }

    /**
     * Upgrades to v1.3.0 of the plugin
     * Removes service synchronization cron task that was moved to module
     */
    private function upgrade1_3_0()
    {
        Loader::loadModels($this, ['CronTasks']);

        $cron_tasks = $this->getCronTasks();

        // Remove the cron task
        foreach ($cron_tasks as $task) {
            $cron_task = $this->CronTasks->getByKey('tss_order_sync', 'thesslstore', 'plugin');
            if ($cron_task) {
                $this->CronTasks->deleteTask($cron_task->id, 'plugin', 'thesslstore');
            }
        }

        // Remove individual cron task run
        foreach ($cron_tasks as $task) {
            $cron_task_run = $this->CronTasks->getTaskRunByKey('tss_order_sync', 'thesslstore', false, 'plugin');
            if ($cron_task_run) {
                $this->CronTasks->deleteTaskRun($cron_task_run->task_run_id);
            }
        }
    }

    /**
     * Performs any necessary cleanup actions
     *<commit_msg>Simplified code<commit_after><?php
/**
 * TheSSLStore plugin handler
 */
class ThesslstorePlugin extends Plugin
{
    /**
     * Init
     */
    public function __construct()
    {
        $this->loadConfig(dirname(__FILE__) . DS . 'config.json');
        Language::loadLang("thesslstore_plugin", null, dirname(__FILE__) . DS . "language" . DS);
    }

    /**
     * Returns the name of this plugin
     *
     * @return string The common name of this plugin
     */
    public function getName()
    {
        return Language::_("TheSSLStorePlugin.name", true);
    }

    /**
     * Performs any necessary bootstraping actions
     *
     * @param int $plugin_id The ID of the plugin being installed
     */
    public function install($plugin_id)
    {

        Loader::loadModels($this, array('CronTasks'));
        $this->addCronTasks($this->getCronTasks());
        // Create Email group for expiration reminder email
        Loader::loadModels($this, array("Emails", "EmailGroups", "Languages", "Permissions"));
        $group = array(
                'action' => "Thesslstore.expiration_reminder",
                'type' => "client",
                'plugin_dir' => "thesslstore",
                'tags' => "{client.first_name},{service.name},{service.id},{servicelink},{days}"
        );
        // Add the custom group
        $group_id = $this->EmailGroups->add($group);
        // Fetch all currently-installed languages for this company, for which email templates should be created for
        $languages = $this->Languages->getAll(Configure::get("Blesta.company_id"));
        // Add the email template for each language
        foreach ($languages as $language)
        {
            $data = array(
                'email_group_id' => $group_id,
                'company_id' => Configure::get('Blesta.company_id'),
                'lang' => $language->code,
                'from' => '<EMAIL>',
                'from_name' => 'My Company',
                'subject' => 'Certificate Expiration Reminder',
                'text' => 'Hello {client.first_name},
                This is a reminder that your SSL Certificate ({service.name}) with service ID#{service.id} (http://{servicelink}) is set to expire in {days} days.
                Please renew at your earliest convenience.
                Thank you!',
                'html' => '<p>Hello {client.first_name},</p>
                <p>This is a reminder that your SSL Certificate ({service.name}) with service ID#{service.id} (<a href="http://{servicelink}">{servicelink}</a>) is set to expire in {days} days.</p>
                <p>Please renew at your earliest convenience.</p>
                <p>Thank you!</p>'
            );
            $this->Emails->add($data);
        }
    }

    /**
     * Performs migration of data from $current_version (the current installed version)
     * to the given file set version
     *
     * @param string $current_version The current installed version of this plugin
     * @param int $plugin_id The ID of the plugin being upgraded
     */
    public function upgrade($current_version, $plugin_id)
    {
        // Upgrade if possible
        if (version_compare($this->getVersion(), $current_version, '>')) {
            // Handle the upgrade, set errors using $this->Input->setErrors() if any errors encountered

            // Upgrade to 1.2.2
            if (version_compare($current_version, '1.2.2', '<')) {
                $this->upgrade1_2_2();
            }

            // Upgrade to 1.3.0
            if (version_compare($current_version, '1.3.0', '<')) {
                $this->upgrade1_3_0();
            }
        }
    }

    /**
     * Upgrades to v1.2.2 of the plugin
     * Changes email template tags, but leaves existing HTML/text content alone otherwise
     */
    private function upgrade1_2_2()
    {
        // Update all email templates to change the tags
        Loader::loadComponents($this, array('Record'));
        Loader::loadModels($this, array('EmailGroups'));

        if (($email_group = $this->EmailGroups->getByAction('Thesslstore.expiration_reminder'))) {
            // Update the email template tags to change the package.name to service.name
            $tags = '{client.first_name},{service.name},{service.id},{servicelink},{days}';
            $this->Record->where('id', '=', $email_group->id)
                ->update('email_groups', array('tags' => $tags));

            $emails = $this->Record->select(array('id', 'text', 'html'))
                ->from('emails')
                ->where('email_group_id', '=', $email_group->id)
                ->fetchAll();

            // Set HTTP protocol for servicelink tag and replace the package.name tag in all emails
            foreach ($emails as $email) {
                $search = array('{servicelink}', '{package.name}', '{package.');
                $replace = array('http://{servicelink}', '{service.name}', '{service.package.');
                $vars = array(
                    'text' => str_replace($search, $replace, $email->text),
                    'html' => str_replace($search, $replace, $email->html)
                );

                if ($vars['html'] != $email->html || $vars['text'] != $email->text) {
                    $this->Record->where('id', '=', $email->id)->update('emails', $vars);
                }
            }
        }
    }

    /**
     * Upgrades to v1.3.0 of the plugin
     * Removes service synchronization cron task that was moved to module
     */
    private function upgrade1_3_0()
    {
        Loader::loadModels($this, ['CronTasks']);

        // Remove the cron task
        $cron_task = $this->CronTasks->getByKey('tss_order_sync', 'thesslstore', 'plugin');
        if ($cron_task) {
            $this->CronTasks->deleteTask($cron_task->id, 'plugin', 'thesslstore');
        }
    }

    /**
     * Performs any necessary cleanup actions
     *
{'fraction_non_alphanumeric': 0.14380205781479666, 'fraction_numerical': 0.004899559039686428, 'mean_word_length': 2.089556004036327, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 143, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '734458', 'n_tokens_mistral': 3471, 'n_tokens_neox': 3358, 'n_words': 852}