| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace GeminiLabs\SiteReviews\Controllers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use GeminiLabs\SiteReviews\Database; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use GeminiLabs\SiteReviews\Database\OptionManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use GeminiLabs\SiteReviews\Helper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use GeminiLabs\SiteReviews\Helpers\Arr; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use GeminiLabs\SiteReviews\Modules\Html\Builder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use GeminiLabs\SiteReviews\Modules\Migrate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use GeminiLabs\SiteReviews\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class NoticeController extends Controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     const USER_META_KEY = '_glsr_notices'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     protected $dismissValuesMap; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     public function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $this->dismissValuesMap = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             'addons' => glsr()->version('major'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             'welcome' => glsr()->version('minor'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @filter admin_notices | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public function adminNotices() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         // order is intentional! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $this->renderAddonsNotice(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->renderWelcomeNotice(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->renderMigrationNotice(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @action site-reviews/route/admin/dismiss-notice | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     public function dismissNotice(Request $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         if ($request->notice) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             $this->setUserMeta($request->notice, $this->getVersionFor($request->notice)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @action site-reviews/route/ajax/dismiss-notice | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     public function dismissNoticeAjax(Request $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $this->dismissNotice($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         wp_send_json_success(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @param mixed $fallback | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     protected function getUserMeta($key, $fallback) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $meta = get_user_meta(get_current_user_id(), static::USER_META_KEY, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         return Arr::get($meta, $key, $fallback); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @param string $noticeKey | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     protected function getVersionFor($noticeKey) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         return Arr::get($this->dismissValuesMap, $noticeKey, glsr()->version('major')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @return bool | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 85 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |     protected function isCurrentScreen() | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |         $screen = glsr_current_screen(); | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |         $screenIds = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |             'dashboard', | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |         return glsr()->post_type == $screen->post_type || in_array($screen->id, $screenIds); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     protected function renderAddonsNotice() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         if ($this->isCurrentScreen() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             && Helper::isGreaterThan($this->getVersionFor('addons'), $this->getUserMeta('addons', 0)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             && glsr()->can('edit_others_posts')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             glsr()->render('partials/notices/addons'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     protected function renderMigrationNotice() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         if ($this->isCurrentScreen() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             && glsr()->hasPermission('tools', 'general') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             && (glsr(Migrate::class)->isMigrationNeeded() || glsr(Database::class)->isMigrationNeeded())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             glsr()->render('partials/notices/migrate', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                 'action' => glsr(Builder::class)->a([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                     'href' => admin_url('edit.php?post_type='.glsr()->post_type.'&page=tools#tab-general'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                     'text' => _x('Import Third Party Reviews', 'admin-text', 'site-reviews'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                 ]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     protected function renderWelcomeNotice() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         if ($this->isCurrentScreen() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             && Helper::isGreaterThan($this->getVersionFor('welcome'), $this->getUserMeta('welcome', 0)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             && glsr()->can('edit_others_posts')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             $welcomeText = '0.0.0' == glsr(OptionManager::class)->get('version_upgraded_from') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                 ? _x('Thanks for installing Site Reviews v%s, we hope you love it!', 'admin-text', 'site-reviews') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                 : _x('Thanks for updating to Site Reviews v%s, we hope you love the changes!', 'admin-text', 'site-reviews'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             glsr()->render('partials/notices/welcome', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                 'text' => sprintf($welcomeText, glsr()->version), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * @param mixed $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     protected function setUserMeta($key, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         $userId = get_current_user_id(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         $meta = (array) get_user_meta($userId, static::USER_META_KEY, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         $meta = array_filter(wp_parse_args($meta, [])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         $meta[$key] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         update_user_meta($userId, static::USER_META_KEY, $meta); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 154 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 155 |  |  |  |