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