Completed
Push — master ( 1e9cab...a0a34c )
by Barry
06:02
created

Page::to_checkout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
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() ) {
0 ignored issues
show
Bug introduced by
The function get_the_ID was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

15
		if ( download_monitor()->service( 'settings' )->get_option( 'page_cart' ) == /** @scrutinizer ignore-call */ get_the_ID() ) {
Loading history...
Bug introduced by
The function download_monitor was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

15
		if ( /** @scrutinizer ignore-call */ download_monitor()->service( 'settings' )->get_option( 'page_cart' ) == get_the_ID() ) {
Loading history...
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() ) {
0 ignored issues
show
Bug introduced by
The function get_the_ID was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
		if ( download_monitor()->service( 'settings' )->get_option( 'page_checkout' ) == /** @scrutinizer ignore-call */ get_the_ID() ) {
Loading history...
Bug introduced by
The function download_monitor was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
		if ( /** @scrutinizer ignore-call */ download_monitor()->service( 'settings' )->get_option( 'page_checkout' ) == get_the_ID() ) {
Loading history...
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' ) );
0 ignored issues
show
Bug introduced by
The function download_monitor was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
		return get_permalink( /** @scrutinizer ignore-call */ download_monitor()->service( 'settings' )->get_option( 'page_cart' ) );
Loading history...
Bug introduced by
The function get_permalink was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
		return /** @scrutinizer ignore-call */ get_permalink( download_monitor()->service( 'settings' )->get_option( 'page_cart' ) );
Loading history...
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() );
0 ignored issues
show
Bug introduced by
The function add_query_arg was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
		return /** @scrutinizer ignore-call */ add_query_arg( array( 'dlm-add-to-cart' => $download_id ), $this->get_cart_url() );
Loading history...
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' ) );
0 ignored issues
show
Bug introduced by
The function download_monitor was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
		$url = get_permalink( /** @scrutinizer ignore-call */ download_monitor()->service( 'settings' )->get_option( 'page_checkout' ) );
Loading history...
Bug introduced by
The function get_permalink was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
		$url = /** @scrutinizer ignore-call */ get_permalink( download_monitor()->service( 'settings' )->get_option( 'page_checkout' ) );
Loading history...
80
81
		if ( ! empty( $endpoint ) ) {
82
			$url = add_query_arg( 'ep', $endpoint, $url );
0 ignored issues
show
Bug introduced by
The function add_query_arg was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

82
			$url = /** @scrutinizer ignore-call */ add_query_arg( 'ep', $endpoint, $url );
Loading history...
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' ) . ' --'
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

110
			0 => '-- ' . /** @scrutinizer ignore-call */ __( 'no page', 'download-monitor' ) . ' --'
Loading history...
111
		);
112
		// get pages from WP
113
		$pages_raw = get_pages();
0 ignored issues
show
Bug introduced by
The function get_pages was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

113
		$pages_raw = /** @scrutinizer ignore-call */ get_pages();
Loading history...
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
}