| 1 |
<?php |
| 2 |
|
| 3 |
namespace WeDevs\ERP\Accounting; |
| 4 |
|
| 5 |
// don't call the file directly |
| 6 |
if ( ! defined( 'ABSPATH' ) ) { |
| 7 |
exit; |
| 8 |
} |
| 9 |
|
| 10 |
/** |
| 11 |
* Accounting class |
| 12 |
* |
| 13 |
* @class Accounting The class that holds the entire Accounting module |
| 14 |
*/ |
| 15 |
final class Accounting { |
| 16 |
|
| 17 |
/** |
| 18 |
* The main plugin instance |
| 19 |
* |
| 20 |
* @var object |
| 21 |
*/ |
| 22 |
private $plugin; |
| 23 |
|
| 24 |
/** |
| 25 |
* Holds various class instances |
| 26 |
* |
| 27 |
* @var array |
| 28 |
*/ |
| 29 |
private $container = []; |
| 30 |
|
| 31 |
/** |
| 32 |
* Constructor for the ERP_Accounting class |
| 33 |
* |
| 34 |
* Sets up all the appropriate hooks and actions |
| 35 |
* within our plugin. |
| 36 |
*/ |
| 37 |
public function __construct( $plugin ) { |
| 38 |
// prevent duplicate loading |
| 39 |
if ( did_action( 'erp_accounting_loaded' ) ) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
$this->plugin = $plugin; |
| 44 |
|
| 45 |
$this->deactive_addon(); |
| 46 |
|
| 47 |
// Define constants |
| 48 |
$this->define_constants(); |
| 49 |
|
| 50 |
// Include required files |
| 51 |
$this->includes(); |
| 52 |
|
| 53 |
// load the module |
| 54 |
add_action( 'erp_loaded', [ $this, 'plugin_init' ] ); |
| 55 |
|
| 56 |
// trigger after accounting module loaded |
| 57 |
do_action( 'erp_accounting_loaded' ); |
| 58 |
|
| 59 |
// pdf plugin is not installed notice |
| 60 |
if ( ! is_plugin_active( 'erp-pdf-invoice/wp-erp-pdf.php' ) ) { |
| 61 |
if ( 'hide' !== get_option( 'pdf-notice-dismissed' ) ) { |
| 62 |
add_action( 'admin_notices', [ $this, 'admin_pdf_notice' ] ); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
add_action( 'wp_ajax_dismiss_pdf_notice', [ $this, 'dismiss_pdf_notice' ] ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Show notice if PDF plugin is not installed |
| 71 |
* |
| 72 |
* @return void |
| 73 |
*/ |
| 74 |
public function admin_pdf_notice() { |
| 75 |
if ( current_user_can( 'install_plugins' ) ) { |
| 76 |
$action = empty( $_GET['erp-pdf'] ) ? '' : \sanitize_text_field( wp_unslash( $_GET['erp-pdf'] ) ); |
| 77 |
$plugin = 'erp-pdf-invoice/wp-erp-pdf.php'; |
| 78 |
$pdf_install = new \WeDevs\ERP\Accounting\Includes\Classes\PDF_Install(); |
| 79 |
|
| 80 |
if ( $action === 'install' ) { |
| 81 |
$pdf_install->install_plugin( 'https://downloads.wordpress.org/plugin/erp-pdf-invoice.zip' ); |
| 82 |
} elseif ( $action === 'active' ) { |
| 83 |
$pdf_install->activate_pdf_plugin( $plugin ); |
| 84 |
} |
| 85 |
|
| 86 |
if ( \file_exists( WP_PLUGIN_DIR . '/' . $plugin ) ) { |
| 87 |
if ( ! \is_plugin_active( $plugin ) ) { |
| 88 |
$this->pdf_notice_message( 'active' ); |
| 89 |
} |
| 90 |
} else { |
| 91 |
$this->pdf_notice_message( 'install' ); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* PDF notice message |
| 98 |
* |
| 99 |
* @param string $type |
| 100 |
* |
| 101 |
* @return void |
| 102 |
*/ |
| 103 |
public function pdf_notice_message( $type ) { |
| 104 |
$protocol = empty( $_SERVER['HTTPS'] ) ? 'http' : 'https'; |
| 105 |
$host = empty( $_SERVER['HTTP_HOST'] ) ? '' : sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ); |
| 106 |
$uri = empty( $_SERVER['REQUEST_URI'] ) ? '' : sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ); |
| 107 |
$sign = empty( $_GET ) ? '?' : '&'; |
| 108 |
|
| 109 |
$actual_link = esc_url( $protocol . '://' . $host . $uri ); |
| 110 |
|
| 111 |
echo '<div class="updated notice is-dismissible notice-pdf"><p>'; |
| 112 |
// translators: %1$s: type, %2$s: link, %3$s: type |
| 113 |
echo wp_kses_post( sprintf( __( 'Please %1$s <a href="%2$serp-pdf=%3$s">WP ERP PDF</a> extension to get PDF export feature.', 'erp' ), $type, $actual_link . $sign, $type ) ); |
| 114 |
echo '</p></div>'; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Dismiss PDF notice message |
| 119 |
* |
| 120 |
* @return void |
| 121 |
*/ |
| 122 |
public function dismiss_pdf_notice() { |
| 123 |
update_option( 'pdf-notice-dismissed', 'hide' ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Initializes the ERP_Accounting() class |
| 128 |
* |
| 129 |
* Checks for an existing ERP_Accounting() instance |
| 130 |
* and if it doesn't find one, creates it. |
| 131 |
*/ |
| 132 |
public static function init() { |
| 133 |
static $instance = false; |
| 134 |
|
| 135 |
if ( ! $instance ) { |
| 136 |
$instance = new self(); |
| 137 |
} |
| 138 |
|
| 139 |
return $instance; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Magic getter to bypass referencing plugin. |
| 144 |
* |
| 145 |
* @param $prop |
| 146 |
* |
| 147 |
* @return mixed |
| 148 |
*/ |
| 149 |
public function __get( $prop ) { |
| 150 |
if ( array_key_exists( $prop, $this->container ) ) { |
| 151 |
return $this->container[ $prop ]; |
| 152 |
} |
| 153 |
|
| 154 |
return $this->{$prop}; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Magic isset to bypass referencing plugin. |
| 159 |
* |
| 160 |
* @param $prop |
| 161 |
* |
| 162 |
* @return mixed |
| 163 |
*/ |
| 164 |
public function __isset( $prop ) { |
| 165 |
return isset( $this->{$prop} ) || isset( $this->container[ $prop ] ); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Define the constants |
| 170 |
* |
| 171 |
* @return void |
| 172 |
*/ |
| 173 |
public function define_constants() { |
| 174 |
define( 'ERP_ACCOUNTING_FILE', __FILE__ ); |
| 175 |
define( 'ERP_ACCOUNTING_PATH', dirname( ERP_ACCOUNTING_FILE ) ); |
| 176 |
define( 'ERP_ACCOUNTING_INCLUDES', ERP_ACCOUNTING_PATH . '/includes' ); |
| 177 |
define( 'ERP_ACCOUNTING_API', ERP_ACCOUNTING_PATH . '/api' ); |
| 178 |
define( 'ERP_ACCOUNTING_URL', plugins_url( '', ERP_ACCOUNTING_FILE ) ); |
| 179 |
define( 'ERP_ACCOUNTING_ASSETS', ERP_ACCOUNTING_URL . '/assets' ); |
| 180 |
define( 'ERP_ACCOUNTING_VIEWS', ERP_ACCOUNTING_PATH . '/includes/views' ); |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Load the plugin after all plugins are loaded |
| 185 |
* |
| 186 |
* @return void |
| 187 |
*/ |
| 188 |
public function plugin_init() { |
| 189 |
$this->includes(); |
| 190 |
$this->init_actions(); |
| 191 |
$this->init_filters(); |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Include the required files |
| 196 |
* |
| 197 |
* @return void |
| 198 |
*/ |
| 199 |
public function includes() { |
| 200 |
$this->include_functions(); |
| 201 |
$this->include_classes(); |
| 202 |
|
| 203 |
/*** Init emailer class **/ |
| 204 |
new Emailer(); |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Includes Rest API helper Functions |
| 209 |
*/ |
| 210 |
public function include_functions() { |
| 211 |
foreach ( glob( ERP_ACCOUNTING_INCLUDES . '/functions/*.php' ) as $filename ) { |
| 212 |
include_once $filename; |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Includes Classes |
| 218 |
*/ |
| 219 |
public function include_classes() { |
| 220 |
require_once ERP_ACCOUNTING_API . '/class-controller-rest-api.php'; |
| 221 |
require_once ERP_ACCOUNTING_INCLUDES . '/classes/class-assets.php'; |
| 222 |
require_once ERP_ACCOUNTING_INCLUDES . '/classes/class-ledger-map.php'; |
| 223 |
require_once ERP_ACCOUNTING_INCLUDES . '/classes/class-send-email.php'; |
| 224 |
require_once ERP_ACCOUNTING_INCLUDES . '/classes/class-user-profile.php'; |
| 225 |
|
| 226 |
if ( $this->is_request( 'admin' ) ) { |
| 227 |
require_once ERP_ACCOUNTING_INCLUDES . '/classes/class-admin.php'; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Init the plugin actions |
| 233 |
* |
| 234 |
* @return void |
| 235 |
*/ |
| 236 |
public function init_actions() { |
| 237 |
add_action( 'init', [ $this, 'init_classes' ] ); |
| 238 |
add_action( 'template_redirect', [ $this, 'readonly_invoice_template' ] ); |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Init the plugin filters |
| 243 |
* |
| 244 |
* @return void |
| 245 |
*/ |
| 246 |
public function init_filters() { |
| 247 |
add_filter( 'erp_settings_pages', [ $this, 'add_settings_page' ] ); |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* Add settings page |
| 252 |
* |
| 253 |
* @param array $settings |
| 254 |
* |
| 255 |
* @return array |
| 256 |
*/ |
| 257 |
public function add_settings_page( $settings = [] ) { |
| 258 |
$settings[] = include __DIR__ . '/includes/classes/class-settings.php'; |
| 259 |
|
| 260 |
return $settings; |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Instantiate the required classes |
| 265 |
* |
| 266 |
* @return void |
| 267 |
*/ |
| 268 |
public function init_classes() { |
| 269 |
if ( $this->is_request( 'admin' ) ) { |
| 270 |
$this->container['admin'] = new \WeDevs\ERP\Accounting\Includes\Classes\Admin(); |
| 271 |
} |
| 272 |
|
| 273 |
$this->container['rest'] = new \WeDevs\ERP\Accounting\API\REST_API(); |
| 274 |
$this->container['assets'] = new \WeDevs\ERP\Accounting\Includes\Classes\Assets(); |
| 275 |
$this->container['profile'] = new \WeDevs\ERP\Accounting\Includes\Classes\User_Profile(); |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* What type of request is this? |
| 280 |
* |
| 281 |
* @param string $type admin, ajax, cron or frontend |
| 282 |
* |
| 283 |
* @return bool |
| 284 |
*/ |
| 285 |
private function is_request( $type ) { |
| 286 |
switch ( $type ) { |
| 287 |
case 'admin': |
| 288 |
return is_admin(); |
| 289 |
|
| 290 |
case 'ajax': |
| 291 |
return defined( 'DOING_AJAX' ); |
| 292 |
|
| 293 |
case 'cron': |
| 294 |
return defined( 'DOING_CRON' ); |
| 295 |
|
| 296 |
case 'frontend': |
| 297 |
return ( ! is_admin() || defined( 'DOING_AJAX' ) ) && ! defined( 'DOING_CRON' ); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Returns true if the request is a non-legacy REST API request. |
| 303 |
* |
| 304 |
* Legacy REST requests should still run some extra code for backwards compatibility. |
| 305 |
* |
| 306 |
* @todo: replace this function once core WP function is available: https://core.trac.wordpress.org/ticket/42061. |
| 307 |
* |
| 308 |
* @return bool |
| 309 |
*/ |
| 310 |
public function is_rest_api_request() { |
| 311 |
if ( empty( $_SERVER['REQUEST_URI'] ) ) { |
| 312 |
return false; |
| 313 |
} |
| 314 |
|
| 315 |
$rest_prefix = trailingslashit( rest_get_url_prefix() ); |
| 316 |
$is_rest_api_request = ( false !== strpos( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ), $rest_prefix ) ); |
| 317 |
|
| 318 |
return apply_filters( 'wperp_acct_is_rest_api_request', $is_rest_api_request ); |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* Backward compatibility |
| 323 |
* |
| 324 |
* Check if the previous accounting add-on installed. |
| 325 |
* If found, deactivate the add-on |
| 326 |
* |
| 327 |
* @return void |
| 328 |
*/ |
| 329 |
public function deactive_addon() { |
| 330 |
/** |
| 331 |
* Detect plugin. For use on Front End only. |
| 332 |
*/ |
| 333 |
include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 334 |
|
| 335 |
// check for plugin using plugin name |
| 336 |
if ( is_plugin_active( 'accounting/accounting.php' ) ) { |
| 337 |
deactivate_plugins( 'accounting/accounting.php' ); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
/** |
| 342 |
* Callback to template_redirect hook |
| 343 |
* Shows template when invoice readonly link is called |
| 344 |
* |
| 345 |
* @return mixed |
| 346 |
*/ |
| 347 |
public function readonly_invoice_template() { |
| 348 |
$query = isset( $_REQUEST['query'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['query'] ) ) : ''; |
| 349 |
$transaction_id = isset( $_REQUEST['trans_id'] ) ? intval( $_REQUEST['trans_id'] ) : ''; |
| 350 |
$auth_id = isset( $_REQUEST['auth'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['auth'] ) ) : ''; |
| 351 |
$verified = false; |
| 352 |
|
| 353 |
if ( ! $query || ! $transaction_id || ! $auth_id ) { |
| 354 |
return; |
| 355 |
} |
| 356 |
|
| 357 |
$transaction = erp_acct_get_transaction( $transaction_id ); |
| 358 |
|
| 359 |
if ( $transaction ) { |
| 360 |
$verified = erp_acct_verify_invoice_link_hash( $transaction_id, $transaction['type'], $auth_id ); |
| 361 |
} |
| 362 |
|
| 363 |
if ( $verified ) { |
| 364 |
include ERP_ACCOUNTING_VIEWS . '/transactions/invoice-readonly.php'; |
| 365 |
exit(); |
| 366 |
} |
| 367 |
|
| 368 |
return; |
| 369 |
} |
| 370 |
} // ERP_Accounting |
| 371 |
|