1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Never5\DownloadMonitor\Shop\Util; |
4
|
|
|
|
5
|
|
|
use Never5\DownloadMonitor\Shop\Services\Services; |
6
|
|
|
|
7
|
|
|
class Page { |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Checks if current page is cart page |
11
|
|
|
* |
12
|
|
|
* @return bool |
13
|
|
|
*/ |
14
|
|
|
public function is_cart() { |
15
|
|
|
if ( download_monitor()->service( 'settings' )->get_option( 'page_cart' ) == get_the_ID() ) { |
|
|
|
|
16
|
|
|
return true; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
return false; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Checks if current page is checkout page |
24
|
|
|
* |
25
|
|
|
* @return bool |
26
|
|
|
*/ |
27
|
|
|
public function is_checkout() { |
28
|
|
|
if ( download_monitor()->service( 'settings' )->get_option( 'page_checkout' ) == get_the_ID() ) { |
|
|
|
|
29
|
|
|
return true; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return false; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Returns cart URL |
37
|
|
|
* |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
public function get_cart_url() { |
41
|
|
|
return get_permalink( download_monitor()->service( 'settings' )->get_option( 'page_cart' ) ); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Returns add to cart URL for given download ID |
46
|
|
|
* |
47
|
|
|
* @param int $download_id |
48
|
|
|
* |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
public function get_add_to_cart_url( $download_id ) { |
52
|
|
|
return add_query_arg( array( 'dlm-add-to-cart' => $download_id ), $this->get_cart_url() ); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Returns checkout URL |
57
|
|
|
* |
58
|
|
|
* @param string $action |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function get_checkout_url( $action = '' ) { |
63
|
|
|
|
64
|
|
|
$endpoint = ''; |
65
|
|
|
if ( ! empty( $action ) ) { |
66
|
|
|
switch ( $action ) { |
67
|
|
|
case 'complete': |
68
|
|
|
$endpoint = 'complete'; |
69
|
|
|
break; |
70
|
|
|
case 'cancelled': |
71
|
|
|
$endpoint = 'cancelled'; |
72
|
|
|
break; |
73
|
|
|
default: |
74
|
|
|
$endpoint = ''; |
75
|
|
|
break; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$url = get_permalink( download_monitor()->service( 'settings' )->get_option( 'page_checkout' ) ); |
|
|
|
|
80
|
|
|
|
81
|
|
|
if ( ! empty( $endpoint ) ) { |
82
|
|
|
$url = add_query_arg( 'ep', $endpoint, $url ); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $url; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Redirect to checkout page |
90
|
|
|
*/ |
91
|
|
|
public function to_checkout() { |
92
|
|
|
Services::get()->service( 'redirect' )->redirect( Services::get()->service( 'page' )->get_checkout_url() ); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Redirect to cart page |
97
|
|
|
*/ |
98
|
|
|
public function to_cart() { |
99
|
|
|
Services::get()->service( 'redirect' )->redirect( Services::get()->service( 'page' )->get_cart_url() ); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get all pages and format them in a id(k)=>title(v) array |
104
|
|
|
* |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
public function get_pages() { |
108
|
|
|
// setup array with default option |
109
|
|
|
$pages = array( |
110
|
|
|
0 => '-- ' . __( 'no page', 'download-monitor' ) . ' --' |
|
|
|
|
111
|
|
|
); |
112
|
|
|
// get pages from WP |
113
|
|
|
$pages_raw = get_pages(); |
|
|
|
|
114
|
|
|
// count. loop. add |
115
|
|
|
if ( count( $pages_raw ) > 0 ) { |
116
|
|
|
foreach ( $pages_raw as $page ) { |
117
|
|
|
$pages[ $page->ID ] = $page->post_title; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// return |
122
|
|
|
return $pages; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
} |