400 ) ); } if ( empty( $in['targets'] ) || ! is_array( $in['targets'] ) ) { return new WP_Error( 'hidexo_targets', __( 'No element was selected.', 'hidexo-lite' ), array( 'status' => 400 ) ); } if ( count( $in['targets'] ) > self::MAX_TARGETS ) { /* translators: %d: maximum number of elements per rule. */ return new WP_Error( 'hidexo_targets', sprintf( __( 'A rule can target up to %d elements.', 'hidexo-lite' ), self::MAX_TARGETS ), array( 'status' => 400 ) ); } $targets = array(); foreach ( $in['targets'] as $raw_target ) { $clean = self::locator( $raw_target, $type ); if ( false === $clean ) { return new WP_Error( 'hidexo_locator', __( 'An element could not be identified reliably, so the rule was not saved.', 'hidexo-lite' ), array( 'status' => 400 ) ); } $targets[] = $clean; } $path = ( isset( $in['page_path'] ) && is_string( $in['page_path'] ) ) ? Hidexo_Lite_Rules::normalize_path( $in['page_path'] ) : '/'; $name = self::name( isset( $in['name'] ) ? $in['name'] : '' ); if ( '' === $name ) { $name = __( 'Hide text', 'hidexo-lite' ); } $count = count( $targets ); return array( 'name' => $name, 'type' => $type, 'scope' => self::scope( isset( $in['scope'] ) ? $in['scope'] : 'page' ), 'page_path' => $path, 'page_title' => self::cut( sanitize_text_field( isset( $in['page_title'] ) && is_scalar( $in['page_title'] ) ? (string) $in['page_title'] : '' ), 120 ), 'preview' => self::cut( sanitize_text_field( isset( $in['preview'] ) && is_scalar( $in['preview'] ) ? (string) $in['preview'] : '' ), 200 ), 'targets' => $targets, 'health' => array( 'state' => 'ok', 'resolved' => $count, 'total' => $count, 'checked' => time(), ), ); } /** * Validate a tag/id/classes signature. * * @param mixed $s Raw signature. * @return array|false */ private static function signature( $s ) { $tag = ( is_array( $s ) && isset( $s['tag'] ) && is_string( $s['tag'] ) ) ? strtolower( $s['tag'] ) : ''; if ( ! preg_match( self::TAG_RE, $tag ) ) { return false; } $id = ''; if ( ! empty( $s['id'] ) ) { if ( ! is_string( $s['id'] ) || ! preg_match( self::ID_RE, $s['id'] ) ) { return false; } $id = $s['id']; } $classes = array(); if ( ! empty( $s['classes'] ) ) { if ( ! is_array( $s['classes'] ) || count( $s['classes'] ) > 5 ) { return false; } foreach ( $s['classes'] as $c ) { if ( ! is_string( $c ) || ! preg_match( self::CLASS_RE, $c ) ) { return false; } $classes[] = $c; } } return array( 'tag' => $tag, 'id' => $id, 'classes' => $classes, ); } /** * Validate one locator. * * @param mixed $l Raw locator. * @param string $type Rule type. * @return array|false */ public static function locator( $l, $type ) { $out = self::signature( $l ); if ( false === $out ) { return false; } $out['parent'] = null; if ( ! empty( $l['parent'] ) ) { $out['parent'] = self::signature( $l['parent'] ); if ( false === $out['parent'] ) { return false; } } $out['path'] = array(); if ( ! empty( $l['path'] ) ) { if ( ! is_array( $l['path'] ) || count( $l['path'] ) > 12 ) { return false; } foreach ( $l['path'] as $step ) { $step = is_array( $step ) ? array_values( $step ) : array(); if ( 2 !== count( $step ) || ! is_string( $step[0] ) || ! preg_match( self::TAG_RE, $step[0] ) || ! is_numeric( $step[1] ) ) { return false; } $out['path'][] = array( $step[0], max( 0, min( 99999, (int) $step[1] ) ) ); } } $out['occ'] = isset( $l['occ'] ) ? max( 0, min( 9999, (int) $l['occ'] ) ) : 0; $out['occCount'] = isset( $l['occCount'] ) ? max( 1, min( 10000, (int) $l['occCount'] ) ) : 1; // Every text rule carries a fingerprint of the element's text. if ( empty( $l['fp'] ) || ! is_string( $l['fp'] ) || ! preg_match( '/^[a-z0-9]{1,16}$/', $l['fp'] ) ) { return false; } $out['fp'] = $l['fp']; $out['len'] = isset( $l['len'] ) ? max( 0, min( 1000000, (int) $l['len'] ) ) : 0; // Optional: hide only some words inside the element instead of the whole element. if ( isset( $l['part'] ) ) { $part = $l['part']; if ( ! is_array( $part ) || ! isset( $part['t'] ) || ! is_string( $part['t'] ) ) { return false; } $words = trim( preg_replace( '/\s+/u', ' ', wp_check_invalid_utf8( $part['t'] ) ) ); if ( '' === $words || strlen( $words ) > 1200 || ( function_exists( 'mb_strlen' ) && mb_strlen( $words ) > 300 ) ) { return false; } $out['part'] = array( 't' => $words, 'i' => isset( $part['i'] ) ? max( -1, min( 9999, (int) $part['i'] ) ) : -1, ); } return $out; } }