Advertisement
Guenni007

avia-special-characters-plugin

Mar 22nd, 2022 (edited)
1,644
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.06 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Avia Special Character Converter Plugin
  4. Plugin URI: www.kriesi.at
  5. Description: Replaces special characters that break layout or Enfold Advanced Layout Editor
  6. Version: 1.1.3
  7. Author: Guenter for www.kriesi.at
  8. Author URI: www.kriesi.at
  9. Text Domain: avia_special_characters
  10.  
  11. @requires:  PHP 5.3   (anonymous functions)
  12. @requires:  WP 4.7
  13. */
  14.  
  15. /*
  16.  * Copyright 2018
  17. */
  18.  
  19. if ( ! defined( 'ABSPATH' ) ) {   exit;  } // Exit if accessed directly
  20.  
  21.  
  22. if( ! class_exists( 'avia_special_characters' ) )
  23. {
  24.  
  25.     class avia_special_characters
  26.     {
  27.         /**
  28.          * Holds the instance of this class
  29.          *
  30.          * @since 1.1
  31.          * @var avia_special_characters
  32.          */
  33.         static private $_instance = null;
  34.  
  35.         /**
  36.          *
  37.          * @since 1.0.0
  38.          * @var array
  39.          */
  40.         protected $translate;
  41.  
  42.         /**
  43.          * Return the instance of this class
  44.          *
  45.          * @since 1.1
  46.          * @return avia_special_characters
  47.          */
  48.         static public function instance()
  49.         {
  50.             if( is_null( avia_special_characters::$_instance ) )
  51.             {
  52.                 avia_special_characters::$_instance = new avia_special_characters();
  53.             }
  54.  
  55.             return avia_special_characters::$_instance;
  56.         }
  57.  
  58.         /**
  59.          *
  60.          * @since 1.0.0
  61.          */
  62.         protected function __construct()
  63.         {
  64.             $this->translate = array(
  65.                 '#lt#'          => '<',
  66.                 '#le#'          => '≤',
  67.                 '#gt#'          => '>',
  68.                 '#ge#'          => '≥',
  69.                 '#amp#'         => '&',
  70.                 '#lbrack#'      => '[',
  71.                 '#rbrack#'      => ']',
  72.                 '#lbrace#'      => '{',
  73.                 '#rbrace#'      => '}',
  74.                 '#bigSZ#'       => '&#7838;',   // german big Letter of ß
  75.                 '#quot#'        => '"',
  76.                 '#34#'          => "'",
  77.                 '#bdquo#'       => "„",
  78.                 '#rdquo#'       => "”",
  79.                 '#ctemp#'       => '&#8451;',   // Degree in Celcius
  80.                 '#ftemp#'       => '&#8457;',   // Degree in Fahrenheit
  81.                 '#copy#'        => '&#169;',    // Copyright
  82.                 '#reg#'         => '&#174;',    // Registermark
  83.                 '#trade#'       => '&#8482;',   // Trademark
  84.                 '#br#'          => '<br/>',     // linebreak
  85.                 '#shy#'         => '&#173;',    // Softhyphen
  86.                 '#nbsp#'        => '&#160;',    // non breaking spaces
  87.                 '#sbsp#'        => '&#8239;',   // small non breaking spaces
  88.                 '#nbhyp#'       => '&#8209;',   // non breaking hyphen
  89.                 '#ndash#'       => '&#8211;',   // dash with n-letter width
  90.                 '#mdash#'       => '&#8212;',   // dash with m-letter width
  91.                 '#horbar#'      => '&#8213;',   // horizontal bar
  92.                 '#1/2#'         => '&#189;',
  93.                 '#1/3#'         => '&#8531;',
  94.                 '#2/3#'         => '&#8532;',
  95.                 '#1/4#'         => '&#188;',
  96.                 '#3/4#'         => '&#190;',
  97.                 '#1/5#'         => '&#8533;',
  98.                 '#2/5#'         => '&#8534;',
  99.                 '#3/5#'         => '&#8535;',
  100.                 '#4/5#'         => '&#8536;',
  101.             );
  102.  
  103.             add_filter( 'the_content', array( $this, 'handler_the_content' ), 9999999, 1 );
  104.             add_filter( 'avf_text_to_preview', array( $this, 'handler_the_content' ), 9999999, 1 );
  105.  
  106.             /**
  107.              * Allows to translate special characters also in ALB codeblock element
  108.              * (suggested by Guenni007)
  109.              *
  110.              * @since 1.1.3
  111.              */
  112.             add_filter( 'avf_template_builder_content', array( $this, 'handler_the_content' ), 9999999, 1 );
  113.  
  114.             /**
  115.              * Allow shortcode attributes to translate special characters (e.g. for attribute content used in js)
  116.              *
  117.              * @since 1.1.2
  118.              */
  119.             add_filter( 'avf_sc_attr_value', array( $this, 'handler_the_content' ), 9999999, 1 );
  120.  
  121.             /**
  122.              * @since 1.1
  123.              */
  124.             add_filter( 'avf_form_subject', array( $this, 'handler_the_content' ), 9999999, 1 );
  125.             add_filter( 'avf_form_mail_form_field', array( $this, 'handler_the_content' ), 9999999, 1 );
  126.             add_filter( 'avf_contact_form_autoresponder_mail', array( $this, 'handler_avf_autoresponder_mail' ), 9999999, 4 );
  127.         }
  128.  
  129.         /**
  130.          *
  131.          * @since 1.0.0
  132.          */
  133.         public function __destruct()
  134.         {
  135.             unset( $this->translate );
  136.         }
  137.  
  138.         /**
  139.          * Replace the special characters
  140.          *
  141.          * @since 1.0.0
  142.          * @param string $content
  143.          * @return string
  144.          */
  145.         public function handler_the_content( $content )
  146.         {
  147.             /**
  148.              * Add additional special characters to translate
  149.              *
  150.              * @since 1.0.0
  151.              * @param array $this->translate
  152.              * @return array
  153.              */
  154.             $this->translate = apply_filters( 'avia_special_characters_translations', $this->translate );
  155.  
  156.             $search = array_keys( $this->translate );
  157.             $replace = array_values( $this->translate );
  158.  
  159.             $new_content = str_replace( $search, $replace, $content );
  160.  
  161.             return $new_content;
  162.         }
  163.  
  164.         /**
  165.          *
  166.          * @since 1.1.1
  167.          * @param array $mail_array
  168.          * @param array $new_post
  169.          * @param array $form_params
  170.          * @param avia_form $form
  171.          * @return array
  172.          */
  173.         public function handler_avf_autoresponder_mail( array $mail_array, array $new_post, array $form_params, avia_form $form )
  174.         {
  175.             $check = [ 'Subject', 'Message' ];
  176.  
  177.             foreach( $check as $key )
  178.             {
  179.                 if( isset( $mail_array[ $key ] ) )
  180.                 {
  181.                     $mail_array[ $key ] = $this->handler_the_content( $mail_array[ $key ] );
  182.                 }
  183.             }
  184.  
  185.             return $mail_array;
  186.         }
  187.     }
  188.  
  189.     /**
  190.      * Returns the main instance of avia_special_characters to prevent the need to use globals
  191.      *
  192.      * @since 1.1
  193.      * @return avia_special_characters
  194.      */
  195.     function AviaSpecialCharacters()
  196.     {
  197.         return avia_special_characters::instance();
  198.     }
  199.  
  200.     AviaSpecialCharacters();
  201.  
  202. }   //  class exists
  203.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement