<?php
namespace Wpupgrader\Libraries\WordPress\Plugins\Core\Settings\Page;

abstract class Callback
{
    public $settings;
    public $args;

    abstract protected function callbackAction();

    public function __construct($settings, $args)
    {
        $this->settings = $settings;
        $this->args = $args;
    }

    public function getSettingsKey()
    {
        return $this->settings->getSettingsKey();
    }

    public function getIdString()
    {
        return $this->getSettingsKey().'-'.$this->args['name'];
    }

    public function getNameString()
    {
        return $this->getSettingsKey().'['.$this->args['name'].']';
    }

    public function getNameIdString($subfield = null)
    {
        if (!empty($subfield)) {
            return 'id="'.\esc_attr($this->getIdString()).'['.$subfield.']" name="'.\esc_attr($this->getNameString()).'['.$subfield.']"';
        }
        else {
            return 'id="'.\esc_attr($this->getIdString()).'" name="'.\esc_attr($this->getNameString()).'"';
        }
    }

    public function getValue($subfield = null, $default = null)
    {
        $result = !empty($this->settings->options[$this->args['name']]) ? $this->settings->options[$this->args['name']] : (isset($this->args['default']) ? $this->args['default'] : $default);
        if (is_array($result) && !empty($subfield)) {
            return \esc_attr(isset($result[$subfield]) ? $result[$subfield] : $default);
        }
        return \esc_attr($result);
    }

    public function getDescription()
    {
        return !empty($this->args['description']) ? '<p class="description" id="'.\esc_attr($this->getIdString()).'-desscription">'.$this->args['description'].'</p>' : false;
    }

    public function getOptions()
    {
        return empty($this->args['choices']) ? [] : $this->args['choices'];
    }

    protected function getTranslateLink()
    {
        if (!\gdprConsentPluginIsProEdition()) {
            return null;
        }

        if (!function_exists('icl_st_is_registered_string')) {
            return null;
        }

        $registered_string = \icl_st_is_registered_string('admin_texts_'.$this->getSettingsKey(), '['.$this->getSettingsKey().']'.$this->args['name']);

        if (!$registered_string) {
            return null;
        }

        $url = \admin_url('admin.php?page='.WPML_ST_FOLDER.'/menu/string-translation.php&context=admin_texts_'.\esc_html($this->getSettingsKey()).'&search='.\urlencode($this->getValue()));
        return '<a href="'.\esc_attr($url).'" class="translate button" target="_blank">Translate</a>';
    }
}