| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 4 |
|
| 5 |
/** |
| 6 |
* Controller class |
| 7 |
* |
| 8 |
* @author Flipper Code<hello@flippercode.com> |
| 9 |
* @version 3.0.0 |
| 10 |
* @package Core |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! class_exists( 'Flippercode_Core_Controller' ) ) { |
| 14 |
|
| 15 |
/** |
| 16 |
* Controller class to display views. |
| 17 |
* |
| 18 |
* @author Flipper Code<hello@flippercode.com> |
| 19 |
* @version 3.0.0 |
| 20 |
* @package Core |
| 21 |
*/ |
| 22 |
class Flippercode_Core_Controller { |
| 23 |
|
| 24 |
/** |
| 25 |
* Store object type |
| 26 |
* |
| 27 |
* @var String |
| 28 |
*/ |
| 29 |
private $entity; |
| 30 |
/** |
| 31 |
* Store entity object return by factory |
| 32 |
* |
| 33 |
* @var Object |
| 34 |
*/ |
| 35 |
private $entityObj; |
| 36 |
/** |
| 37 |
* Store properties of the $entity object. |
| 38 |
* |
| 39 |
* @var Array |
| 40 |
*/ |
| 41 |
private $entityObjProperties; |
| 42 |
/** |
| 43 |
* Store path to modules |
| 44 |
* |
| 45 |
* @var String |
| 46 |
*/ |
| 47 |
private $modulePath; |
| 48 |
/** |
| 49 |
* Store current module prefix according to plugin for frontend prefix needs. |
| 50 |
* |
| 51 |
* @var String |
| 52 |
*/ |
| 53 |
private $modulePrefix; |
| 54 |
/** |
| 55 |
* Store plugin's text domain. |
| 56 |
* |
| 57 |
* @var String |
| 58 |
*/ |
| 59 |
private $textDomain; |
| 60 |
|
| 61 |
|
| 62 |
private $pluginInstance = null; |
| 63 |
|
| 64 |
function __construct( $objectType, $module_path, $modulePrefix = '', $plugin_controller_obj = '' ) { |
| 65 |
|
| 66 |
$this->entity = $objectType; |
| 67 |
$this->modulePath = $module_path; |
| 68 |
$this->modulePrefix = $modulePrefix; |
| 69 |
if ( ! empty( $plugin_controller_obj ) ) { |
| 70 |
$this->pluginInstance = $plugin_controller_obj; |
| 71 |
} |
| 72 |
|
| 73 |
if ( file_exists( $this->modulePath . $this->entity . '/model.' . $this->entity . '.php' ) ) { |
| 74 |
$factoryObject = new Flippercode_Factory_Model( $module_path, $modulePrefix ); |
| 75 |
$this->entityObj = $factoryObject->create_object( $this->entity ); |
| 76 |
if ( is_object( $this->entityObj ) ) { |
| 77 |
$this->entityObjProperties = get_object_vars( $this->entityObj ); |
| 78 |
} |
| 79 |
} else { |
| 80 |
$object_name = $modulePrefix . ucwords( $this->entity ); |
| 81 |
if ( is_object( new $object_name() ) ) { |
| 82 |
$this->entityObj = new $object_name(); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
} |
| 87 |
/** |
| 88 |
* Load requested views. |
| 89 |
* |
| 90 |
* @param String $view View name. |
| 91 |
* @param array $options View Options. |
| 92 |
*/ |
| 93 |
public function display( $view, $options = array() ) { |
| 94 |
|
| 95 |
$this->entity = apply_filters( 'fc_plugin_module_to_load', $this->entity ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound |
| 96 |
|
| 97 |
if ( isset( $this->pluginInstance ) && $this->pluginInstance->needs_license_verification() ) { |
| 98 |
if ( $this->entity == 'debug' ) { |
| 99 |
$view = 'form'; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
$response = $this->do_action(); |
| 104 |
|
| 105 |
switch ( $view ) { |
| 106 |
default: |
| 107 |
$view = basename( $view ) . '.php'; |
| 108 |
} |
| 109 |
|
| 110 |
$this->modulePath = apply_filters('fc_backend_module_path', $this->modulePath,$this->entity, $view ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound |
| 111 |
|
| 112 |
|
| 113 |
if ( ! empty( $view ) ) { |
| 114 |
if ( file_exists( $this->modulePath . "{$this->entity}/views/" . $view ) ) { |
| 115 |
return include $this->modulePath . "{$this->entity}/views/" . $view; |
| 116 |
} else { |
| 117 |
if ( is_object( $this->entityObj ) ) { |
| 118 |
return $this->entityObj->display( $this->entity, $view, $response ); // Extension Object. |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
/** |
| 124 |
* Return entity name. |
| 125 |
* |
| 126 |
* @return String Type of entity. |
| 127 |
*/ |
| 128 |
protected function get_entity() { |
| 129 |
return $this->entity;} |
| 130 |
/** |
| 131 |
* Handle form submissions |
| 132 |
* |
| 133 |
* @param string $action Action name. |
| 134 |
* @return [type] Success or Failure response. |
| 135 |
*/ |
| 136 |
protected function do_action( $action = '' ) { |
| 137 |
|
| 138 |
global $wpdb; |
| 139 |
|
| 140 |
$response = array(); |
| 141 |
|
| 142 |
try { |
| 143 |
if ( isset( $_POST['operation'] ) and sanitize_text_field( wp_unslash( $_POST['operation'] ) ) != '' ) { |
| 144 |
$operation = sanitize_text_field( wp_unslash( $_POST['operation'] ) ); |
| 145 |
if ( method_exists( $this->entityObj, $operation ) ) { |
| 146 |
$response = $this->entityObj->$operation(); |
| 147 |
} |
| 148 |
} |
| 149 |
} catch ( Exception $e ) { |
| 150 |
$response['error'] = $e->getMessage(); |
| 151 |
} |
| 152 |
|
| 153 |
return $response; |
| 154 |
} |
| 155 |
/** |
| 156 |
* Handle Add & Edit operations. |
| 157 |
* |
| 158 |
* @return Array Success or Failure response. |
| 159 |
*/ |
| 160 |
protected function action_add_edit() { |
| 161 |
|
| 162 |
if ( isset( $_REQUEST['_wpnonce'] ) ) { |
| 163 |
$nonce = sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ); } |
| 164 |
|
| 165 |
if ( ! wp_verify_nonce( $nonce, 'wpgmp-nonce' ) ) { |
| 166 |
|
| 167 |
die( 'Cheating...' ); |
| 168 |
|
| 169 |
} |
| 170 |
|
| 171 |
$response = array(); |
| 172 |
// Ignore changes in these class variables while setting up class object for insertion/updation. |
| 173 |
$properties_to_ignore = array( 'validations', 'table', 'unique' ); |
| 174 |
|
| 175 |
foreach ( $properties_to_ignore as $classproperty ) { |
| 176 |
|
| 177 |
if ( array_key_exists( $classproperty, $this->entityObjProperties ) ) { |
| 178 |
unset( $this->entityObjProperties[ $classproperty ] ); } |
| 179 |
} |
| 180 |
|
| 181 |
if ( is_object( $this->entityObjProperties ) ) { |
| 182 |
|
| 183 |
foreach ( $this->entityObjProperties as $key => $val ) { |
| 184 |
|
| 185 |
if ( isset( $_POST[ $key ] ) and ! is_array( $_POST[ $key ] ) ) { |
| 186 |
$post_key = sanitize_text_field( wp_unslash( $_POST[ $key ] ) ); |
| 187 |
} else { |
| 188 |
$post_key = array_map( 'esc_attr', (array) wp_unslash( $_POST[ $key ] ) ); |
| 189 |
|
| 190 |
} |
| 191 |
|
| 192 |
if ( isset( $key ) && isset( $post_key ) && is_object( $this->entityObj ) ) { |
| 193 |
$this->entityObj->set_val( $key, $post_key ); |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
if ( isset( $_POST['entityID'] ) ) { |
| 199 |
// Setting value of Id field in case of edit. |
| 200 |
$this->entityObj->set_val( $this->entity . '_id', intval( wp_unslash( $_POST['entityID'] ) ) ); } |
| 201 |
|
| 202 |
if ( $this->entityObj->save() > 0 ) { |
| 203 |
|
| 204 |
$current_obj_name = ucfirst( $this->entity ); |
| 205 |
$_POST = array(); |
| 206 |
} |
| 207 |
|
| 208 |
return $response; |
| 209 |
} |
| 210 |
/** |
| 211 |
* Handle import locations action. |
| 212 |
* |
| 213 |
* @return Array Success or Failure Information. |
| 214 |
*/ |
| 215 |
public function action_import_location() { |
| 216 |
$response = $this->entityObj->import_location(); |
| 217 |
return $response; } |
| 218 |
/** |
| 219 |
* Handle Backup action. |
| 220 |
* |
| 221 |
* @return Array Success or Failure Information. |
| 222 |
*/ |
| 223 |
public function action_take_backup() { |
| 224 |
$response = $this->entityObj->take_backup(); |
| 225 |
return $response; } |
| 226 |
/** |
| 227 |
* Handle upload backup action. |
| 228 |
* |
| 229 |
* @return Array Success or Failure Information. |
| 230 |
*/ |
| 231 |
public function action_upload_backup() { |
| 232 |
$response = $this->entityObj->upload_backup(); |
| 233 |
return $response; } |
| 234 |
/** |
| 235 |
* Handle import backup action. |
| 236 |
* |
| 237 |
* @return Array Success or Failure Information. |
| 238 |
*/ |
| 239 |
public function action_import_backup() { |
| 240 |
$response = $this->entityObj->import_backup(); |
| 241 |
return $response; } |
| 242 |
|
| 243 |
} |
| 244 |
} |
| 245 |
|