plugin_version = $plugin_version; $this->readme = $readme; } /** * @return array * * @throws RuntimeException */ public function get_paragraphs() { $section = $this->get_upgrade_notice_section(); $current_version = $this->plugin_version->get_source_code_version(); $result = array(); array_walk( $section, function ( $paragraphs, $version ) use ( &$result, $current_version ) { if ( version_compare( $current_version, $version, '<' ) ) { $result = array_merge( $result, $paragraphs ); } } ); return $result; } /** * @return array * * @throws RuntimeException */ private function get_upgrade_notice_section() { $section = $this->readme->get_section( Readme::SECTION_UPGRADE_NOTICE ); if ( is_null( $section ) ) { return array(); } return $this->parse( $section ); } /** * @param string $content * * @return array * * @throws RuntimeException */ private function parse( $content ) { $pattern = '/= (\d+\.\d+\.\d+) =/'; preg_match_all( $pattern, $content, $matches ); $versions = preg_filter( $pattern, '${1}', $matches[0] ); $messages = preg_split( $pattern, $content, null, PREG_SPLIT_NO_EMPTY ); $messages = $this->split( $messages ); if ( count( $versions ) === count( $messages ) ) { return array_combine( $versions, $messages ); } throw new RuntimeException( 'Upgrade Notice section has invalid format.' ); } /** * @param array $messages * * @return array */ private function split( array $messages ) { return array_map( function ( $message ) { $message = htmlspecialchars( $message, ENT_QUOTES ); return preg_split( '/\r|\n|\r\n/', $message, null, PREG_SPLIT_NO_EMPTY ); }, $messages ); } }