ReviewLogic.php
301 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Wpae\Reviews; |
| 4 | |
| 5 | |
| 6 | class ReviewLogic |
| 7 | { |
| 8 | const MAILTO = 'support@wpallimport.com'; |
| 9 | const SUBJECT = 'New Feedback'; |
| 10 | |
| 11 | private $wpdb; |
| 12 | |
| 13 | private $exports = false; |
| 14 | |
| 15 | private $pluginName = ''; |
| 16 | |
| 17 | private $pluginReviewLink = ''; |
| 18 | |
| 19 | private $modalType; |
| 20 | |
| 21 | private $pluginModalText; |
| 22 | |
| 23 | public function __construct() |
| 24 | { |
| 25 | global $wpdb; |
| 26 | |
| 27 | $this->wpdb = $wpdb; |
| 28 | } |
| 29 | |
| 30 | |
| 31 | |
| 32 | public function shouldShowReviewModal() |
| 33 | { |
| 34 | |
| 35 | // Only display on the Manage Exports page. |
| 36 | if($_GET['page'] !== 'pmxe-admin-manage' || isset($_GET['id']) ){ |
| 37 | return false; |
| 38 | } |
| 39 | |
| 40 | if($this->hasMoreThan4ModalsDismissed()) { |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | if(!$this->hasExportsThatMatch()) { |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | if($this->thereWasAModalInTheLast30Days()) { |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | $modalToShow = $this->getModalToShow(); |
| 53 | |
| 54 | $this->modalType = $modalToShow; |
| 55 | |
| 56 | if($modalToShow == 'orders') { |
| 57 | $this->pluginName = 'WooCommerce Order Export Add-On'; |
| 58 | $this->pluginReviewLink = 'https://wordpress.org/plugins/order-export-for-woocommerce/#reviews'; |
| 59 | $this->pluginModalText ='How was your experience exporting WooCommerce orders with WP All Export?'; |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | if($modalToShow == 'users') { |
| 64 | $this->pluginName = 'User Export Add-On'; |
| 65 | $this->pluginReviewLink = 'https://wordpress.org/plugins/export-wp-users-xml-csv/#reviews'; |
| 66 | $this->pluginModalText ='How was your experience exporting users with WP All Export?'; |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | if($modalToShow == 'products') { |
| 71 | $this->pluginName = 'WooCommerce Product Export Add-On'; |
| 72 | $this->pluginReviewLink = 'https://wordpress.org/plugins/product-export-for-woocommerce/#reviews'; |
| 73 | $this->pluginModalText ='How was your experience exporting WooCommerce products with WP All Export?'; |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | if($modalToShow === 'wpae') { |
| 78 | |
| 79 | if(defined('PMXE_EDITION') && PMXE_EDITION === 'free') { |
| 80 | $this->pluginName = 'WP All Export'; |
| 81 | }else{ |
| 82 | $this->pluginName = 'WP All Export Pro'; |
| 83 | } |
| 84 | $this->pluginReviewLink = 'https://wordpress.org/plugins/wp-all-export/#reviews'; |
| 85 | $this->pluginModalText = 'How was your experience exporting records with WP All Export?'; |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | public function dismissNotice() |
| 94 | { |
| 95 | if (current_user_can('manage_options')) { |
| 96 | update_option('wpae_modal_review_dismissed', true, false); |
| 97 | update_option('wpae_modal_review_dismissed_time', time(), false); |
| 98 | |
| 99 | $dismissedModals = get_option('wpae_modal_review_dismissed_modals', []); |
| 100 | |
| 101 | $dismissModalType = esc_html($_POST['modal_type']); |
| 102 | |
| 103 | if(!is_array($dismissedModals)) { |
| 104 | $dismissedModals = []; |
| 105 | } |
| 106 | |
| 107 | $dismissedModals[] = $dismissModalType; |
| 108 | update_option('wpae_modal_review_dismissed_modals', $dismissedModals); |
| 109 | |
| 110 | $dismissedTimes = get_option('wpae_modal_review_dismissed_times', 0); |
| 111 | $dismissedTimes++; |
| 112 | |
| 113 | update_option('wpae_modal_review_dismissed_times', $dismissedTimes, false); |
| 114 | |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | public function submitFeedback() |
| 119 | { |
| 120 | |
| 121 | $headers = ['Content-Type: text/html; charset=UTF-8']; |
| 122 | |
| 123 | $this->dismissNotice(); |
| 124 | |
| 125 | $proInUse = ''; |
| 126 | |
| 127 | // Check if WP All Export Pro is installed |
| 128 | if( defined('PMXE_EDITION') && PMXE_EDITION === 'paid' ){ |
| 129 | $proInUse .= 'Installed Pro Plugin: WP All Export Pro <br/><br/>'; |
| 130 | } |
| 131 | |
| 132 | // Check if the WooCommerce Export Add-On is installed |
| 133 | if( class_exists('PMWE_Plugin') and PMWE_EDITION == "paid" ){ |
| 134 | $proInUse .= 'Installed Pro Plugin: WooCommerce Export Add-On Pro <br/><br/>'; |
| 135 | } |
| 136 | |
| 137 | // Check if the User Export Add-On is installed. |
| 138 | if ( class_exists('PMUE_Plugin') and PMUE_EDITION == "paid"){ |
| 139 | $proInUse .= 'Installed Pro Plugin: User Export Add-On Pro <br/><br/>'; |
| 140 | } |
| 141 | |
| 142 | // Prettify the reviewed plugin. |
| 143 | $plugin = 'Plugin Reviewed: '; |
| 144 | switch( $_POST['plugin'] ){ |
| 145 | case 'wpae': |
| 146 | $plugin .= 'WP All Export'; |
| 147 | break; |
| 148 | case 'orders': |
| 149 | $plugin .= 'Order Export Add-On'; |
| 150 | break; |
| 151 | |
| 152 | case 'users': |
| 153 | $plugin .= 'User Export Add-On'; |
| 154 | break; |
| 155 | |
| 156 | case 'products': |
| 157 | $plugin .= 'Product Export Add-On'; |
| 158 | break; |
| 159 | } |
| 160 | |
| 161 | $message = $plugin . " <br/><br/>" . $proInUse . wp_kses_post(stripslashes(wpautop($_POST['message']))); |
| 162 | wp_mail( self::MAILTO, self::SUBJECT, $message, $headers ); |
| 163 | } |
| 164 | |
| 165 | |
| 166 | public function getPluginName() { |
| 167 | return $this->pluginName; |
| 168 | } |
| 169 | |
| 170 | public function getReviewLink() { |
| 171 | return $this->pluginReviewLink; |
| 172 | } |
| 173 | |
| 174 | public function getModalType() { |
| 175 | return $this->modalType; |
| 176 | } |
| 177 | |
| 178 | public function getModalText() { |
| 179 | return $this->pluginModalText; |
| 180 | } |
| 181 | |
| 182 | private function getModalToShow() |
| 183 | { |
| 184 | $exportCount = [ |
| 185 | 'users' => 0, |
| 186 | 'products' => 0, |
| 187 | 'orders' => 0 |
| 188 | ]; |
| 189 | |
| 190 | // Only show modal for export types that have been on the site for at least two days. |
| 191 | $exportOlderThanTwoDays = [ |
| 192 | 'users' => false, |
| 193 | 'products' => false, |
| 194 | 'orders' => false |
| 195 | ]; |
| 196 | |
| 197 | $exports = $this->getExports(); |
| 198 | |
| 199 | // Go through the exports and find the export count for each export type |
| 200 | foreach($exports as $export) { |
| 201 | $options = maybe_unserialize($export->options); |
| 202 | |
| 203 | if ($options) { |
| 204 | |
| 205 | $cpt = $options['cpt']; |
| 206 | |
| 207 | if (!is_array($cpt)) { |
| 208 | $cpt = [$cpt]; |
| 209 | } |
| 210 | |
| 211 | // Is user export |
| 212 | if (in_array('users', $cpt) || in_array('shop_customer', $cpt)) { |
| 213 | $exportCount['users']++; |
| 214 | if( strtotime($export->created_at) < time() - 2 * 24 * 3600 ){ |
| 215 | $exportOlderThanTwoDays['users'] = true; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | // Is product export |
| 220 | if (in_array('product', $cpt)) { |
| 221 | $exportCount['products']++; |
| 222 | if( strtotime($export->created_at) < time() - 2 * 24 * 3600 ){ |
| 223 | $exportOlderThanTwoDays['products'] = true; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | // Is order export |
| 228 | if (in_array('shop_order', $cpt)) { |
| 229 | $exportCount['orders']++; |
| 230 | if( strtotime($export->created_at) < time() - 2 * 24 * 3600 ){ |
| 231 | $exportOlderThanTwoDays['orders'] = true; |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | // Get the plugin with most exports |
| 238 | $max = 0; |
| 239 | $plugin = false; |
| 240 | |
| 241 | $dismissedModals = get_option('wpae_modal_review_dismissed_modals', []); |
| 242 | |
| 243 | foreach($exportCount as $key => $exports) { |
| 244 | if($exports > $max && !in_array($key, $dismissedModals) && $exportOlderThanTwoDays[$key]) { |
| 245 | $plugin = $key; |
| 246 | $max = $exports; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | if(!$plugin && !in_array('wpae', $dismissedModals)) { |
| 251 | $plugin = 'wpae'; |
| 252 | } |
| 253 | |
| 254 | return $plugin; |
| 255 | } |
| 256 | |
| 257 | |
| 258 | private function thereWasAModalInTheLast30Days() |
| 259 | { |
| 260 | $lastModalDismissed = get_option('wpae_modal_review_dismissed_time'); |
| 261 | |
| 262 | if( $lastModalDismissed > time() - 30 * 24 * 3600 ) { |
| 263 | |
| 264 | return true; |
| 265 | } |
| 266 | |
| 267 | return false; |
| 268 | } |
| 269 | |
| 270 | private function hasExportsThatMatch(){ |
| 271 | |
| 272 | $exportsOlderThan48Hours = $this->wpdb->get_results("SELECT * FROM " . $this->wpdb->prefix . "pmxe_exports WHERE created_at < NOW() - INTERVAL 2 DAY AND created_at <> '0000-00-00 00:00:00' "); |
| 273 | |
| 274 | $exports = $this->getExports(); |
| 275 | |
| 276 | return (count($exportsOlderThan48Hours) >= 1 && count($exports) >= 5 ); |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * @return exports[] |
| 281 | */ |
| 282 | private function getExports() |
| 283 | { |
| 284 | if (!$this->exports) { |
| 285 | $this->exports = $this->wpdb->get_results("SELECT * FROM " . $this->wpdb->prefix . "pmxe_exports"); |
| 286 | } |
| 287 | |
| 288 | return $this->exports; |
| 289 | } |
| 290 | |
| 291 | private function hasMoreThan4ModalsDismissed() |
| 292 | { |
| 293 | $dismissedTimes = get_option('wpae_modal_review_dismissed_times', 0); |
| 294 | |
| 295 | if($dismissedTimes > 4) { |
| 296 | return true; |
| 297 | } |
| 298 | |
| 299 | return false; |
| 300 | } |
| 301 | } |