_className = 'dctr'; //sets all of the plugin urls so it can reach out to other places $this->_createURLs( $file ); //sets the plugin version for backwards compat checks $this->_version = $version; //initializes the languages for the plugin $this->init_i18n_textdomain(); add_action( 'init', array( $this, 'init_languages' ), 0 ); // load the scripts and styles required to function add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) ); add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) ); add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) ); add_filter( 'the_content' , array($this, 'redact_post_content') ); register_activation_hook( $this->_file, array( $this->_className, 'install' ) ); } static function install(){ global $wpdb; $charset_collate = $wpdb->get_charset_collate(); $table_name = $wpdb->prefix . "wrdctr_redactions"; $sql = "CREATE TABLE $table_name ( \n". " id int(11) NOT NULL AUTO_INCREMENT, \n". " dt_added datetime DEFAULT NOW() NOT NULL, \n". " rx_redaction varchar(500) DEFAULT '' NOT NULL, \n". " str_groups varchar(500) DEFAULT '' NOT NULL, \n". ") $charset_collate; "; require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); dbDelta( $sql ); add_option( 'wordactor_version', datasync_PHP_MINIMUM_VERSION ); } public function enqueue_styles(){ } public function enqueue_admin_styles( $hook = '' ){ } public function enqueue_scripts(){ } public function enqueue_admin_scripts( $hook = '' ){ } /** * Helper function on whether SCRIPT_DEBUG is set * * Returns whether or not SCRIPT_DEBUG is set or not * * @access private * @since 0.0.1 * @return boolean */ private function is_script_debug(){ return defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG; } /** * Create and initializes paths for the plugin * * Stores all of the paths that the plugin will use to access the world * * @access private * @since 0.0.1 * @return void */ private function _createURLs( $file ) { $this->_file = $file; $this->_dir = dirname($file); $this->_assets_dir = trailingslashit( $this->_dir ) . 'assets'; $this->_assets_url = esc_url( trailingslashit( plugins_url( '/assets/', $this->_file ) ) ); $this->_languages_dir = trailingslashit( $this->_dir ) . 'languages'; } /** * Initialize the textdomain for the plugin * * Initializes the textdomain for the plugin * * @access private * @since 0.0.1 * @return void */ public function init_i18n_textdomain () { $lang = apply_filters( 'plugin_locale', get_locale(), datasync_PLUGIN_TEXTDOMAIN ); load_textdomain( datasync_PLUGIN_TEXTDOMAIN, WP_LANG_DIR . '/' . datasync_PLUGIN_TEXTDOMAIN . '/' . datasync_PLUGIN_TEXTDOMAIN . '-' . $lang . '.mo' ); load_plugin_textdomain( datasync_PLUGIN_TEXTDOMAIN, false, $this->_languages_dir ); } /** * Initialize lanaguages * @access public * @since 0.0.1 * @return void */ public function init_languages () { load_plugin_textdomain( datasync_PLUGIN_PRIVATE_NAME, false, $this->_languages_dir ); } /** * Not allowed * * The plugin is a singleton so don't allow cloning. * * @access private * @since 0.0.1 * @return void */ final private function __clone() {} private function _getMatchingStrings($arrPatterns, $strContent){ $arrResults = array(); foreach( $arrPatterns as $pattern ){ preg_match_all("/".$pattern."/i", $strContent, $out, PREG_PATTERN_ORDER); $arrResults = array_merge($arrResults, $out[0]); } return $arrResults; } private function _getMatchingRedactsFromDatabase($strContent){ global $wpdb; $table_name = $wpdb->prefix . datasync_DATABASE_REDACT_TABLENAME; $sql = $wpdb->prepare( "select * from $table_name where %s RLIKE `rx_redaction`", $strContent ); $rows = $wpdb->get_results( $sql ); return $rows; } private function _convertToRedactStrings($arrMatches){ if(!is_string($arrMatches) && !is_array($arrMatches)){ return array(); } if(is_string( $arrMatches ) ){ $arrMatches = array($arrMatches); } $arrResult = array(); foreach($arrMatches as $strMatch){ if(!is_string($strMatch)){ $arrResult[] = ''; continue; } $arrResult[] = preg_replace("/[^\s]/", "█", $strMatch); } return $arrResult; } public function redact_post_content( $post_content ){ $rows = $this->_getMatchingRedactsFromDatabase($post_content); $rw_cnt = count( $rows ); $result_text = ''; $result_text .= " Found $rw_cnt rows
"; $strResult = $post_content; if($rw_cnt > 0){ $arrMatches = $this->_getMatchingStrings( datasync_pluckColumn('rx_redaction', $rows) , $post_content); $arrReplace = $this->_convertToRedactStrings($arrMatches); $strResult = str_replace($arrMatches, $arrReplace, $post_content); } return $strResult . $result_text; } };