Untitled

 avatar
unknown
php
3 years ago
6.1 kB
5
Indexable
<?php

namespace DevOwl\RealCategoryLibrary\Vendor\DevOwl\RealProductManagerWpClient\view;

use DevOwl\RealCategoryLibrary\Vendor\DevOwl\RealProductManagerWpClient\Core;
use DevOwl\RealCategoryLibrary\Vendor\DevOwl\RealProductManagerWpClient\PluginUpdate;
use DevOwl\RealCategoryLibrary\Vendor\DevOwl\RealProductManagerWpClient\UtilsProvider;
use DevOwl\RealCategoryLibrary\Vendor\MatthiasWeb\Utils\Service;
// @codeCoverageIgnoreStart
\defined('ABSPATH') or die('No script kiddies please!');
// Avoid direct file request
// @codeCoverageIgnoreEnd
/**
 * Plugin update view handling. E. g. add links to the plugins row in plugins list.
 */
class PluginUpdateView {
    use UtilsProvider;
    const OPTION_NAME_ADMIN_NOTICE_LICENSE_DISMISSED_DAY_PREFIX =
        RPM_WP_CLIENT_OPT_PREFIX . '-noticeLicenseDismissDay_';
    const CLICK_HANDLER_ATTRIBUTE = 'data-rpm-wp-client-plugin-update';
    const CLICK_NOTICE_LICENSE_DISMISS_HANDLER_ATTRIBUTE = 'data-rpm-wp-client-plugin-update-license-notice-dismiss';
    const HASH_HANDLER_PREFIX = 'rpm-wp-client-plugin-update-';
    /**
     * Plugin update instance.
     *
     * @var PluginUpdate
     */
    private $pluginUpdate;
    /**
     * C'tor.
     *
     * @param PluginUpdate $pluginUpdate
     * @codeCoverageIgnore
     */
    private function __construct($pluginUpdate) {
        $this->pluginUpdate = $pluginUpdate;
    }
    /**
     * Show a notice when the plugin is not fully licensed.
     */
    public function admin_notices_not_licensed() {
       
    }
    /**
     * Show the notice of the license hint, if a license got disabled e.g. through remote or a new hostname (migration).
     */
    public function admin_notices_license_hint() {
        
    }
    /**
     * Show a notice in the plugins list that the plugin is not activated, yet.
     */
    public function after_plugin_row() {
        
    }
    /**
     * Generate a `<a` link to open the license form for a given plugin.
     *
     * @param string $text
     */
    public function generateLicenseLink($text) {
        return \sprintf(
            '<a href="#" %s="%s">%s</a>',
            self::CLICK_HANDLER_ATTRIBUTE,
            $this->getPluginUpdate()
                ->getInitiator()
                ->getPluginSlug(),
            $text
        );
    }
    /**
     * Add a "Enter License" link to the plugins actions.
     *
     * @param array $actions The plugin actions
     * @return array The updated actions.
     */
    public function plugin_action_links($actions) {
        // Free version should never show a license button in plugins list
        if (
            !$this->getPluginUpdate()
                ->getInitiator()
                ->isExternalUpdateEnabled()
        ) {
            return $actions;
        }
        $link = $this->generateLicenseLink(__('License', RPM_WP_CLIENT_TD));
        $actions[RPM_WP_CLIENT_SLUG] = $this->getPluginUpdate()->isLicensed()
            ? $link
            : \sprintf('<span style="font-weight:bold;">%s</span>', $link);
        return $actions;
    }
    /**
     * Dismiss the license admin notice for the current day.
     */
    public function dismissLicenseAdminNotice() {
        $day = $this->getPluginUpdate()->getDaysSinceFirstInitialization();
        return update_option(
            self::OPTION_NAME_ADMIN_NOTICE_LICENSE_DISMISSED_DAY_PREFIX .
                $this->getPluginUpdate()
                    ->getInitiator()
                    ->getPluginSlug(),
            $day
        );
    }
    /**
     * Get the admin notice text for not-fully licensed plugins. Returns `false` if the admin
     * notice should not be shown. Returns `[true, 'text']` if a notice should be shown and is dismissible.
     * Returns `[false, 'text']` for a permanent notice.
     *
     * @param boolean $allowFirstDays If `true`, the first and second day also shows a notice. Useful for `update-core` page
     */
    protected function getAdminNoticeLicenseText($allowFirstDays = \false) {
      
    }
    /**
     * Get the "Activate now" link for the current plugin.
     *
     * @param boolean $onlyHref
     */
    public function getActivateLink($onlyHref = \false) {
        $initiator = $this->getPluginUpdate()->getInitiator();
        $href = admin_url('plugins.php#' . self::HASH_HANDLER_PREFIX . $initiator->getPluginSlug());
        return $onlyHref
            ? $href
            : \sprintf(
                // Force to reload the page so the hash works always (also in Plugins list)
                '<a href="%1$s" onclick="window.location.href=\'%1$s\';window.location.reload();">%2$s</a>',
                $href,
                __('Activate now', RPM_WP_CLIENT_TD)
            );
    }
    /**
     * Dismiss-functionality is handled through a inline-onclick handler because we
     * do not need to enqueue an extra script on each page.
     *
     * @param string $slug
     */
    protected function dismissOnClickHandler($slug) {
        return \join('', [
            'jQuery(this).parents(".notice").remove();',
            \sprintf(
                'window.fetch("%s");',
                add_query_arg(
                    ['_method' => 'DELETE', '_wpnonce' => wp_create_nonce('wp_rest')],
                    \sprintf(
                        '%splugin-update/%s/license-notice',
                        \DevOwl\RealCategoryLibrary\Vendor\MatthiasWeb\Utils\Service::getUrl(
                            \DevOwl\RealCategoryLibrary\Vendor\DevOwl\RealProductManagerWpClient\Core::getInstance()
                        ),
                        $slug
                    )
                )
            )
        ]);
    }
    /**
     * Get plugin update instance.
     *
     * @codeCoverageIgnore
     */
    public function getPluginUpdate() {
        return $this->pluginUpdate;
    }
    /**
     * New instance.
     *
     * @param PluginUpdate $pluginUpdate
     * @codeCoverageIgnore
     */
    public static function instance($pluginUpdate) {
        return new \DevOwl\RealCategoryLibrary\Vendor\DevOwl\RealProductManagerWpClient\view\PluginUpdateView(
            $pluginUpdate
        );
    }
}
Editor is loading...