| 1 |
<?php |
| 2 |
|
| 3 |
namespace forge12\contactform7\CF7DoubleOptIn { |
| 4 |
|
| 5 |
use Forge12\Shared\Logger; |
| 6 |
use Forge12\Shared\LoggerInterface; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Class Pagination |
| 14 |
* |
| 15 |
* @package forge12\contactform7\CF7DoubleOptIn |
| 16 |
*/ |
| 17 |
class Pagination { |
| 18 |
private static $_instance = null; |
| 19 |
private LoggerInterface $logger; |
| 20 |
|
| 21 |
public static function getInstance() { |
| 22 |
if ( null === self::$_instance ) { |
| 23 |
self::$_instance = new Pagination( Logger::getInstance() ); |
| 24 |
} |
| 25 |
|
| 26 |
return self::$_instance; |
| 27 |
} |
| 28 |
|
| 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 |
] ); |
| 34 |
|
| 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 |
} |
| 41 |
|
| 42 |
public function get_logger() { |
| 43 |
return $this->logger; |
| 44 |
} |
| 45 |
|
| 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 |
] ); |
| 52 |
|
| 53 |
/* |
| 54 |
* Default Parameter |
| 55 |
*/ |
| 56 |
$default = array( |
| 57 |
'perPage' => 10, |
| 58 |
'pageNum' => 1, |
| 59 |
); |
| 60 |
|
| 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 |
} |
| 68 |
|
| 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 |
} |
| 114 |
} |