1: <?php
2: /**
3: * Admin Reports Page
4: *
5: * @package EPL
6: * @subpackage Admin/Reports
7: * @copyright Copyright (c) 2019, Merv Barrett
8: * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9: * @since 3.0
10: */
11:
12: // Exit if accessed directly.
13: if ( ! defined( 'ABSPATH' ) ) {
14: exit;
15: }
16: // phpcs:disable WordPress.Security.NonceVerification
17: /**
18: * Reports Page
19: *
20: * Renders the reports page contents.
21: *
22: * @since 3.0
23: * @return void
24: */
25: function epl_reports_page() {
26: $current_page = admin_url( 'edit.php?post_type=download&page=epl-reports' );
27: $active_tab = isset( $_GET['tab'] ) ? sanitize_text_field( wp_unslash( $_GET['tab'] ) ) : 'reports';
28: ?>
29: <div class="wrap">
30: <h1 class="nav-tab-wrapper">
31: <a href="
32: <?php
33: echo esc_url(
34: add_query_arg(
35: array(
36: 'tab' => 'reports',
37: 'settings-updated' => false,
38: ),
39: $current_page
40: )
41: );
42: ?>
43: " class="nav-tab <?php echo 'reports' === $active_tab ? esc_attr( 'nav-tab-active' ) : ''; ?>"><?php esc_html_e( 'Reports', 'easy-property-listings' ); ?></a>
44: <?php do_action( 'epl_reports_tabs' ); ?>
45: </h1>
46:
47: <?php
48: do_action( 'epl_reports_page_top' );
49: do_action( 'epl_reports_tab_' . $active_tab );
50: do_action( 'epl_reports_page_bottom' );
51: ?>
52: </div><!-- .wrap -->
53: <?php
54: }
55:
56: /**
57: * Default Report Views
58: *
59: * @since 3.0
60: * @return array $views Report Views
61: */
62: function epl_reports_default_views() {
63:
64: $views = apply_filters( 'epl_report_views', epl_get_active_post_types() );
65:
66: return $views;
67: }
68:
69: /**
70: * Default Report Views
71: *
72: * Checks the $_GET['view'] parameter to ensure it exists within the default allowed views.
73: *
74: * @param string $default_value Default view to use.
75: *
76: * @since 3.0
77: * @return string $view Report View
78: */
79: function epl_get_reporting_view( $default_value = 'property' ) {
80:
81: if ( ! isset( $_GET['view'] ) || ! in_array( $_GET['view'], array_keys( epl_reports_default_views() ), true ) ) {
82: $view = $default_value;
83: } else {
84: $view = sanitize_text_field( wp_unslash( $_GET['view'] ) );
85: }
86:
87: return apply_filters( 'epl_get_reporting_view', $view );
88: }
89:
90: /**
91: * Renders the Reports page
92: *
93: * @since 3.0
94: * @return void
95: */
96: function epl_reports_tab_reports() {
97:
98: if ( ! is_admin() || ! epl_reports_access() ) {
99: wp_die( esc_html__( 'You do not have permission to access this report', 'easy-property-listings' ), esc_html__( 'Error', 'easy-property-listings' ), array( 'response' => 403 ) );
100: }
101:
102: $current_view = 'property';
103: $views = epl_reports_default_views();
104:
105: if ( isset( $_GET['view'] ) && array_key_exists( sanitize_text_field( wp_unslash( $_GET['view'] ) ), $views ) ) {
106: $current_view = sanitize_text_field( wp_unslash( $_GET['view'] ) );
107: }
108:
109: do_action( 'epl_reports_view_' . $current_view );
110: }
111: add_action( 'epl_reports_tab_reports', 'epl_reports_tab_reports' );
112:
113: /**
114: * Renders the Reports Page Views Drop Downs
115: *
116: * @since 3.0
117: * @return void
118: */
119: function epl_report_views() {
120:
121: if ( ! is_admin() || ! epl_reports_access() ) {
122: return;
123: }
124:
125: $views = epl_reports_default_views();
126: $current_view = isset( $_GET['view'] ) ? sanitize_text_field( wp_unslash( $_GET['view'] ) ) : 'property';
127: ?>
128: <form id="epl-reports-filter" method="get">
129: <select id="epl-reports-view" name="view">
130: <option value="-1"><?php esc_html_e( 'Report Type', 'easy-property-listings' ); ?></option>
131: <?php foreach ( $views as $view_id => $label ) : ?>
132: <option value="<?php echo esc_attr( $view_id ); ?>" <?php selected( $view_id, $current_view ); ?>><?php echo esc_attr( $label ); ?></option>
133: <?php endforeach; ?>
134: </select>
135:
136: <?php do_action( 'epl_report_view_actions' ); ?>
137:
138: <input type="hidden" name="page" value="epl-reports"/>
139: <?php submit_button( __( 'Show', 'easy-property-listings' ), 'secondary', 'submit', false ); ?>
140: </form>
141: <?php
142: do_action( 'epl_report_view_actions_after' );
143: }
144:
145: /**
146: * Renders the Reports property Graphs
147: *
148: * @since 3.0
149: * @return void
150: */
151: function epl_reports_property() {
152:
153: if ( ! is_admin() || ! epl_reports_access() ) {
154: return;
155: }
156: ?>
157: <div class="tablenav top">
158: <div class="alignleft actions"><?php epl_report_views(); ?></div>
159: </div>
160: <?php
161: epl_reports_graph( 'sold', 'current', '#e50000', '#a5df41' );
162: }
163: add_action( 'epl_reports_view_property', 'epl_reports_property' );
164:
165: /**
166: * Renders the Reports rental Graphs
167: *
168: * @since 3.0
169: * @return void
170: */
171: function epl_reports_rental() {
172:
173: if ( ! is_admin() || ! epl_reports_access() ) {
174: return;
175: }
176: ?>
177: <div class="tablenav top">
178: <div class="alignleft actions"><?php epl_report_views(); ?></div>
179: </div>
180: <?php
181: epl_reports_graph( 'leased', 'current', '#e50000', '#a5df41' );
182: }
183: add_action( 'epl_reports_view_rental', 'epl_reports_rental' );
184:
185: /**
186: * Renders the Reports land Graphs
187: *
188: * @since 3.0
189: * @return void
190: */
191: function epl_reports_land() {
192:
193: if ( ! is_admin() || ! epl_reports_access() ) {
194: return;
195: }
196: ?>
197: <div class="tablenav top">
198: <div class="alignleft actions"><?php epl_report_views(); ?></div>
199: </div>
200: <?php
201: epl_reports_graph( 'sold', 'current', '#e50000', '#a5df41' );
202: }
203: add_action( 'epl_reports_view_land', 'epl_reports_land' );
204:
205: /**
206: * Renders the Reports land Graphs
207: *
208: * @since 3.0
209: * @return void
210: */
211: function epl_reports_commercial() {
212:
213: if ( ! is_admin() || ! epl_reports_access() ) {
214: return;
215: }
216: ?>
217: <div class="tablenav top">
218: <div class="alignleft actions"><?php epl_report_views(); ?></div>
219: </div>
220: <?php
221: epl_reports_graph( 'sold', 'current', '#e50000', '#a5df41' );
222: }
223: add_action( 'epl_reports_view_commercial', 'epl_reports_commercial' );
224:
225: /**
226: * Renders the Reports land Graphs
227: *
228: * @since 3.0
229: * @return void
230: */
231: function epl_reports_commercial_land() {
232:
233: if ( ! is_admin() || ! epl_reports_access() ) {
234: return;
235: }
236: ?>
237: <div class="tablenav top">
238: <div class="alignleft actions"><?php epl_report_views(); ?></div>
239: </div>
240: <?php
241: epl_reports_graph( 'sold', 'current', '#e50000', '#a5df41' );
242: }
243: add_action( 'epl_reports_view_commercial_land', 'epl_reports_commercial_land' );
244:
245: /**
246: * Renders the Reports land Graphs
247: *
248: * @since 3.0
249: * @return void
250: */
251: function epl_reports_business() {
252:
253: if ( ! is_admin() || ! epl_reports_access() ) {
254: return;
255: }
256: ?>
257: <div class="tablenav top">
258: <div class="alignleft actions"><?php epl_report_views(); ?></div>
259: </div>
260: <?php
261: epl_reports_graph( 'sold', 'current', '#e50000', '#a5df41' );
262: }
263: add_action( 'epl_reports_view_business', 'epl_reports_business' );
264:
265: /**
266: * Renders the Reports land Graphs
267: *
268: * @since 3.0
269: * @return void
270: */
271: function epl_reports_rural() {
272:
273: if ( ! is_admin() || ! epl_reports_access() ) {
274: return;
275: }
276: ?>
277: <div class="tablenav top">
278: <div class="alignleft actions"><?php epl_report_views(); ?></div>
279: </div>
280: <?php
281: epl_reports_graph( 'sold', 'current', '#e50000', '#a5df41' );
282: }
283: add_action( 'epl_reports_view_rural', 'epl_reports_rural' );
284:
285: /**
286: * Reports Access Roles
287: *
288: * @since 3.0
289: */
290: function epl_reports_access() {
291: $allowed = epl_get_option( 'min_reports_access' );
292: $allowed = empty( $allowed ) ? 'level_10' : $allowed;
293: return current_user_can( $allowed ) ? true : false;
294: }
295: