| 1 |
<?php |
| 2 |
/** |
| 3 |
* Setup message asking for review. |
| 4 |
* |
| 5 |
* @author UpStream |
| 6 |
* @category Admin |
| 7 |
* @package UpStream/Admin |
| 8 |
* @version 1.0.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
// Exit if accessed directly or already defined. |
| 12 |
if ( ! defined( 'ABSPATH' ) || class_exists( 'UpStream_Report_Generator' ) ) { |
| 13 |
return; |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Class UpStream_Report |
| 18 |
*/ |
| 19 |
class UpStream_Report_Generator { |
| 20 |
|
| 21 |
/** |
| 22 |
* Instance |
| 23 |
* |
| 24 |
* @var mixed |
| 25 |
*/ |
| 26 |
protected static $instance; |
| 27 |
|
| 28 |
/** |
| 29 |
* Reports |
| 30 |
* |
| 31 |
* @var array |
| 32 |
*/ |
| 33 |
public $reports = array(); |
| 34 |
|
| 35 |
/** |
| 36 |
* UpStream_Report_Generator constructor. |
| 37 |
*/ |
| 38 |
public function __construct() { |
| 39 |
if ( class_exists( 'UpStream_Report' ) ) { |
| 40 |
$this->reports[] = new UpStream_Report_Projects(); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Get All Reports |
| 46 |
*/ |
| 47 |
public function get_all_reports() { |
| 48 |
return $this->reports; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Get Report |
| 53 |
* |
| 54 |
* @param mixed $id Id. |
| 55 |
*/ |
| 56 |
public function get_report( $id ) { |
| 57 |
$reports = $this->get_all_reports(); |
| 58 |
|
| 59 |
foreach ( $reports as $r ) { |
| 60 |
if ( $r->id === $id ) { |
| 61 |
return $r; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
return null; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Get Report Fields From Post |
| 70 |
* |
| 71 |
* @param mixed $remove Remove. |
| 72 |
*/ |
| 73 |
public function get_report_fields_from_post( $remove ) { |
| 74 |
$post_data = isset( $_POST ) ? wp_unslash( $_POST ) : array(); |
| 75 |
$nonce_passed = false; |
| 76 |
$nonce_keys = array( |
| 77 |
'nonce' => 'upstream-nonce', |
| 78 |
'upstream_report_form_nonce' => 'upstream_report_form', |
| 79 |
); |
| 80 |
|
| 81 |
// check multiple nonce possibility. |
| 82 |
foreach ( $nonce_keys as $key => $value ) { |
| 83 |
if ( ! isset( $post_data[ $key ] ) ) { |
| 84 |
continue; |
| 85 |
} |
| 86 |
|
| 87 |
if ( wp_verify_nonce( $post_data[ $key ], $value ) ) { |
| 88 |
$nonce_passed = true; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
// bail out on invalid nonce. |
| 93 |
if ( ! $nonce_passed ) { |
| 94 |
return array(); |
| 95 |
} |
| 96 |
|
| 97 |
$report_fields = array(); |
| 98 |
|
| 99 |
foreach ( $post_data as $key => $value ) { // key and value are totally sanitized in the following lines. |
| 100 |
|
| 101 |
$key = sanitize_text_field( $key ); |
| 102 |
|
| 103 |
// value is totally sanitized after this. |
| 104 |
if ( is_array( $value ) ) { |
| 105 |
$v = array(); |
| 106 |
foreach ( $value as $itm ) { |
| 107 |
$v[] = sanitize_text_field( $itm ); |
| 108 |
} |
| 109 |
$value = $v; |
| 110 |
} else { |
| 111 |
$value = sanitize_text_field( $value ); |
| 112 |
} |
| 113 |
|
| 114 |
if ( stristr( $key, 'upstream_report__' ) ) { |
| 115 |
if ( $remove ) { |
| 116 |
$report_fields[ str_replace( 'upstream_report__', '', $key ) ] = $value; |
| 117 |
} else { |
| 118 |
$report_fields[ $key ] = $value; |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
return $report_fields; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Execute Report |
| 128 |
* |
| 129 |
* @param mixed $report Report. |
| 130 |
*/ |
| 131 |
public function execute_report( $report ) { |
| 132 |
$data = $report->executeReport( $this->get_report_fields_from_post( true ) ); |
| 133 |
return $data; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Get Instance |
| 138 |
*/ |
| 139 |
public static function get_instance() { |
| 140 |
if ( empty( static::$instance ) ) { |
| 141 |
$instance = new self(); |
| 142 |
static::$instance = $instance; |
| 143 |
} |
| 144 |
|
| 145 |
return static::$instance; |
| 146 |
} |
| 147 |
|
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Upstream Register Report |
| 152 |
* |
| 153 |
* @param mixed $r R. |
| 154 |
* @param mixed $display Display. |
| 155 |
* @return void |
| 156 |
*/ |
| 157 |
function upstream_register_report( $r, $display = false ) { |
| 158 |
if ( $display ) { |
| 159 |
\UpStream_Report_Generator::get_instance()->reports[] = $r; |
| 160 |
} |
| 161 |
} |
| 162 |
|