Merge pull request #354 from RichardCoffee/master

Add a new factory method that allows array as parameter argument and pulls update server URI from file header
This commit is contained in:
Yahnis Elsts 2020-03-31 16:55:26 +03:00 committed by GitHub
commit 0af94b5609
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 60 additions and 1 deletions

View File

@ -18,6 +18,34 @@ if ( !class_exists('Puc_v4p9_Factory', false) ):
protected static $myMajorVersion = '';
protected static $latestCompatibleVersion = '';
/**
* Wrapper method for buildUpdateChecker.
*
* @param string $fullPath
* @param array
*/
public static function buildFromHeader( $fullPath, $args = array() ) {
$fullPath = self::normalizePath( $fullPath );
//Set up defaults.
$defaults = array(
'metadataUrl' => '',
'slug' => '',
'checkPeriod' => 12,
'optionName' => '',
'muPluginFile' => '',
);
$args = array_merge( $defaults, array_intersect_key( $args, $defaults ) );
extract( $args, EXTR_SKIP );
//Check for the service URI
if ( empty( $metadataUrl ) ) {
$metadataUrl = self::getServiceURI( $fullPath );
}
return self::buildUpdateChecker( $metadataUrl, $fullPath, $slug, $checkPeriod, $optionName, $muPluginFile );
}
/**
* Create a new instance of the update checker.
*
@ -178,6 +206,37 @@ if ( !class_exists('Puc_v4p9_Factory', false) ):
return null;
}
/**
* Get the service URI from the file header.
*
* @param string $fullPath
* @return string
*/
private static function getServiceURI( $fullPath ) {
//Look for the URI
if ( is_readable( $fullPath ) ) {
$seek = array(
'github' => 'GitHub URI',
'gitlab' => 'GitLab URI',
'bucket' => 'BitBucket URI'
);
$seek = apply_filters( 'puc_get_source_uri', $seek );
$data = get_file_data( $fullPath, $seek );
foreach( $data as $key => $uri ) {
if ( $uri ) return $uri;
}
}
//URI was not found so trigger an error.
trigger_error(
sprintf(
'Unable to locate uri in header of "%s"',
htmlentities( $fullPath )
),
E_USER_ERROR
);
}
/**
* Get the name of the hosting service that the URL points to.
*
@ -294,4 +353,4 @@ if ( !class_exists('Puc_v4p9_Factory', false) ):
}
}
endif;
endif;