is->messages[] = $message; } /** * Get error count. * * @since 1.0.0 * @return int error message count */ public function error_count() { return count( $this->errors ); } /** * Gets the warning message count. * * @since 5.1.0 * * @return int warning message count */ public function warning_count() { return count( $this->warnings ); } /** * Gets the info message count. * * @since 5.1.0 * * @return int info message count */ public function info_count() { return count( $this->infos ); } /** * Get message count. * * @since 1.0.0 * @return int message count */ public function message_count() { return count( $this->messages ); } /** * Get error messages * * @since 1.0.0 * @return array of error message strings */ public function get_errors() { return $this->errors; } /** * Get an error message * * @since 1.0.0 * @param int $index the error index * @return string the error message */ public function get_error( $index ) { return isset( $this->errors[ $index ] ) ? $this->errors[ $index ] : ''; } /** * Gets all warning messages. * * @since 5.1.0 * * @return array */ public function get_warnings() { return $this->warnings; } /** * Gets a specific warning message. * * @since 5.1.0 * * @param int $index warning message index * @return string */ public function get_warning( $index ) { return isset( $this->warnings[ $index ] ) ? $this->warnings[ $index ] : ''; } /** * Gets all info messages. * * @since 5.1.0 * * @return array */ public function get_infos() { return $this->infos; } /** * Gets a specific info message. * * @since 5.0.0 * * @param int $index info message index * @return string */ public function get_info( $index ) { return isset( $this->infos[ $index ] ) ? $this->infos[ $index ] : ''; } /** * Get messages * * @since 1.0.0 * @return array of message strings */ public function get_messages() { return $this->messages; } /** * Get a message * * @since 1.0.0 * @param int $index the message index * @return string the message */ public function get_message( $index ) { return isset( $this->messages[ $index ] ) ? $this->messages[ $index ] : ''; } /** * Render the errors and messages. * * @since 1.0.0 * @param array $params { * Optional parameters. * * @type array $capabilities Any user capabilities to check if the user is allowed to view the messages, * default: `manage_woocommerce` * } */ public function show_messages( $params = [] ) { $params = wp_parse_args( $params, array( 'capabilities' => array( 'manage_woocommerce', ), ) ); $check_user_capabilities = []; // check if user has at least one capability that allows to see messages foreach ( $params['capabilities'] as $capability ) { $check_user_capabilities[] = current_user_can( $capability ); } // bail out if user has no minimum capabilities to see messages if ( ! in_array( true, $check_user_capabilities, true ) ) { return; } $output = ''; if ( $this->error_count() > 0 ) { $output .= '
'; } if ( $this->warning_count() > 0 ) { $output .= '
'; } if ( $this->info_count() > 0 ) { $output .= '
'; } if ( $this->message_count() > 0 ) { $output .= '
'; } echo wp_kses_post( $output ); } /** * Redirection hook which persists messages into session data. * * @since 1.0.0 * @param string $location the URL to redirect to * @param int $_status the http status * @return string the URL to redirect to */ // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter public function redirect( $location, $_status ) { // add the admin message id param to the if ( $this->set_messages() ) { $location = add_query_arg( self::MESSAGE_ID_GET_NAME, $this->get_message_id(), $location ); } // nosemgrep: audit.php.wp.security.xss.query-arg return $location; } /** * Generate a unique id to identify the messages * * @since 1.0.0 * @return string unique identifier */ protected function get_message_id() { if ( ! isset( $this->message_id ) ) { $this->message_id = __FILE__; } return wp_create_nonce( $this->message_id ); } }