| 1 |  |  | <?php namespace EmailLog\Core\DB; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Handle installation and db table creation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use EmailLog\Core\Loadie; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use EmailLog\Util; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | defined( 'ABSPATH' ) || exit; // Exit if accessed directly. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * Helper class to create table. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @since 2.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class TableManager implements Loadie { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	/* Database table name */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	const LOG_TABLE_NAME = 'email_log'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	/* Database option name */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	const DB_OPTION_NAME = 'email-log-db'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	/* Database version */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	const DB_VERSION = '0.3'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	 * Setup hooks. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 29 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  | 	public function load() { | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  | 		add_action( 'wpmu_new_blog', array( $this, 'create_table_for_new_blog' ) ); | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  | 		add_filter( 'wpmu_drop_tables', array( $this, 'delete_table_from_deleted_blog' ) ); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  | 		// Do any DB upgrades. | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  | 		$this->update_table_if_needed(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	 * On plugin activation, create table if needed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	 * @param bool $network_wide True if the plugin was network activated. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	public function on_activate( $network_wide ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		if ( is_multisite() && $network_wide ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 			// Note: if there are more than 10,000 blogs or | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 			// if `wp_is_large_network` filter is set, then this may fail. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 			$sites = get_sites(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 			foreach ( $sites as $site ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 				switch_to_blog( $site->blog_id ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 				$this->create_table_if_needed(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 				restore_current_blog(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 			$this->create_table_if_needed(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	 * Create email log table when a new blog is created. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	 * @param int $blog_id Blog Id. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	public function create_table_for_new_blog( $blog_id ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		if ( is_plugin_active_for_network( 'email-log/email-log.php' ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 			switch_to_blog( $blog_id ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 			$this->create_table_if_needed(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 			restore_current_blog(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	 * Add email log table to the list of tables deleted when a blog is deleted. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	 * @param array $tables List of tables to be deleted. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	 * @return string[] $tables Modified list of tables to be deleted. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 1 |  | 	public function delete_table_from_deleted_blog( $tables ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 1 |  | 		$tables[] = $this->get_log_table_name(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  | 		return $tables; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	 * Get email log table name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	 * @return string Email Log Table name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 2 |  | 	public function get_log_table_name() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 2 |  | 		global $wpdb; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 2 |  | 		return $wpdb->prefix . self::LOG_TABLE_NAME; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	 * Insert log data into DB. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	 * @param array $data Data to be inserted. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 	public function insert_log( $data ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 		global $wpdb; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 		$table_name = $this->get_log_table_name(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 		$wpdb->insert( $table_name, $data ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 	 * Delete log entries by ids. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 	 * @param string $ids Comma separated list of log ids. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 	 * @return false|int Number of log entries that got deleted. False on failure. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 	public function delete_logs( $ids ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 		global $wpdb; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 		$table_name = $this->get_log_table_name(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 		// Can't use wpdb->prepare for the below query. If used it results in this bug // https://github.com/sudar/email-log/issues/13. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 		$ids = esc_sql( $ids ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 		return $wpdb->query( "DELETE FROM {$table_name} where id IN ( {$ids} )" ); //@codingStandardsIgnoreLine | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 	 * Delete all log entries. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 	 * @return false|int Number of log entries that got deleted. False on failure. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 	public function delete_all_logs() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 		global $wpdb; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 		$table_name = $this->get_log_table_name(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 		return $wpdb->query( "DELETE FROM {$table_name}" ); //@codingStandardsIgnoreLine | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 	 * Deletes Email Logs older than the specified interval. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 	 * @param int $interval_in_days No. of days beyond which logs are to be deleted. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 	 * @return int $deleted_rows_count  Count of rows deleted. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 	public function delete_logs_older_than( $interval_in_days ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 		global $wpdb; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 		$table_name = $this->get_log_table_name(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 		$query              = $wpdb->prepare( "DELETE FROM {$table_name} WHERE sent_date < DATE_SUB( CURDATE(), INTERVAL %d DAY )", $interval_in_days ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 		$deleted_rows_count = $wpdb->query( $query ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 		return $deleted_rows_count; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 	 * Fetch log item by ID. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 	 * @param array $ids             Optional. Array of IDs of the log items to be retrieved. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 	 * @param array $additional_args { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 	 *                               Optional. Array of additional args. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 	 * @type string $date_column_format MySQL date column format. Refer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 	 * @link  https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 	 * } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | 	 * @return array Log item(s). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 	public function fetch_log_items_by_id( $ids = array(), $additional_args = array() ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | 		global $wpdb; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 		$table_name = $this->get_log_table_name(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  | 		$query = "SELECT * FROM {$table_name}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  | 		// When `date_column_format` exists, should replace the `$query` var. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  | 		$date_column_format_key = 'date_column_format'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  | 		if ( array_key_exists( $date_column_format_key, $additional_args ) && ! empty( $additional_args[ $date_column_format_key ] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  | 			$query = "SELECT DATE_FORMAT(sent_date, \"{$additional_args[ $date_column_format_key ]}\") as sent_date_custom, el.* FROM {$table_name} as el"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  | 		if ( ! empty( $ids ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  | 			$ids = array_map( 'absint', $ids ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  | 			// Can't use wpdb->prepare for the below query. If used it results in this bug https://github.com/sudar/email-log/issues/13. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  | 			$ids_list = esc_sql( implode( ',', $ids ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  | 			$query .= " where id IN ( {$ids_list} )"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  | 		if ( isset( $additional_args['step'] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  | 			$step = absint( $additional_args['step'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  | 			$query .= " AND 1 = {$step}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  | 		$output = isset( $additional_args['output'] ) ? $additional_args['output'] : ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  | 		if ( OBJECT === $output ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  | 			return $wpdb->get_results( $query ); //@codingStandardsIgnoreLine | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  | 		return $wpdb->get_results( $query, 'ARRAY_A' ); //@codingStandardsIgnoreLine | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  | 	 * Fetch log items. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  | 	 * @since 2.3.0 Implemented Advanced Search. Search queries could look like the following. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  | 	 *              Example: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  | 	 *              id: 2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  | 	 *              to: [email protected] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  | 	 * @param array $request         Request object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  | 	 * @param int   $per_page        Entries per page. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  | 	 * @param int   $current_page_no Current page no. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  | 	 * @return array Log entries and total items count. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  | 	public function fetch_log_items( $request, $per_page, $current_page_no ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  | 		global $wpdb; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  | 		$table_name = $this->get_log_table_name(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  | 		$query       = 'SELECT * FROM ' . $table_name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  | 		$count_query = 'SELECT count(*) FROM ' . $table_name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  | 		$query_cond  = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  | 		if ( isset( $request['s'] ) && is_string( $request['s'] ) && $request['s'] !== '' ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  | 			$search_term = trim( esc_sql( $request['s'] ) ); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  | 			if ( Util\is_advanced_search_term( $search_term ) ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  | 				$predicates = Util\get_advanced_search_term_predicates( $search_term ); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  | 				foreach ( $predicates as $column => $email ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  | 					switch ( $column ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  | 						case 'id': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  | 							$query_cond .= empty( $query_cond ) ? ' WHERE ' : ' AND '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  | 							$query_cond .= "id = '$email'"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  | 							break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  | 						case 'to': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  | 							$query_cond .= empty( $query_cond ) ? ' WHERE ' : ' AND '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  | 							$query_cond .= "to_email LIKE '%$email%'"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  | 							break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  | 						case 'email': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  | 							$query_cond .= empty( $query_cond ) ? ' WHERE ' : ' AND '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  | 							$query_cond .= ' ( '; /* Begin 1st */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  | 							$query_cond .= " ( to_email LIKE '%$email%' OR subject LIKE '%$email%' ) "; /* Begin 2nd & End 2nd */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  | 							$query_cond .= ' OR '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  | 							$query_cond .= ' ( '; /* Begin 3rd */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  | 							$query_cond .= "headers <> ''"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  | 							$query_cond .= ' AND '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  | 							$query_cond .= ' ( '; /* Begin 4th */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  | 							$query_cond .= "headers REGEXP '[F|f]rom:.*$email' OR "; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  | 							$query_cond .= "headers REGEXP '[CC|Cc|cc]:.*$email' OR "; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  | 							$query_cond .= "headers REGEXP '[BCC|Bcc|bcc]:.*$email' OR "; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  | 							$query_cond .= "headers REGEXP '[R|r]eply-[T|t]o:.*$email'"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  | 							$query_cond .= ' ) '; /* End 4th */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  | 							$query_cond .= ' ) '; /* End 3rd */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  | 							$query_cond .= ' ) '; /* End 1st */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  | 							break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  | 						case 'cc': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  | 							$query_cond .= empty( $query_cond ) ? ' WHERE ' : ' AND '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  | 							$query_cond .= ' ( '; /* Begin 1st */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  | 							$query_cond .= "headers <> ''"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  | 							$query_cond .= ' AND '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  | 							$query_cond .= ' ( '; /* Begin 2nd */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  | 							$query_cond .= "headers REGEXP '[CC|Cc|cc]:.*$email' "; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  | 							$query_cond .= ' ) '; /* End 2nd */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  | 							$query_cond .= ' ) '; /* End 1st */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  | 							break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  | 						case 'bcc': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  | 							$query_cond .= empty( $query_cond ) ? ' WHERE ' : ' AND '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  | 							$query_cond .= ' ( '; /* Begin 1st */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  | 							$query_cond .= "headers <> ''"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  | 							$query_cond .= ' AND '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  | 							$query_cond .= ' ( '; /* Begin 2nd */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  | 							$query_cond .= "headers REGEXP '[BCC|Bcc|bcc]:.*$email' "; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  | 							$query_cond .= ' ) '; /* End 2nd */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  | 							$query_cond .= ' ) '; /* End 1st */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  | 							break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  | 						case 'reply-to': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  | 							$query_cond .= empty( $query_cond ) ? ' WHERE ' : ' AND '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  | 							$query_cond .= ' ( '; /* Begin 1st */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  | 							$query_cond .= "headers <> ''"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  | 							$query_cond .= ' AND '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  | 							$query_cond .= ' ( '; /* Begin 2nd */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  | 							$query_cond .= "headers REGEXP '[R|r]eply-to:.*$email' "; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  | 							$query_cond .= ' ) '; /* End 2nd */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  | 							$query_cond .= ' ) '; /* End 1st */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  | 							break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  | 					} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  | 			} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 |  |  | 				$query_cond .= " WHERE ( to_email LIKE '%$search_term%' OR subject LIKE '%$search_term%' ) "; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 |  |  | 		if ( isset( $request['d'] ) && $request['d'] !== '' ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 |  |  | 			$search_date = trim( esc_sql( $request['d'] ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 |  |  | 			if ( '' === $query_cond ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 |  |  | 				$query_cond .= " WHERE sent_date BETWEEN '$search_date 00:00:00' AND '$search_date 23:59:59' "; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 |  |  | 			} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 |  |  | 				$query_cond .= " AND sent_date BETWEEN '$search_date 00:00:00' AND '$search_date 23:59:59' "; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 |  |  | 		// Ordering parameters. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 |  |  | 		$orderby = ! empty( $request['orderby'] ) ? esc_sql( $request['orderby'] ) : 'sent_date'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 |  |  | 		$order   = ! empty( $request['order'] ) ? esc_sql( $request['order'] ) : 'DESC'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 |  |  | 		if ( ! empty( $orderby ) & ! empty( $order ) ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 |  |  | 			$query_cond .= ' ORDER BY ' . $orderby . ' ' . $order; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 314 |  |  | 		// Find total number of items. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 315 |  |  | 		$count_query = $count_query . $query_cond; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 316 |  |  | 		$total_items = $wpdb->get_var( $count_query ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 318 |  |  | 		// Adjust the query to take pagination into account. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 319 |  |  | 		if ( ! empty( $current_page_no ) && ! empty( $per_page ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 320 |  |  | 			$offset     = ( $current_page_no - 1 ) * $per_page; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 321 |  |  | 			$query_cond .= ' LIMIT ' . (int) $offset . ',' . (int) $per_page; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 322 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 323 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 324 |  |  | 		// Fetch the items. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 325 |  |  | 		$query = $query . $query_cond; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 326 |  |  | 		$items = $wpdb->get_results( $query ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 327 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 328 |  |  | 		return array( $items, $total_items ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 329 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 330 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 331 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 332 |  |  | 	 * Create email log table. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 333 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 334 |  |  | 	 * @global object $wpdb | 
            
                                                                                                            
                            
            
                                    
            
            
                | 335 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 336 |  |  | 	public function create_table_if_needed() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 337 |  |  | 		global $wpdb; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 338 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 339 |  |  | 		$table_name = $this->get_log_table_name(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 340 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 341 |  |  | 		if ( $wpdb->get_var( "show tables like '{$table_name}'" ) != $table_name ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 342 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 343 |  |  | 			$sql = $this->get_create_table_query(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 344 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 345 |  |  | 			require_once ABSPATH . 'wp-admin/includes/upgrade.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 346 |  |  | 			dbDelta( $sql ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 347 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 348 |  |  | 			add_option( self::DB_OPTION_NAME, self::DB_VERSION ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 349 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 350 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 351 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 352 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 353 |  |  | 	 * Get the total number of email logs. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 354 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 355 |  |  | 	 * @return int Total email log count | 
            
                                                                                                            
                            
            
                                    
            
            
                | 356 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 357 |  |  | 	public function get_logs_count() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 358 |  |  | 		global $wpdb; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 359 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 360 |  |  | 		$query = 'SELECT count(*) FROM ' . $this->get_log_table_name(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 361 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 362 |  |  | 		return $wpdb->get_var( $query ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 363 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 364 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 365 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 366 |  |  | 	 * Fetches the log id by item data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 367 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 368 |  |  | 	 * Use this method to get the log item id when the error instance only returns the log item data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 369 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 370 |  |  | 	 * @param array        $data Array of Email information { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 371 |  |  | 	 * @type  array|string to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 372 |  |  | 	 * @type  string       subject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 373 |  |  | 	 * @type  string       message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 374 |  |  | 	 * @type  array|string headers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 375 |  |  | 	 * @type  array|string attachments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 376 |  |  | 	 *                          } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 377 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 378 |  |  | 	 * @return int Log item id. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 379 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 380 |  |  | 	public function fetch_log_id_by_data( $data ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 381 |  |  | 		if ( empty( $data ) || ! is_array( $data ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 382 |  |  | 			return 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 383 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 384 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 385 |  |  | 		global $wpdb; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 386 |  |  | 		$table_name = $this->get_log_table_name(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 387 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 388 |  |  | 		$query      = "SELECT ID FROM {$table_name}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 389 |  |  | 		$query_cond = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 390 |  |  | 		$where      = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 391 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 392 |  |  | 		// Execute the following `if` conditions only when $data is array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 393 |  |  | 		if ( array_key_exists( 'to', $data ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 394 |  |  | 			// Since the value is stored as CSV in DB, convert the values from error data to CSV to compare. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 395 |  |  | 			$to_email = Util\stringify( $data['to'] ); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 396 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 397 |  |  | 			$to_email = trim( esc_sql( $to_email ) ); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 398 |  |  | 			$where[]  = "to_email = '$to_email'"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 399 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 400 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 401 |  |  | 		if ( array_key_exists( 'subject', $data ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 402 |  |  | 			$subject = trim( esc_sql( $data['subject'] ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 403 |  |  | 			$where[] = "subject = '$subject'"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 404 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 405 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 406 |  |  | 		if ( array_key_exists( 'attachments', $data ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 407 |  |  | 			if ( is_array( $data['attachments'] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 408 |  |  | 				$attachments = count( $data['attachments'] ) > 0 ? 'true' : 'false'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 409 |  |  | 			} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 410 |  |  | 				$attachments = empty( $data['attachments'] ) ? 'false' : 'true'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 411 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 412 |  |  | 			$attachments = trim( esc_sql( $attachments ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 413 |  |  | 			$where[]     = "attachments = '$attachments'"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 414 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 415 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 416 |  |  | 		foreach ( $where as $index => $value ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 417 |  |  | 			$query_cond .= 0 === $index ? ' WHERE ' : ' AND '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 418 |  |  | 			$query_cond .= $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 419 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 420 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 421 |  |  | 		// Get only the latest logged item when multiple rows match. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 422 |  |  | 		$query_cond .= ' ORDER BY id DESC LIMIT 1'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 423 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 424 |  |  | 		$query = $query . $query_cond; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 425 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 426 |  |  | 		return absint( $wpdb->get_var( $query ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 427 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 428 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 429 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 430 |  |  | 	 * Sets email sent status and error message for the given log item when email fails. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 431 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 432 |  |  | 	 * @param int    $log_item_id ID of the log item whose email sent status should be set to failed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 433 |  |  | 	 * @param string $message     Error message. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 434 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 435 |  |  | 	 * @since 2.4.0 Include error message during update. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 436 |  |  | 	 * @since 2.3.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 437 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 438 |  |  | 	 * @global \wpdb $wpdb | 
            
                                                                                                            
                            
            
                                    
            
            
                | 439 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 440 |  |  | 	 * @see  TableManager::get_log_table_name() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 441 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 442 |  |  | 	public function mark_log_as_failed( $log_item_id, $message ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 443 |  |  | 		global $wpdb; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 444 |  |  | 		$table_name = $this->get_log_table_name(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 445 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 446 |  |  | 		$wpdb->update( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 447 |  |  | 			$table_name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 448 |  |  | 			array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 449 |  |  | 				'result'        => '0', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 450 |  |  | 				'error_message' => $message, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 451 |  |  | 			), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 452 |  |  | 			array( 'ID' => $log_item_id ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 453 |  |  | 			array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 454 |  |  | 				'%d', // `result` format. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 455 |  |  | 				'%s', // `error_message` format. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 456 |  |  | 			), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 457 |  |  | 			array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 458 |  |  | 				'%d', // `ID` format. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 459 |  |  | 			) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 460 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 461 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 462 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 463 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 464 |  |  | 	 * Updates the DB schema. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 465 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 466 |  |  | 	 * Adds new columns to the Database as of v0.2. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 467 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 468 |  |  | 	 * @since 2.3.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 469 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 470 |  |  | 	private function update_table_if_needed() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 471 |  |  | 		if ( get_option( self::DB_OPTION_NAME, false ) === self::DB_VERSION ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 472 |  |  | 			return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 473 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 474 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 475 |  |  | 		$sql = $this->get_create_table_query(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 476 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 477 |  |  | 		require_once ABSPATH . 'wp-admin/includes/upgrade.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 478 |  |  | 		dbDelta( $sql ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 479 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 480 |  |  | 		update_option( self::DB_OPTION_NAME, self::DB_VERSION ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 481 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 482 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 483 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 484 |  |  | 	 * Gets the Create Table query. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 485 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 486 |  |  | 	 * @since 2.4.0 Added error_message column. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 487 |  |  | 	 * @since 2.3.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 488 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 489 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 490 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 491 |  |  | 	private function get_create_table_query() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 492 |  |  | 		global $wpdb; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 493 |  |  | 		$table_name      = $this->get_log_table_name(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 494 |  |  | 		$charset_collate = $wpdb->get_charset_collate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 495 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 496 |  |  | 		$sql = 'CREATE TABLE ' . $table_name . ' ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 497 |  |  | 				id mediumint(9) NOT NULL AUTO_INCREMENT, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 498 |  |  | 				to_email VARCHAR(500) NOT NULL, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 499 |  |  | 				subject VARCHAR(500) NOT NULL, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 500 |  |  | 				message TEXT NOT NULL, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 501 |  |  | 				headers TEXT NOT NULL, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 502 |  |  | 				attachments TEXT NOT NULL, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 503 |  |  | 				sent_date timestamp NOT NULL, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 504 |  |  | 				attachment_name VARCHAR(1000), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 505 |  |  | 				ip_address VARCHAR(15), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 506 |  |  | 				result TINYINT(1), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 507 |  |  | 				error_message VARCHAR(1000), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 508 |  |  | 				PRIMARY KEY  (id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 509 |  |  | 			) ' . $charset_collate . ';'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 510 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 511 |  |  | 		return $sql; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 512 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 513 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 514 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 515 |  |  | 	 * Callback for the Array filter. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 516 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 517 |  |  | 	 * @since 2.3.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 518 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 519 |  |  | 	 * @param string $column A column from the array Columns. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 520 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 521 |  |  | 	 * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 522 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 523 |  |  | 	private function validate_columns( $column ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 524 |  |  | 		return in_array( $column, array( 'to' ), true ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 525 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 526 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 527 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 528 |  |  | 	 * Query log items by column. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 529 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 530 |  |  | 	 * @since 2.3.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 531 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 532 |  |  | 	 * @param array $columns Key value pair based on which items should be retrieved. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 533 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 534 |  |  | 	 * @uses \EmailLog\Core\DB\TableManager::validate_columns() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 535 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 536 |  |  | 	 * @return array|object|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 537 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 538 |  |  | 	public function query_log_items_by_column( $columns ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 539 |  |  | 		if ( ! is_array( $columns ) ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 540 |  |  | 			return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 541 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 542 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 543 |  |  | 		// Since we support PHP v5.2.4, we cannot use ARRAY_FILTER_USE_KEY | 
            
                                                                                                            
                            
            
                                    
            
            
                | 544 |  |  | 		// TODO: PHP v5.5: Once WordPress updates minimum PHP version to v5.5, start using ARRAY_FILTER_USE_KEY. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 545 |  |  | 		$columns_keys = array_keys( $columns ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 546 |  |  | 		if ( ! array_filter( $columns_keys, array( $this, 'validate_columns' ) ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 547 |  |  | 			return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 548 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 549 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 550 |  |  | 		global $wpdb; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 551 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 552 |  |  | 		$table_name = $this->get_log_table_name(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 553 |  |  | 		$query      = "SELECT id, sent_date, to_email, subject FROM {$table_name}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 554 |  |  | 		$query_cond = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 555 |  |  | 		$where      = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 556 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 557 |  |  | 		// Execute the following `if` conditions only when $data is array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 558 |  |  | 		if ( array_key_exists( 'to', $columns ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 559 |  |  | 			// Since the value is stored as CSV in DB, convert the values from error data to CSV to compare. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 560 |  |  | 			$to_email = Util\stringify( $columns['to'] ); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 561 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 562 |  |  | 			$to_email = trim( esc_sql( $to_email ) ); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 563 |  |  | 			$where[]  = "to_email = '$to_email'"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 564 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 565 |  |  | 			foreach ( $where as $index => $value ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 566 |  |  | 				$query_cond .= 0 === $index ? ' WHERE ' : ' AND '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 567 |  |  | 				$query_cond .= $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 568 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 569 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 570 |  |  | 			// Get only the latest logged item when multiple rows match. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 571 |  |  | 			$query_cond .= ' ORDER BY id DESC'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 572 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 573 |  |  | 			$query = $query . $query_cond; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 574 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 575 |  |  | 			return $wpdb->get_results( $query ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 576 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 577 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 578 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 579 |  |  |  |