1: <?php
2: /**
3: * EPL Actions
4: *
5: * @package EPL
6: * @subpackage Functions/Actions
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:
17: // phpcs:disable WordPress.Security.NonceVerification
18:
19: /**
20: * Actions that may be triggered by the epl_action / epl-action request parameter.
21: *
22: * Only these actions are dispatched. Every other epl_* hook is internal and must never be
23: * reachable from a URL or form, as it would be called with the raw request as its arguments.
24: * Actions are only dispatched for logged in users. Each allowed handler is still responsible
25: * for its own capability and nonce checks.
26: *
27: * @since 3.5.26
28: * @return array
29: */
30: function epl_get_allowed_request_actions() {
31:
32: $actions = array(
33: 'new-contact',
34: 'meta-contact',
35: 'delete-contact',
36: 'add-contact-note',
37: 'add-contact-note-note-tab',
38: 'filter_reports',
39: );
40:
41: return (array) apply_filters( 'epl_allowed_request_actions', $actions );
42: }
43:
44: /**
45: * Dispatch an allowed request action.
46: *
47: * @since 3.5.26
48: * @param string $action Unslashed action name from the request.
49: * @param array $args Request data passed to the action.
50: * @return void
51: */
52: function epl_do_request_action( $action, $args ) {
53:
54: if ( ! is_user_logged_in() ) {
55: return;
56: }
57:
58: $action = sanitize_text_field( $action );
59:
60: if ( in_array( $action, epl_get_allowed_request_actions(), true ) ) {
61: do_action( 'epl_' . $action, $args );
62: }
63: }
64:
65: /**
66: * Hooks EPL front end actions, when present in the $_GET and $_POST superglobal. Each allowed
67: * epl_action present in the $_GET or $_POST is called using WordPress's do_action function.
68: * Functions triggered on init hook.
69: *
70: * @since 3.0
71: * @since 3.5.26 Only allowed actions are dispatched.
72: * @return void
73: */
74: function epl_process_actions() {
75:
76: if ( isset( $_POST['epl_action'] ) ) {
77: epl_do_request_action( wp_unslash( $_POST['epl_action'] ), $_POST ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
78: }
79:
80: if ( isset( $_GET['epl_action'] ) ) {
81: epl_do_request_action( wp_unslash( $_GET['epl_action'] ), $_GET ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
82: }
83: }
84: add_action( 'init', 'epl_process_actions' );
85:
86: /**
87: * Hooks EPL admin actions, when present in the $_GET and $_POST superglobal. Each allowed
88: * epl-action present in the $_GET or $_POST is called using WordPress's do_action function.
89: * Functions triggered on admin_init hook.
90: *
91: * @since 3.0
92: * @since 3.5.26 Only allowed actions are dispatched.
93: * @return void
94: */
95: function epl_process_admin_actions() {
96:
97: if ( isset( $_POST['epl-action'] ) ) {
98: epl_do_request_action( wp_unslash( $_POST['epl-action'] ), $_POST ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
99: }
100:
101: if ( isset( $_GET['epl-action'] ) ) {
102: epl_do_request_action( wp_unslash( $_GET['epl-action'] ), $_GET ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
103: }
104: }
105: add_action( 'admin_init', 'epl_process_admin_actions' );
106: