*/ const KNOWN = array( 'hello.php' => 'hello-dolly', ); /** * Returns the WordPress.org slug or null if the plugin is clearly not hosted there. * * @param string $plugin_file Plugin basename, e.g. "akismet/akismet.php". * @param array $plugin_data Plugin headers from get_plugins(). * @param object|false|null $update_plugins Value of the update_plugins site transient. * @return string|null */ public static function resolve( string $plugin_file, array $plugin_data, $update_plugins ): ?string { // Plugins that declare a foreign update source are not on WordPress.org. $update_uri = isset( $plugin_data['UpdateURI'] ) ? trim( (string) $plugin_data['UpdateURI'] ) : ''; if ( '' !== $update_uri && ! self::is_wporg_uri( $update_uri ) ) { return null; } // WordPress core already knows the slug for plugins it checks for updates. if ( is_object( $update_plugins ) ) { foreach ( array( 'response', 'no_update' ) as $list ) { $items = isset( $update_plugins->$list ) && is_array( $update_plugins->$list ) ? $update_plugins->$list : array(); $item = $items[ $plugin_file ] ?? null; if ( is_object( $item ) && ! empty( $item->slug ) ) { return sanitize_key( (string) $item->slug ); } } } if ( isset( self::KNOWN[ $plugin_file ] ) ) { return self::KNOWN[ $plugin_file ]; } $dir = dirname( $plugin_file ); $slug = ( '.' === $dir || '' === $dir ) ? basename( $plugin_file, '.php' ) : $dir; $slug = sanitize_key( $slug ); return '' === $slug ? null : $slug; } /** * Whether an Update URI points to WordPress.org. * * @param string $uri Update URI header. * @return bool */ private static function is_wporg_uri( string $uri ): bool { $host = wp_parse_url( $uri, PHP_URL_HOST ); if ( ! is_string( $host ) ) { $host = strtok( $uri, '/' ); } return in_array( strtolower( (string) $host ), array( 'wordpress.org', 'w.org' ), true ); } }