Поддержка Плагины Hide Protected Content

  • Помогите вытащить функции из плагина. Он скрывает контент от пользователей в зависимости от количества очков. Срабатывает в контенте по тегам:

    [private points='200']Скрытый текст[else]Чтобы увидеть скрытый текст, вам необходимо иметь 200 очков рейтинга. [/private]

    Нужно закрыть произвольное поле в шаблоне.

    <?php
        class SjContentHider
        {
            public static function instance()
            {
                static $self = false;
                if (false === $self) {
                    $self = new SjContentHider();
                }
    
                return $self;
            }
    
            public function __construct()
            {
                add_action('init', array(&$this, 'init'));
            }
    
            public function init()
            {
                global $cp_modules;
    
                if (true == function_exists('cp_getPoints')) {
                    add_filter('the_content', array(&$this, 'the_content'));
                    $cp_modules[] = array (
                        name        => 'Content Protection',
                        version     => '0.1',
                        url         => 'http:',
                        description => 'Content protection plugin',
                        api_version => '1.0',
                        author      => '',
                        author_url  => 'http:'
                    );
                }
    
                if (false == current_user_can('edit_posts') && false == current_user_can('edit_pages')) {
                    return;
                }
    
                add_filter('mce_external_plugins', array(&$this, 'mce_plugins'));
                add_filter('mce_buttons_2',        array(&$this, 'mce_buttons'));
            }
    
            public function mce_plugins($plugin_array)
            {
                $plugin_array["sjcontenthider"] = plugins_url('hider/editor_plugin.js');
                return $plugin_array;
            }
    
            public function mce_buttons($buttons)
            {
                array_push($buttons, "separator", "sjcontenthider");
                return $buttons;
            }
    
            public function the_content($s)
            {
                return preg_replace_callback('!\\[private\\s+points=([\'"])([^\'"]*)\\1\\](.*?)(?:\\[else\\](.*?))?\\[/private\\]!is', array(&$this, 'replace'), $s);
            }
    
            public function replace(&$matches)
            {
                global $post;
                $current_user = $GLOBALS['current_user'];
                $points       = (int)$matches[2];
    
                if (false == isset($matches[4])) {
                    $matches[4] = '';
                }
    
                if (true == empty($current_user) || 0 == $current_user->ID) {
                    return $matches[4];
                }
    
                if ($points <= 0 || $current_user->has_cap('administrator') || $post->post_author == $current_user->ID) {
                    return $matches[3];
                }
    
                $user_points = cp_getPoints($current_user->ID);
                if ($user_points >= $points) {
                    return $matches[3];
                }
    
                return $matches[4];
            }
        }
    
        SjContentHider::instance();
    ?>
Просмотр 1 ответа (всего 1)
  • Atrax

    (@atrax)

    Ethical engineer

    Это объект.
    Просто так не выдерешь.
    Переписывать придется.

    Кстати, хороший стиль проектирования.

Просмотр 1 ответа (всего 1)
  • Тема «Hide Protected Content» закрыта для новых ответов.