Skip to content

Commit 964a8b1

Browse files
committed
Optimize redirect page management
1 parent 60195dd commit 964a8b1

File tree

7 files changed

+137
-94
lines changed

7 files changed

+137
-94
lines changed

plugin/aplazame.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ class WC_Aplazame {
3636
*/
3737
public $apiBaseUri;
3838

39+
/**
40+
* @var Aplazame_Redirect
41+
*/
42+
public $redirect;
43+
3944
/**
4045
* @param string $apiBaseUri
4146
*/
@@ -75,8 +80,10 @@ public function __construct($apiBaseUri) {
7580
add_action( 'wp_head', array( $this, 'aplazameJs' ), 999999 );
7681

7782
// Redirect
78-
register_activation_hook( __FILE__, 'Aplazame_Redirect::get_the_ID' );
79-
add_action( 'wp_footer', 'Aplazame_Redirect::payload' );
83+
$this->redirect = new Aplazame_Redirect();
84+
register_activation_hook( __FILE__, array( $this->redirect, 'addRedirectPage' ) );
85+
register_deactivation_hook( __FILE__, array( $this->redirect, 'removeRedirectPage' ) );
86+
add_action( 'wp_footer', array( $this->redirect, 'checkout' ) );
8087

8188
// TODO: Redirect nav
8289
// add_filter('wp_nav_menu_objects', '?');
@@ -180,13 +187,15 @@ public function add_gateway( $methods ) {
180187
* @return null|string
181188
*/
182189
public function router( $template ) {
183-
if ( Aplazame_Redirect::is_redirect() && isset( $_GET['action'] ) ) {
184-
switch ( $_GET['action'] ) {
185-
case 'confirm':
186-
return $this->confirm();
187-
case 'history':
188-
return $this->history();
189-
}
190+
if (! isset( $_GET['action'] ) || ! $this->redirect->isRedirect(get_the_ID())) {
191+
return $template;
192+
}
193+
194+
switch ( $_GET['action'] ) {
195+
case 'confirm':
196+
return $this->confirm();
197+
case 'history':
198+
return $this->history();
190199
}
191200

192201
return $template;

plugin/classes/lib/Redirect.php

Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,78 @@
11
<?php
22

33
class Aplazame_Redirect {
4+
/**
5+
* @var string
6+
*/
7+
public $id;
8+
9+
public function __construct() {
10+
$this->id = $this->getRedirectPageId();
11+
}
12+
413
/**
514
* @return bool
615
*/
7-
public static function is_redirect() {
16+
public function isRedirect( $id ) {
817

9-
return get_the_ID() === static::get_the_ID();
18+
return ( $this->id === $id );
19+
}
20+
21+
public function checkout() {
22+
23+
if ( ! isset( $_GET['order_id'] )
24+
|| ( (string) WC()->session->redirect_order_id !== $_GET['order_id'] )
25+
|| ! $this->isRedirect( get_the_ID() )
26+
) {
27+
return;
28+
}
29+
30+
Aplazame_Helpers::render_to_template( 'gateway/redirect.php' );
1031
}
1132

1233
/**
13-
* @return int
34+
* @return int|WP_Error
1435
*/
15-
public static function get_the_ID() {
36+
public function addRedirectPage() {
37+
if ( $this->id ) {
38+
return $this->id;
39+
}
1640

17-
$posts = get_posts( array(
41+
$post = array(
42+
'post_name' => 'aplazame-redirect',
43+
'post_title' => __( 'Aplazame Redirect' ),
1844
'post_type' => 'page',
19-
'meta_key' => 'aplazame-redirect',
20-
'meta_value' => 'true',
21-
'numberposts' => - 1,
22-
) );
45+
'post_status' => 'publish',
46+
'meta_input' => array(
47+
'aplazame-redirect' => 'true',
48+
),
49+
);
2350

24-
if ( empty( $posts ) ) {
25-
$defaults = array(
26-
'post_title' => __( 'Aplazame Redirect' ),
27-
'post_type' => 'page',
28-
'post_status' => 'publish',
29-
);
30-
31-
$post_id = array( wp_insert_post( $defaults ) );
32-
$post_id = $post_id[0];
33-
add_post_meta( $post_id, 'aplazame-redirect', 'true' );
34-
} else {
35-
$post_id = $posts[0]->ID;
36-
}
51+
$id = wp_insert_post( $post );
52+
53+
return $id;
54+
}
3755

38-
return $post_id;
56+
public function removeRedirectPage() {
57+
while ( $id = $this->getRedirectPageId() ) {
58+
wp_delete_post( $id, true );
59+
}
3960
}
4061

41-
public static function payload() {
62+
/**
63+
* @return int|false
64+
*/
65+
private function getRedirectPageId() {
66+
$posts = get_posts( array(
67+
'post_type' => 'page',
68+
'meta_key' => 'aplazame-redirect',
69+
) );
4270

43-
if ( static::is_redirect() && ( (string) WC()->session->redirect_order_id === $_GET['order_id'] ) ) {
44-
Aplazame_Helpers::render_to_template( 'gateway/redirect.php' );
71+
switch ( count( $posts ) ) {
72+
case 0:
73+
return false;
74+
default:
75+
return $posts[0]->ID;
4576
}
4677
}
4778
}

plugin/classes/wc-aplazame-gateway.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ public function payment_fields() {
5151
}
5252

5353
public function process_payment( $order_id ) {
54-
$url = get_permalink( Aplazame_Redirect::get_the_ID() );
54+
/** @var WC_Aplazame $aplazame */
55+
global $aplazame;
56+
57+
$url = get_permalink( $aplazame->redirect->id );
5558
WC()->session->redirect_order_id = $order_id;
5659

5760
return array(

plugin/l10n/es/aplazame-es_ES.mo

0 Bytes
Binary file not shown.

plugin/l10n/es/default.po

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
11
msgid ""
22
msgstr ""
33
"Report-Msgid-Bugs-To: https://github.com/aplazame/woocommerce\n"
4-
"POT-Creation-Date: 2016-06-21 14:17+0000\n"
4+
"POT-Creation-Date: 2016-07-27 12:32+0000\n"
55
"Language: es_ES\n"
66
"MIME-Version: 1.0\n"
77
"Content-Type: text/plain; charset=UTF-8\n"
88
"Content-Transfer-Encoding: 8bit\n"
99

10-
#: aplazame.php:116
10+
#: aplazame.php:123
1111
msgid "Settings"
1212
msgstr "Ajustes"
1313

14-
#: aplazame.php:205
14+
#: aplazame.php:214
1515
#, php-format
1616
msgid "%s ERROR: Order #%s cannot be confirmed."
1717
msgstr "ERROR en %s: El pedido #%s no se ha podido confirmar."
1818

19-
#: aplazame.php:214
19+
#: aplazame.php:223
2020
#, php-format
2121
msgid "Confirmed by %s."
2222
msgstr "Confirmado por %s."
2323

24-
#: aplazame.php:279
24+
#: aplazame.php:288
2525
#, php-format
2626
msgid "%s ERROR: Order #%s cannot be cancelled."
2727
msgstr "ERROR en %s: El pedido #%s no se ha podido cancelar."
2828

29-
#: aplazame.php:286
29+
#: aplazame.php:295
3030
#, php-format
3131
msgid "Order #%s has been successful cancelled by %s."
3232
msgstr "El pedido #%s ha sido cancelado correctamente por %s."
3333

34-
#: classes/lib/Redirect.php:26
34+
#: classes/lib/Redirect.php:43
3535
msgid "Aplazame Redirect"
3636
msgstr ""
3737

38-
#: classes/wc-aplazame-gateway.php:77
38+
#: classes/wc-aplazame-gateway.php:80
3939
#, php-format
4040
msgid "%s Error: \"%s\""
4141
msgstr "Error en %s: \"%s\""
4242

43-
#: classes/wc-aplazame-gateway.php:82
43+
#: classes/wc-aplazame-gateway.php:85
4444
#, php-format
4545
msgid "%s has successfully returned %d %s of the order #%s."
4646
msgstr "%s ha devuelto correctamente la cantidad de %d %s en el pedido #%s."
4747

48-
#: classes/wc-aplazame-gateway.php:98
48+
#: classes/wc-aplazame-gateway.php:101
4949
#, php-format
5050
msgid ""
5151
"Aplazame gateway requires the API keys, please <a href=\"%s\">sign up</a> "
@@ -54,75 +54,75 @@ msgstr ""
5454
"Se necesitan las claves de API para la pasarela de pago de Aplazame, por "
5555
"favor, <a href=\"%s\">regístrate</a> y obtén tus claves."
5656

57-
#: classes/wc-aplazame-gateway.php:108
57+
#: classes/wc-aplazame-gateway.php:111
5858
msgid "Enable/Disable"
5959
msgstr "Activar/Desactivar"
6060

61-
#: classes/wc-aplazame-gateway.php:109
61+
#: classes/wc-aplazame-gateway.php:112
6262
msgid "Enable Aplazame module"
6363
msgstr "Activar módulo de Aplazame"
6464

65-
#: classes/wc-aplazame-gateway.php:115
65+
#: classes/wc-aplazame-gateway.php:118
6666
msgid "Determines if the module is on Sandbox mode"
6767
msgstr "Determina si el módulo se encuentra en modo Sandbox"
6868

69-
#: classes/wc-aplazame-gateway.php:116
69+
#: classes/wc-aplazame-gateway.php:119
7070
msgid "Turn on Sandbox"
7171
msgstr "Activar modo Sandbox"
7272

73-
#: classes/wc-aplazame-gateway.php:119
73+
#: classes/wc-aplazame-gateway.php:122
7474
msgid "API Credentials"
7575
msgstr ""
7676

77-
#: classes/wc-aplazame-gateway.php:125
77+
#: classes/wc-aplazame-gateway.php:128
7878
msgid "Private API Key"
7979
msgstr "Clave API privada"
8080

81-
#: classes/wc-aplazame-gateway.php:126
81+
#: classes/wc-aplazame-gateway.php:129
8282
msgid "Aplazame API Private Key"
8383
msgstr "Clave API privada de Aplazame"
8484

85-
#: classes/wc-aplazame-gateway.php:134
85+
#: classes/wc-aplazame-gateway.php:137
8686
msgid "Public API Key"
8787
msgstr "Clave API pública"
8888

89-
#: classes/wc-aplazame-gateway.php:135
89+
#: classes/wc-aplazame-gateway.php:138
9090
msgid "Aplazame API Public Key"
9191
msgstr "Clave API pública de Aplazame"
9292

93-
#: classes/wc-aplazame-gateway.php:141
93+
#: classes/wc-aplazame-gateway.php:144
9494
msgid "Advanced options"
9595
msgstr ""
9696

97-
#: classes/wc-aplazame-gateway.php:147
97+
#: classes/wc-aplazame-gateway.php:150
9898
msgid "Button"
9999
msgstr "Botón"
100100

101-
#: classes/wc-aplazame-gateway.php:148
101+
#: classes/wc-aplazame-gateway.php:151
102102
msgid "Aplazame Button CSS Selector"
103103
msgstr "Selector CSS del botón de Aplazame"
104104

105-
#: classes/wc-aplazame-gateway.php:156
105+
#: classes/wc-aplazame-gateway.php:159
106106
msgid "Product quantity CSS selector"
107107
msgstr "Selector CSS del número de unidades del producto"
108108

109-
#: classes/wc-aplazame-gateway.php:157
109+
#: classes/wc-aplazame-gateway.php:160
110110
msgid "CSS selector pointing to product quantity"
111111
msgstr "Selector CSS para obtener la cantidad de producto"
112112

113-
#: classes/wc-aplazame-gateway.php:162
113+
#: classes/wc-aplazame-gateway.php:165
114114
msgid "Product price CSS selector"
115115
msgstr "Selector CSS del precio del producto"
116116

117-
#: classes/wc-aplazame-gateway.php:163
117+
#: classes/wc-aplazame-gateway.php:166
118118
msgid "CSS selector pointing to product price"
119119
msgstr "Selector CSS para obtener el precio del producto"
120120

121-
#: classes/wc-aplazame-gateway.php:168
121+
#: classes/wc-aplazame-gateway.php:171
122122
msgid "Variable product price CSS selector"
123123
msgstr "Selector CSS del precio variable del producto"
124124

125-
#: classes/wc-aplazame-gateway.php:169
125+
#: classes/wc-aplazame-gateway.php:172
126126
msgid "CSS selector pointing to variable product price"
127127
msgstr "Selector CSS para obtener el precio variable del producto"
128128

0 commit comments

Comments
 (0)