ReviewLogic.php
259 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Wpai\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 $imports = 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 Imports page. |
| 36 | if($_GET['page'] !== 'pmxi-admin-manage' || isset($_GET['id']) ){ |
| 37 | return false; |
| 38 | } |
| 39 | |
| 40 | if($this->hasMoreThanMaxModalsDismissed()) { |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | if(!$this->hasImportsThatMatch()) { |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | if($this->thereWasAModalInTheLast30Days()) { |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | // Determine version of WP All Import running |
| 53 | if(defined('PMXI_EDITION') && PMXI_EDITION === 'free') { |
| 54 | $wpaiVersion = 'WP All Import'; |
| 55 | }else{ |
| 56 | $wpaiVersion = 'WP All Import Pro'; |
| 57 | } |
| 58 | |
| 59 | $modalToShow = $this->getModalToShow(); |
| 60 | |
| 61 | $this->modalType = $modalToShow; |
| 62 | |
| 63 | if($modalToShow == 'products') { |
| 64 | $this->pluginName = 'the WooCommerce Product Import Add-On'; |
| 65 | $this->pluginReviewLink = 'https://wordpress.org/plugins/woocommerce-xml-csv-product-import/#reviews'; |
| 66 | $this->pluginModalText ='How was your experience importing WooCommerce products with '.$wpaiVersion.'?'; |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | if($modalToShow === 'wpai') { |
| 71 | |
| 72 | $this->pluginName = 'WP All Import'; |
| 73 | $this->pluginReviewLink = 'https://wordpress.org/plugins/wp-all-import/#reviews'; |
| 74 | $this->pluginModalText = 'How was your experience importing records with '.$wpaiVersion.'?'; |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | public function dismissNotice() |
| 83 | { |
| 84 | if (current_user_can(\PMXI_Plugin::$capabilities)) { |
| 85 | update_option('wpai_modal_review_dismissed', true, false); |
| 86 | update_option('wpai_modal_review_dismissed_time', time(), false); |
| 87 | |
| 88 | $dismissedModals = get_option('wpai_modal_review_dismissed_modals', []); |
| 89 | |
| 90 | $dismissModalType = esc_html($_POST['modal_type']); |
| 91 | |
| 92 | if(!is_array($dismissedModals)) { |
| 93 | $dismissedModals = []; |
| 94 | } |
| 95 | |
| 96 | $dismissedModals[] = $dismissModalType; |
| 97 | update_option('wpai_modal_review_dismissed_modals', $dismissedModals); |
| 98 | |
| 99 | $dismissedTimes = get_option('wpai_modal_review_dismissed_times', 0); |
| 100 | $dismissedTimes++; |
| 101 | |
| 102 | update_option('wpai_modal_review_dismissed_times', $dismissedTimes, false); |
| 103 | |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | public function submitFeedback() |
| 108 | { |
| 109 | |
| 110 | $headers = ['Content-Type: text/html; charset=UTF-8']; |
| 111 | |
| 112 | $this->dismissNotice(); |
| 113 | |
| 114 | $proInUse = ''; |
| 115 | |
| 116 | // Check if WP All Import Pro is installed |
| 117 | if( defined('PMXI_EDITION') && PMXI_EDITION === 'paid' ){ |
| 118 | $proInUse .= 'Installed Pro Plugin: WP All Import Pro <br/><br/>'; |
| 119 | } |
| 120 | |
| 121 | // Check if the WooCommerce Import Add-On is installed |
| 122 | if( defined('PMWI_EDITION') and PMWI_EDITION == "paid" ){ |
| 123 | $proInUse .= 'Installed Pro Plugin: WooCommerce Import Add-On Pro <br/><br/>'; |
| 124 | } |
| 125 | |
| 126 | // Prettify the reviewed plugin. |
| 127 | $plugin = 'Plugin Reviewed: '; |
| 128 | switch( $_POST['plugin'] ){ |
| 129 | case 'wpai': |
| 130 | $plugin .= 'WP All Import'; |
| 131 | break; |
| 132 | |
| 133 | case 'products': |
| 134 | $plugin .= 'Product Import Add-On'; |
| 135 | break; |
| 136 | } |
| 137 | |
| 138 | $message = $plugin . " <br/><br/>" . $proInUse . wp_kses_post(stripslashes(wpautop($_POST['message']))); |
| 139 | wp_mail( self::MAILTO, self::SUBJECT, $message, $headers ); |
| 140 | } |
| 141 | |
| 142 | |
| 143 | public function getPluginName() { |
| 144 | return $this->pluginName; |
| 145 | } |
| 146 | |
| 147 | public function getReviewLink() { |
| 148 | return $this->pluginReviewLink; |
| 149 | } |
| 150 | |
| 151 | public function getModalType() { |
| 152 | return $this->modalType; |
| 153 | } |
| 154 | |
| 155 | public function getModalText() { |
| 156 | return $this->pluginModalText; |
| 157 | } |
| 158 | |
| 159 | private function getModalToShow() |
| 160 | { |
| 161 | $importCount = [ |
| 162 | 'products' => 0 |
| 163 | ]; |
| 164 | |
| 165 | // Only show modal for import types that have been on the site for at least two days. |
| 166 | $importOlderThanTwoDays = [ |
| 167 | 'products' => false |
| 168 | ]; |
| 169 | |
| 170 | $imports = $this->getImports(); |
| 171 | |
| 172 | // Go through the imports and find the import count for each import type |
| 173 | foreach($imports as $import) { |
| 174 | $options = \pmxi_maybe_unserialize($import->options); |
| 175 | |
| 176 | if ($options) { |
| 177 | |
| 178 | $custom_type = $options['custom_type']; |
| 179 | |
| 180 | if (!is_array($custom_type)) { |
| 181 | $custom_type = [$custom_type]; |
| 182 | } |
| 183 | |
| 184 | // Is product import |
| 185 | if (in_array('product', $custom_type)) { |
| 186 | $importCount['products']++; |
| 187 | if( strtotime($import->first_import) < time() - 2 * 24 * 3600 ){ |
| 188 | $importOlderThanTwoDays['products'] = true; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | // Get the plugin with most imports |
| 196 | $max = 0; |
| 197 | $plugin = false; |
| 198 | |
| 199 | $dismissedModals = get_option('wpai_modal_review_dismissed_modals', []); |
| 200 | |
| 201 | foreach($importCount as $key => $imports) { |
| 202 | if($imports > $max && !in_array($key, $dismissedModals) && $importOlderThanTwoDays[$key]) { |
| 203 | $plugin = $key; |
| 204 | $max = $imports; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | if(!$plugin && !in_array('wpai', $dismissedModals)) { |
| 209 | $plugin = 'wpai'; |
| 210 | } |
| 211 | |
| 212 | return $plugin; |
| 213 | } |
| 214 | |
| 215 | |
| 216 | private function thereWasAModalInTheLast30Days() |
| 217 | { |
| 218 | $lastModalDismissed = get_option('wpai_modal_review_dismissed_time'); |
| 219 | |
| 220 | if( $lastModalDismissed > time() - 30 * 24 * 3600 ) { |
| 221 | |
| 222 | return true; |
| 223 | } |
| 224 | |
| 225 | return false; |
| 226 | } |
| 227 | |
| 228 | private function hasImportsThatMatch(){ |
| 229 | |
| 230 | $importsOlderThan48Hours = $this->wpdb->get_results("SELECT * FROM " . $this->wpdb->prefix . "pmxi_imports WHERE first_import < NOW() - INTERVAL 2 DAY AND first_import <> '0000-00-00 00:00:00' "); |
| 231 | |
| 232 | $imports = $this->getImports(); |
| 233 | |
| 234 | return (count($importsOlderThan48Hours) >= 1 && count($imports) >= 5 ); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * @return imports[] |
| 239 | */ |
| 240 | private function getImports() |
| 241 | { |
| 242 | if (!$this->imports) { |
| 243 | $this->imports = $this->wpdb->get_results("SELECT * FROM " . $this->wpdb->prefix . "pmxi_imports"); |
| 244 | } |
| 245 | |
| 246 | return $this->imports; |
| 247 | } |
| 248 | |
| 249 | private function hasMoreThanMaxModalsDismissed() |
| 250 | { |
| 251 | $dismissedTimes = get_option('wpai_modal_review_dismissed_times', 0); |
| 252 | |
| 253 | if($dismissedTimes > 1) { |
| 254 | return true; |
| 255 | } |
| 256 | |
| 257 | return false; |
| 258 | } |
| 259 | } |