. */ require_once('AddActionsAndFilters_ShortCode.php'); class AddActionsAndFilters_Executor { /** * @var AddActionsAndFilters_Plugin */ var $plugin; /** * @var array used as a temporary variable during code execution */ var $codeItem; public function __construct($plugin) { $this->plugin = $plugin; } /** * @param bool $is_admin code to be executed on an admin (dashboard) page * @return array|null|object */ public function getCodeItemsToExecute($is_admin) { global $wpdb; $this->plugin->ensureDatabaseTableInstalled(); // ensure created in multisite $table = $this->plugin->getTableName(); $sql = "select * from $table where enabled = 1"; if ($is_admin) { $sql .= " and inadmin = 1"; } $sql .= " order by id"; return $wpdb->get_results($sql, ARRAY_A); } /** * @param $codeItems array output from getCodeItemsToExecute() */ public function executeCodeItems($codeItems) { register_shutdown_function(array(&$this, 'fatalErrorHandler')); $php7Plus = $this->isPhp7OrLater(); foreach ($codeItems as $this->codeItem) { if ($this->codeItem['shortcode']) { $sc = new AddActionsAndFilters_ShortCode($this->plugin, $this->codeItem); $sc->register_shortcode(); } else { // Execute PHP Code if ($php7Plus) { try { $result = eval($this->codeItem['code']); if ($result === FALSE) { $this->printErrorMessage($this->codeItem); } } catch (Throwable $ex) { // Throwable only in PHP 7+ $this->printErrorMessage($this->codeItem, $ex->getMessage()); } } else { // Prior to PHP 7, does not throw Exception that can be caught $result = eval($this->codeItem['code']); if ($result === FALSE) { $this->printErrorMessage($this->codeItem); } } } } $this->codeItem = null; } public function fatalErrorHandler() { if ($this->codeItem) { $this->printErrorMessage($this->codeItem); } } /** * @param $codeItem array */ public function printErrorMessage($codeItem, $msg = null) { echo '
'; echo '

 

'; // helps prevent header from covering first line of output $url = $this->plugin->getAdminPageUrl() . "&id={$codeItem['id']}&action=edit"; $name = $codeItem['name'] ? $codeItem['name'] : '(unamed)'; echo '

'; printf("%s Plugin: Error in user-provided code item named \"%s\". ", $this->plugin->getPluginDisplayName(), $name); echo '
'; if ($msg) { echo $msg; echo '
'; } printf("%s", $url, __('Fix the code here', 'add-actions-and-filters')); echo '

'; echo '

 

'; echo '
'; } /** * Is the running PHP Version 7.0 or later? * @return bool */ public function isPhp7OrLater() { $phpVersion = phpversion(); return version_compare($phpVersion, '7.0', '>='); } }