<?php
// 付费隐藏内容
if (!class_exists('PPO_Hide')) {
    class PPO_Hide {

        public function __construct(){
            add_shortcode('ppo_hide', array($this, 'ppo_hide_shortcode'));      
            add_filter('the_content', array($this, 'ppo_content_remove_contenteditable'));           
        }

        public static function hide_box($content){

            global $post; 
            $pid = $post->ID;
            $current_user = wp_get_current_user();
            $user_id = $current_user->ID;
            $html = '';
            $meta = get_post_meta($pid,'_ppo_post_options',true);
        
            //return $content;
            if(is_array($meta) && !empty($meta)) {
                $dd_type = $meta['dd_type'];

                if($dd_type == 'read'){
                    $data = $meta['dd_box_data'];
                    $data['pid'] = $pid;
                    $price = PPO_Down::get_down_price($data,$user_id);
                    $bought = PPO_Down::check_user_bought($data);
                    $hide_img = THEME_URL.'/img/icon/lock.png';
    
                    $html = '<div class="ppo-hide-box">
                                <div class="hide-inner">
                                <div class="hide_img"><img src="'.$hide_img.'"></div>
                                    <div class="left-info">
                                        <div class="title"><i class="ri-eye-close-line"></i>隐藏内容</div>
                                        '.$price.'
                                    </div>
                                    <div class="right-btn">'.self::hide_box_btn($data).'</div>
                                </div>
                            </div>';
    
                    $show = '<div class="ppo-show-box">
                                <div class="show-tips"><i class="ri-lock-unlock-line"></i>内容已解锁</div>
                                <div class="show-mce-content"><div class="show-inner">'.do_shortcode($content).'</div></div>
                            </div>'; 
    
                    if($bought) {
                        return $show;
                    } else {
                        return $html;
                    }
                }

            }

        }

        // 按钮
        public static function hide_box_btn($data){

            $type = $data['pay_type'];

            switch ($type) {
                case 'cash':
                case 'credit':    
                    $btn = '<a class="dd-buy-now"><i class="ri-shopping-bag-3-line"></i>立即购买</a>';
                    break;
                
                case 'login': 
                    $btn = '<a class="dd-buy-login need-login"><i class="ri-user-follow-line"></i>登录查看</a>';
                    break;

               case 'cmt': 
                    $btn = '<a href="#comments" class="dd-buy-cmt" uk-scroll><i class="ri-message-3-line"></i>评论查看</a>';
                    break;  

                case 'limits': 
                    $btn = '<a href="'.home_url().'/vip" target="_blank" class="dd-up-vip"><i class="ri-vip-crown-2-line"></i>立即升级</a>';
                    break;  
                    
                case 'pwd': 
                    $btn = '<a class="dd-buy-pwd"><i class="ri-lock-line"></i>密码查看</a>';
                    break;      
            }

            return '<div class="dd-buy-btn">'.$btn.'</div>';

        }


        // 注册短代码
        public static function ppo_hide_shortcode($atts, $content = null) {
            
            //$content = preg_replace('/<div class="qt-hide">(.*?)<\/div>/', '$1', $content);
            return self::hide_box($content);
        }

        
        function ppo_content_remove_contenteditable($content)
        {
            $content = str_replace(' contenteditable="', ' mce-contenteditable="', $content);
            return $content;
        }



    }
}

new PPO_Hide;