| @@ -1,68 +1,114 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace forge12\contactform7\CF7DoubleOptIn { |
| 4 | - if (!defined('ABSPATH')) { | |
| 5 | - exit; | |
| 6 | - } | |
| 7 | 4 | |
| 8 | - /** | |
| 9 | - * Class Pagination | |
| 10 | - * | |
| 11 | - * @package forge12\contactform7\CF7DoubleOptIn | |
| 12 | - */ | |
| 13 | - class Pagination | |
| 14 | - { | |
| 15 | - private static $_instance = null; | |
| 5 | + use Forge12\Shared\Logger; | |
| 6 | + use Forge12\Shared\LoggerInterface; | |
| 16 | 7 | |
| 17 | - public static function getInstance() | |
| 18 | - { | |
| 19 | - if (null === self::$_instance) { | |
| 20 | - self::$_instance = new Pagination(); | |
| 21 | - } | |
| 22 | - return self::$_instance; | |
| 23 | - } | |
| 8 | + if ( ! defined( 'ABSPATH' ) ) { | |
| 9 | + exit; | |
| 10 | + } | |
| 24 | 11 | |
| 25 | - private function __construct() | |
| 26 | - { | |
| 27 | - // Add additional actions | |
| 28 | - add_filter('f12_cf7_doubleoptin_pagination_link', array($this, 'buildLink'), 10, 2); | |
| 29 | - } | |
| 12 | + /** | |
| 13 | + * Class Pagination | |
| 14 | + * | |
| 15 | + * @package forge12\contactform7\CF7DoubleOptIn | |
| 16 | + */ | |
| 17 | + class Pagination { | |
| 18 | + private static $_instance = null; | |
| 19 | + private LoggerInterface $logger; | |
| 30 | 20 | |
| 31 | - public function buildLink($link, $parameter = array()) | |
| 32 | - { | |
| 33 | - /* | |
| 34 | - * Default Parameter | |
| 35 | - */ | |
| 36 | - $default = array( | |
| 37 | - 'perPage' => 10, | |
| 38 | - 'pageNum' => 1, | |
| 39 | - ); | |
| 21 | + public static function getInstance() { | |
| 22 | + if ( null === self::$_instance ) { | |
| 23 | + self::$_instance = new Pagination( Logger::getInstance() ); | |
| 24 | + } | |
| 40 | 25 | |
| 41 | - if(isset($_GET['perPage'])){ | |
| 42 | - $default['perPage'] = (int)$_GET['perPage']; | |
| 43 | - } | |
| 26 | + return self::$_instance; | |
| 27 | + } | |
| 44 | 28 | |
| 45 | - if(isset($_GET['pageNum'])){ | |
| 46 | - $default['pageNum'] = (int)$_GET['pageNum']; | |
| 47 | - } | |
| 29 | + private function __construct( LoggerInterface $logger ) { | |
| 30 | + $this->logger = $logger; | |
| 31 | + $this->get_logger()->info( 'OptIn class instance created.', [ | |
| 32 | + 'plugin' => 'double-opt-in', | |
| 33 | + ] ); | |
| 48 | 34 | |
| 49 | - if(isset($_GET['keyword'])){ | |
| 50 | - $default['keyword'] = sanitize_text_field($_GET['keyword']); | |
| 51 | - } | |
| 35 | + // Add additional actions | |
| 36 | + add_filter( 'f12_cf7_doubleoptin_pagination_link', array( $this, 'buildLink' ), 10, 2 ); | |
| 37 | + $this->get_logger()->debug( 'Added "f12_cf7_doubleoptin_pagination_link" filter to "buildLink" method.', [ | |
| 38 | + 'plugin' => 'double-opt-in', | |
| 39 | + ] ); | |
| 40 | + } | |
| 52 | 41 | |
| 53 | - if(isset($_GET['cf_form_id'])){ | |
| 54 | - $default['cf_form_id'] = (int)$_GET['cf_form_id']; | |
| 55 | - } | |
| 42 | + public function get_logger() { | |
| 43 | + return $this->logger; | |
| 44 | + } | |
| 56 | 45 | |
| 57 | - $default = array_merge($default, $parameter); | |
| 46 | + public function buildLink( $link, $parameter = array() ) { | |
| 47 | + $this->get_logger()->info( 'Building a pagination link with existing and new parameters.', [ | |
| 48 | + 'plugin' => 'double-opt-in', | |
| 49 | + 'base_link' => $link, | |
| 50 | + 'additional_parameters' => $parameter, | |
| 51 | + ] ); | |
| 58 | 52 | |
| 59 | - $default_keypair = array(); | |
| 53 | + /* | |
| 54 | + * Default Parameter | |
| 55 | + */ | |
| 56 | + $default = array( | |
| 57 | + 'perPage' => 10, | |
| 58 | + 'pageNum' => 1, | |
| 59 | + ); | |
| 60 | 60 | |
| 61 | - foreach ($default as $key => $value) { | |
| 62 | - $default_keypair[] = $key . '=' . $value; | |
| 63 | - } | |
| 61 | + // Get existing parameters from the URL | |
| 62 | + if ( isset( $_GET['perPage'] ) ) { | |
| 63 | + $default['perPage'] = (int) $_GET['perPage']; | |
| 64 | + $this->get_logger()->debug( 'Found perPage in URL: ' . $default['perPage'], [ | |
| 65 | + 'plugin' => 'double-opt-in', | |
| 66 | + ] ); | |
| 67 | + } | |
| 64 | 68 | |
| 65 | - return $link . '?' . implode("&", $default_keypair); | |
| 66 | - } | |
| 67 | - } | |
| 69 | + if ( isset( $_GET['pageNum'] ) ) { | |
| 70 | + $default['pageNum'] = (int) $_GET['pageNum']; | |
| 71 | + $this->get_logger()->debug( 'Found pageNum in URL: ' . $default['pageNum'], [ | |
| 72 | + 'plugin' => 'double-opt-in', | |
| 73 | + ] ); | |
| 74 | + } | |
| 75 | + | |
| 76 | + if ( isset( $_GET['keyword'] ) ) { | |
| 77 | + $default['keyword'] = sanitize_text_field( $_GET['keyword'] ); | |
| 78 | + $this->get_logger()->debug( 'Found keyword in URL: ' . $default['keyword'], [ | |
| 79 | + 'plugin' => 'double-opt-in', | |
| 80 | + ] ); | |
| 81 | + } | |
| 82 | + | |
| 83 | + if ( isset( $_GET['cf_form_id'] ) ) { | |
| 84 | + $default['cf_form_id'] = (int) $_GET['cf_form_id']; | |
| 85 | + $this->get_logger()->debug( 'Found cf_form_id in URL: ' . $default['cf_form_id'], [ | |
| 86 | + 'plugin' => 'double-opt-in', | |
| 87 | + ] ); | |
| 88 | + } | |
| 89 | + | |
| 90 | + // Merge default and provided parameters | |
| 91 | + $merged_params = array_merge( $default, $parameter ); | |
| 92 | + $this->get_logger()->debug( 'Merged all parameters for the link.', [ | |
| 93 | + 'plugin' => 'double-opt-in', | |
| 94 | + 'merged_params' => $merged_params, | |
| 95 | + ] ); | |
| 96 | + | |
| 97 | + $default_keypair = array(); | |
| 98 | + | |
| 99 | + // Create a key=value pair for each parameter | |
| 100 | + foreach ( $merged_params as $key => $value ) { | |
| 101 | + $default_keypair[] = urlencode( $key ) . '=' . urlencode( $value ); | |
| 102 | + } | |
| 103 | + | |
| 104 | + $final_link = $link . '?' . implode( "&", $default_keypair ); | |
| 105 | + | |
| 106 | + $this->get_logger()->notice( 'Successfully built the pagination link.', [ | |
| 107 | + 'plugin' => 'double-opt-in', | |
| 108 | + 'final_link' => $final_link, | |
| 109 | + ] ); | |
| 110 | + | |
| 111 | + return $final_link; | |
| 112 | + } | |
| 113 | + } | |
| 68 | 114 | } |