Created
March 20, 2021 14:01
-
-
Save kagg-design/3c16d3f574b692c61e1678d848728398 to your computer and use it in GitHub Desktop.
MU Plugin to block transliteration of WooCommerce attributes and attribute terms.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* MU Plugin to block transliteration of WooCommerce attributes and attribute terms. | |
* | |
* @package cyr-to-lat | |
*/ | |
/** | |
* Class Block_WC_Attr_Cyr2lat. | |
*/ | |
class Block_WC_Attr_Cyr2lat { | |
/** | |
* Block sanitize title in cyr2lat plugin. | |
* | |
* @var bool | |
*/ | |
private $block_cyr2lat_sanitize = false; | |
/** | |
* WC_Attr_Cyr2lat constructor. | |
*/ | |
public function __construct() { | |
add_action( 'plugins_loaded', [ $this, 'init_hooks' ] ); | |
} | |
/** | |
* Init hooks. | |
*/ | |
public function init_hooks() { | |
if ( ! function_exists( 'WC' ) ) { | |
return; | |
} | |
add_filter( 'sanitize_taxonomy_name', [ $this, 'sanitize_taxonomy_name_filter' ], - PHP_INT_MAX, 2 ); | |
add_filter( 'pre_term_slug', [ $this, 'pre_term_filter' ], - PHP_INT_MAX, 2 ); | |
add_filter( 'pre_insert_term', [ $this, 'pre_term_filter' ], - PHP_INT_MAX, 2 ); | |
add_filter( 'ctl_pre_sanitize_title', [ $this, 'ctl_pre_sanitize_title_filter' ], - PHP_INT_MAX, 2 ); | |
} | |
/** | |
* Sanitize taxonomy name. | |
* | |
* @param string $sanitized Sanitized taxonomy name. | |
* @param string $taxonomy Taxonomy name. | |
* | |
* @return string | |
*/ | |
public function sanitize_taxonomy_name_filter( $sanitized, $taxonomy ) { | |
if ( | |
doing_filter( 'product_page_product_attributes' ) || | |
$this->is_called_from( 'wc_create_attribute' ) | |
) { | |
$this->block_cyr2lat_sanitize = true; | |
$sanitized = urldecode( sanitize_title( urldecode( $taxonomy ) ) ); | |
} | |
return $sanitized; | |
} | |
/** | |
* Filters a term field value before it is sanitized. | |
* | |
* @param mixed $value Value of the term field. | |
* @param string $taxonomy Taxonomy slug. | |
* | |
* @return mixed | |
*/ | |
public function pre_term_filter( $value, $taxonomy ) { | |
if ( 0 === strpos( $taxonomy, 'pa_' ) ) { | |
$this->block_cyr2lat_sanitize = true; | |
} | |
return $value; | |
} | |
/** | |
* Filters title before sanitization in cyr2lat. | |
* | |
* @param false|string $pre Preset to false. | |
* @param string $title Title. | |
* | |
* @return false|string | |
*/ | |
public function ctl_pre_sanitize_title_filter( $pre, $title ) { | |
if ( $this->block_cyr2lat_sanitize ) { | |
return $title; | |
} | |
return $pre; | |
} | |
/** | |
* Check if the code was called from a specific function. | |
* | |
* @param string $function_name Function name. | |
* | |
* @return bool | |
*/ | |
private function is_called_from( $function_name ) { | |
$is_called_from = false; | |
// phpcs:disable WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace | |
$backtrace = debug_backtrace( ~DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS ); | |
// phpcs:enable | |
foreach ( $backtrace as $backtrace_entry ) { | |
if ( $function_name === $backtrace_entry['function'] ) { | |
$is_called_from = true; | |
break; | |
} | |
} | |
return $is_called_from; | |
} | |
} | |
new Block_WC_Attr_Cyr2lat(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment