$value) { if (isset($this->{$name})) { $this->{$name} = $value; } } } /** * @param int $cf_form_id */ public function set_cf_form_id($cf_form_id) { $this->cf_form_id = (int)$cf_form_id; } /** * @return int */ public function get_cf_form_id() { return $this->cf_form_id; } /** * Return the ID of the Optin * * @return int */ public function get_id() { return $this->id; } /** * Return the hash of the optin. * * @return string */ public function get_hash() { return $this->hash; } /** * Return true|false depending on confirm status * * @return bool */ public function is_confirmed() { return (bool)$this->get_doubleoptin(); } /** * update the optin value * * @param int $confirmed */ public function set_doubleoptin($confirmed) { $this->doubleoptin = (int)$confirmed; } /** * Return the optin value for the form. * * @return int */ public function get_doubleoptin() { return $this->doubleoptin; } /** * @param string $timestamp */ public function set_createtime($timestamp) { $this->createtime = $timestamp; } /** * Return the Date how long the opt in will be valid * @return string */ public function get_valid_until(){ $settings = CF7DoubleOptIn::getInstance()->getSettings(); $createtime = $this->get_createtime(); $dt = new \DateTime(); $dt->setTimestamp($createtime); if($this->is_confirmed()){ $month = (int)$settings['delete']; $period = $settings['delete_period']; }else{ $month = (int)$settings['delete_unconfirmed']; $period = $settings['delete_unconfirmed_period']; } $dt->modify('+'.(int)$month.' '.$period); $dt->modify('+1 day'); return $dt->format('d.m.Y'); } /** * Return a timestamp * * @param string $view Select how to return the content. raw is the stored db value. use formatted to return a * formatted string. * * @return string */ public function get_createtime($view = "raw") { if ($view != "raw") { if (!is_numeric($this->createtime)) { $date = date('d.m.Y', strtotime($this->createtime)); $time = date('H:i:s', strtotime($this->createtime)); } else { $date = date('d.m.Y', $this->createtime); $time = date('H:i:s', $this->createtime); } return $date . ' ' . __('/', 'double-opt-in') . ' ' . $time; } else { return $this->createtime; } } /** * @param string $timestamp */ public function set_updatetime($timestamp) { $this->updatetime = $timestamp; } /** * Return a timestamp * * @param string $view Select how to return the content. raw is the stored db value. use formatted to return a * formatted string. * * @return string */ public function get_updatetime($view = 'raw') { if ($view != "raw") { if (!is_numeric($this->updatetime)) { $date = date('d.m.Y', strtotime($this->updatetime)); $time = date('H:i:s', strtotime($this->updatetime)); } else { $date = date('d.m.Y', $this->updatetime); $time = date('H:i:s', $this->updatetime); } return $date . ' ' . __('/', 'double-opt-in') . ' ' . $time; } else { return $this->updatetime; } } /** * @param string $ip_addr */ public function set_ipaddr_register($ip_addr) { $this->ipaddr_register = $ip_addr; } /** * @return string */ public function get_ipaddr_register() { return $this->ipaddr_register; } /** * @param string $ip_addr */ public function set_ipaddr_confirmation($ip_addr) { $this->ipaddr_confirmation = $ip_addr; } /** * @return string */ public function get_ipaddr_confirmation() { return $this->ipaddr_confirmation; } /** * Return a serialized string * * @return mixed */ public function get_content() { return $this->content; } /** * @param string - a serialized string */ public function set_content($content) { $this->content = $content; } /** * @param string - a serialized string */ public function set_files($files) { $this->files = $files; } /** * @return string use maybe_unserialize to deserialize the string */ public function get_files() { return $this->files; } /** * Return the assigned Category ID * * @return int */ public function get_category() { if (!is_numeric($this->category)) { return 0; } return $this->category; } /** * Assign a ID to the category * * @param int $id * * @return void */ public function set_category($id) { $this->category = $id; } /** * Check if the given Opt In was created by Contact Form 7 * * @return bool */ public function isTypeCF7() { $form_id = $this->get_cf_form_id(); if (class_exists('\WPCF7_ContactForm')) { $form = \WPCF7_ContactForm::get_instance($form_id); if ($form) { return true; } } return false; } /** * Check if the given Opt In was created by Avada * * @return bool|void */ public function isTypeAvada() { $form_id = $this->get_cf_form_id(); if (function_exists('Avada')) { $form = get_post($form_id); if ($form) { return true; } } } /** * Check if the Opt In was created by 'cf7' or 'avada'. * * @param string $type use either 'cf7' or 'avada' * * @return bool */ public function isType($type) { if ($type == 'cf7') { return $this->isTypeCF7(); } else { return (!$this->isTypeCF7() && $this->isTypeAvada()); } } /** * Load an OptIn by the given hash code. Returns either an Object of Type OptIn or null if not found. * * @param string $hash * * @return OptIn|null */ public static function get_by_hash(string $hash) { global $wpdb; $table = $wpdb->prefix . 'f12_cf7_doubleoptin'; if (null == $wpdb) { return null; } $row = $wpdb->get_row('SELECT * FROM ' . $table . ' WHERE hash="' . $hash . '"', ARRAY_A); if (null == $row) { return null; } return new OptIn($row); } /** * Get number of optins for the given post. * * @param $post_id * * @return int */ public static function get_count($post_id) { global $wpdb; if (!$wpdb) { return 0; } $tableName = $wpdb->prefix . 'f12_cf7_doubleoptin'; $result = $wpdb->get_results($wpdb->prepare('SELECT count(*) AS counter FROM ' . $tableName . ' WHERE cf_form_id=%d ', $post_id)); if (is_array($result)) { return $result[0]->counter; } return 0; } /** * Return a list of opt ins stored in the database. * * @param int $category_id * * @param array $atts array ( * 'perPage' => 10, // Posts per page * 'page' => 0, // Current page * 'order' => DESC, // Order (ASC/DESC) * ); * * @param null $numberOfPages - Stores the number of pages found if limited * * @return array */ public static function get_list_by_category_id($category_id, $atts = array(), &$numberOfPages = null) { global $wpdb; $attr = array( 'perPage' => 10, 'page' => 1, 'order' => 'DESC', 'keyword' => '' ); /** * Parameter */ foreach ($atts as $key => $value) { if (isset($attr[$key])) { $attr[$key] = $atts[$key]; } } /* * Update keyword */ $where = ''; if (!empty($attr['keyword'])) { $where = ' AND content LIKE "%%' . $attr['keyword'] . '%%"'; } $tableName = $wpdb->prefix . 'f12_cf7_doubleoptin'; $pageNum = (int)$attr['page'] - 1; if ($numberOfPages !== null) { $result = $wpdb->get_results($wpdb->prepare('SELECT count(*) AS counter FROM ' . $tableName . ' WHERE category=%d ' . $where, $category_id)); $itemCounter = 0; if (is_array($result)) { $itemCounter = $result[0]->counter; } $numberOfPages = 0; if ($itemCounter != 0) { $numberOfPages = ceil($itemCounter / (int)$attr['perPage']); } } $list = array(); if ($numberOfPages == 0) { return $list; } $offset = $pageNum * (int)$attr['perPage']; $rows = $wpdb->get_results($wpdb->prepare('SELECT * FROM ' . $tableName . ' WHERE category=%d ' . $where . ' ORDER BY ID ' . $attr['order'] . ' LIMIT ' . $attr['perPage'] . ' OFFSET %d', $category_id, $offset), ARRAY_A); foreach ($rows as $row) { $list[] = new OptIn($row); } return $list; } /** * Return a list of opt ins stored in the database. * * @param string $email * @param int $confirmed Either recieve confirmed or unconfirmed only * * @return array */ public static function get_list_by_email($email) { global $wpdb; $tableName = $wpdb->prefix . 'f12_cf7_doubleoptin'; $list = array(); $rows = $wpdb->get_results($wpdb->prepare('SELECT * FROM ' . $tableName . ' WHERE email=%s', $email), ARRAY_A); foreach ($rows as $row) { $list[] = new OptIn($row); } return $list; } /** * Return a list of opt ins stored in the database. * * @param $atts array ( * 'perPage' => 10, // Posts per page * 'page' => 0, // Current page * 'order' => DESC, // Order (ASC/DESC) * ); * * @param null $numberOfPages - Stores the number of pages found if limited * * @return array */ public static function get_list($atts = array(), &$numberOfPages = null) { global $wpdb; $attr = array( 'perPage' => 10, 'page' => 1, 'order' => 'DESC', 'keyword' => '', 'cf_form_id' => '', ); /* * Update atts */ foreach ($atts as $key => $value) { if (isset($attr[$key])) { $attr[$key] = $atts[$key]; } } /* * Update WHERE for Query */ $where = ''; $where_condition = []; /* * Add Keyword */ if (!empty($attr['keyword'])) { $where_condition[] = 'content LIKE "%%' . $attr['keyword'] . '%%"'; } /* * Add Form ID */ if (!empty($attr['cf_form_id'])) { $where_condition[] = 'cf_form_id="' . $attr['cf_form_id'] . '"'; } /* * build where string */ if (!empty($where_condition)) { $where = ' WHERE ' . implode(' AND ', $where_condition); } /* * set table */ $tableName = $wpdb->prefix . 'f12_cf7_doubleoptin'; $pageNum = (int)$attr['page'] - 1; if ($numberOfPages !== null) { $result = $wpdb->get_results('SELECT count(*) as counter FROM ' . $tableName . $where); $itemCounter = 0; if (is_array($result)) { $itemCounter = $result[0]->counter; } $numberOfPages = 0; if ($itemCounter != 0) { $numberOfPages = ceil($itemCounter / (int)$attr['perPage']); } } $list = array(); if ($numberOfPages == 0) { return $list; } $offset = $pageNum * (int)$attr['perPage']; $rows = $wpdb->get_results($wpdb->prepare('SELECT * FROM ' . $tableName . $where . ' ORDER BY ID ' . $attr['order'] . ' LIMIT ' . $attr['perPage'] . ' OFFSET % d', $offset), ARRAY_A); foreach ($rows as $row) { $list[] = new OptIn($row); } return $list; } /** * This will update all categories from A to B, e.g.: All Opt-Ins with id 1 to category id 2. * * @param int $from_id The origin Category ID * @param int $to_id The new Category ID * * @return bool|int|\mysqli_result|resource|null Return the number of rows affected by the query. */ public static function bulk_update_category($from_id, $to_id) { global $wpdb; $table = $wpdb->prefix . 'f12_cf7_doubleoptin'; return $wpdb->query($wpdb->prepare('UPDATE ' . $table . ' SET category =%d WHERE category =%d', $to_id, $from_id)); } /** * Update the category of the given opt in id. * * @param int $optin_id The ID of the OptIn * @param int $category_id The ID of the Category * * @return bool|int|\mysqli_result|resource|null */ public static function update_category_by_id($optin_id, $category_id) { global $wpdb; $table = $wpdb->prefix . 'f12_cf7_doubleoptin'; return $wpdb->query($wpdb->prepare('UPDATE ' . $table . ' SET category =%d WHERE id =%d', $category_id, $optin_id)); } /** * Update or create the given Object */ public function save() { global $wpdb; $table = $wpdb->prefix . 'f12_cf7_doubleoptin'; if (!empty($this->get_hash())) { return $wpdb->update($table, array( 'doubleoptin' => $this->get_doubleoptin(), 'createtime' => $this->get_createtime(), 'updatetime' => $this->get_updatetime(), 'ipaddr_confirmation' => $this->get_ipaddr_confirmation(), 'ipaddr_register' => $this->get_ipaddr_register(), 'cf_form_id' => $this->get_cf_form_id(), 'content' => $this->get_content(), 'files' => $this->get_files(), 'category' => $this->get_category(), 'mail_optin' => $this->get_mail_optin(), ), array( 'hash' => $this->get_hash() )); } else { $result = $wpdb->insert($table, array( 'cf_form_id' => $this->get_cf_form_id(), 'doubleoptin' => $this->get_doubleoptin(), 'createtime' => $this->get_createtime(), 'updatetime' => $this->get_updatetime(), 'ipaddr_confirmation' => $this->get_ipaddr_confirmation(), 'ipaddr_register' => $this->get_ipaddr_register(), 'content' => $this->get_content(), 'files' => $this->get_files(), 'category' => $this->get_category(), 'form' => $this->get_form(), 'mail_optin' => $this->get_mail_optin(), 'email' => $this->get_email() )); if ($result > 0) { $this->id = $wpdb->insert_id; return $this->createHash(); } } return false; } /** * Creates a unique hash code and adds it to the current OptIn Object */ private function createHash() { global $wpdb; if (null == $wpdb) { return false; } $table = $wpdb->prefix . 'f12_cf7_doubleoptin'; // add the hash to the element $this->hash = base64_encode($this->get_createtime() . $this->get_id() . $this->get_cf_form_id()); return $wpdb->update($table, array( 'hash' => $this->hash, ), array( 'id' => $this->get_id() )); } /** * Return the Form * * @return string */ public function get_form() { return $this->form; } /** * Set the Form as HTML * * @param string $form * * @return void */ public function set_form($form) { $this->form = $form; } /** * Return the Form * * @return string */ public function get_mail_optin() { return $this->mail_optin; } /** * Return the E-mail * * @return string */ public function get_email() { return $this->email; } /** * Store the email * * @param string $email * * @return void */ public function set_email($email) { $this->email = $email; } /** * Set the Form as HTML * * @param string $form * * @return void */ public function set_mail_optin($mail_html) { $this->mail_optin = $mail_html; } /** * Return the link to the detail view * * @return string */ public function get_link_ui() { return admin_url('admin.php?page=f12-cf7-doubleoptin_optin_view&hash=' . $this->get_hash()); } /** * Return the link to delete * * @return string */ public function get_link_delete() { return admin_url('admin.php?page=f12-cf7-doubleoptin&option=delete&hash=' . $this->get_hash()); } /** * Return the link to the optin * * @return string */ public function get_link_optin() { if ($this->get_cf_form_id() == 0) { return get_home_url() . '?optin=' . $this->get_hash(); } $parameter = CF7DoubleOptIn::getInstance()->getParameter($this->get_cf_form_id()); $page_id = $parameter['page']; if ($page_id == -1 || !$page_id || $page_id == 0) { $link = get_home_url() . '?optin=' . $this->get_hash(); } else { $link = get_permalink($page_id) . '?optin=' . $this->get_hash(); } return $link; } } }