did_action( 'bricks/frontend/after_render_data' ); return $in_tree ? $html : self::note( $html ); } /** * Note what one piece of rendered HTML uses. Never changes it. * * @param string $html HTML. * @return string The same HTML. */ public static function note( $html ) { if ( ! is_string( $html ) || false === stripos( $html, 'data-tf-' ) ) { return $html; } // The name as an attribute of an opening tag (quoted values skipped whole), so echoed visitor text such as a search does not count; a raw < ends the scan, so crafted markup stays linear; a PCRE error loads it as before. // An attribute follows a space, or comes right after a quoted value (class="a"data-tf-copy: a parse error browsers accept); names hold no quote, so a quote before one closes a value. // shortcut: markup written inside script or textarea text counts too (it only loads the small script); fine as long as nothing worse follows from a match. if ( 0 !== preg_match( '/<[a-z][^\s\/<>"\']*(?:(?:\s+|(?<=["\']))[^\s"\'<>=]+(?:\s*=\s*(?:"[^"]*"|\'[^\']*\'|[^\s"\'<>]+))?)*?(?:\s+|(?<=["\']))data-tf-(?:copy|tooltip|filter)/i', $html ) ) { self::$script = true; } return $html; } /** * Load the script (with its strings), only when the page uses it. * * @return void */ public static function footer() { if ( self::$script ) { wp_enqueue_script( self::HANDLE, TOKENFORGE_URL . 'assets/front/attrs.js', array(), TOKENFORGE_VERSION, array( 'in_footer' => true, 'strategy' => 'defer' ) ); $strings = array( 'copied' => __( 'Copied', 'tokenforge' ), 'copyFailed' => __( 'Could not copy', 'tokenforge' ), ); wp_add_inline_script( self::HANDLE, 'var tokenforgeAttrs = ' . wp_json_encode( $strings, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT ) . ';', 'before' ); } } }