errors ) ) { return $notices; } return array_merge( $notices, $this->errors ); } public function maybe_perform_test( $can_save, $content ) { // If another addon has determined the file can not be saved, bail early. if ( ! $can_save ) { return $can_save; } // Do not perform a smart scan if the option for it is disabled. if ( ! isset( $_POST['string-locator-smart-edit'] ) ) { return $can_save; } return $this->run( $content ); } public function print_checks_option() { ?>
errors; } /** * Main test runner. * * @param string $content The content to scan. * * @return bool */ public function run( $content ) { $this->content = $content; // Reset the stored errors for a fresh run. $this->errors = array(); $this->check_braces(); $this->check_brackets(); $this->check_parenthesis(); if ( ! empty( $this->errors ) ) { return false; } return true; } private function check_braces() { $open_brace = substr_count( $this->content, '{' ); $close_brace = substr_count( $this->content, '}' ); if ( $open_brace !== $close_brace ) { $opened = $this->compare( '{', '}' ); foreach ( $opened as $line ) { $this->errors[] = array( 'type' => 'error', 'message' => sprintf( // translators: 1: Line number with an error. __( 'There is an inconsistency in the opening and closing braces, { and }, of your file on line %s', 'string-locator' ), '' . ( $line + 1 ) . '' ), ); } return false; } return true; } private function check_brackets() { $open_bracket = substr_count( $this->content, '[' ); $close_bracket = substr_count( $this->content, ']' ); if ( $open_bracket !== $close_bracket ) { $opened = $this->compare( '[', ']' ); foreach ( $opened as $line ) { $this->errors[] = array( 'type' => 'error', 'message' => sprintf( // translators: 1: Line number with an error. __( 'There is an inconsistency in the opening and closing braces, [ and ], of your file on line %s', 'string-locator' ), '' . ( $line + 1 ) . '' ), ); } return false; } return true; } private function check_parenthesis() { $open_parenthesis = substr_count( $this->content, '(' ); $close_parenthesis = substr_count( $this->content, ')' ); if ( $open_parenthesis !== $close_parenthesis ) { $this->failed_edit = true; $opened = $this->compare( '(', ')' ); foreach ( $opened as $line ) { $this->errors[] = array( 'type' => 'error', 'message' => sprintf( // translators: 1: Line number with an error. __( 'There is an inconsistency in the opening and closing braces, ( and ), of your file on line %s', 'string-locator' ), '' . ( $line + 1 ) . '' ), ); } return false; } return true; } /** * Check for inconsistencies in brackets and similar. * * @param string $start Start delimited. * @param string $end End delimiter. * * @return array */ function compare( $start, $end ) { $opened = array(); $lines = explode( "\n", $this->content ); for ( $i = 0; $i < count( $lines ); $i ++ ) { if ( stristr( $lines[ $i ], $start ) ) { $opened[] = $i; } if ( stristr( $lines[ $i ], $end ) ) { array_pop( $opened ); } } return $opened; } } new Smart_Scan();