repo stringlengths 7 63 | file_url stringlengths 81 284 | file_path stringlengths 5 200 | content stringlengths 0 32.8k | language stringclasses 1
value | license stringclasses 7
values | commit_sha stringlengths 40 40 | retrieved_at stringdate 2026-01-04 15:02:33 2026-01-05 05:24:06 | truncated bool 2
classes |
|---|---|---|---|---|---|---|---|---|
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/admin/update/Puc/v4p11/Update.php | admin/update/Puc/v4p11/Update.php | <?php
if ( !class_exists('Puc_v4p11_Update', false) ):
/**
* A simple container class for holding information about an available update.
*
* @author Janis Elsts
* @access public
*/
abstract class Puc_v4p11_Update extends Puc_v4p11_Metadata {
public $slug;
public $version;
public $download_url;
public $translations = array();
/**
* @return string[]
*/
protected function getFieldNames() {
return array('slug', 'version', 'download_url', 'translations');
}
public function toWpFormat() {
$update = new stdClass();
$update->slug = $this->slug;
$update->new_version = $this->version;
$update->package = $this->download_url;
return $update;
}
}
endif;
| php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/admin/update/Puc/v4p11/UpdateChecker.php | admin/update/Puc/v4p11/UpdateChecker.php | <?php
if ( !class_exists('Puc_v4p11_UpdateChecker', false) ):
abstract class Puc_v4p11_UpdateChecker {
protected $filterSuffix = '';
protected $updateTransient = '';
protected $translationType = ''; //"plugin" or "theme".
/**
* Set to TRUE to enable error reporting. Errors are raised using trigger_error()
* and should be logged to the standard PHP error log.
* @var bool
*/
public $debugMode = null;
/**
* @var string Where to store the update info.
*/
public $optionName = '';
/**
* @var string The URL of the metadata file.
*/
public $metadataUrl = '';
/**
* @var string Plugin or theme directory name.
*/
public $directoryName = '';
/**
* @var string The slug that will be used in update checker hooks and remote API requests.
* Usually matches the directory name unless the plugin/theme directory has been renamed.
*/
public $slug = '';
/**
* @var Puc_v4p11_InstalledPackage
*/
protected $package;
/**
* @var Puc_v4p11_Scheduler
*/
public $scheduler;
/**
* @var Puc_v4p11_UpgraderStatus
*/
protected $upgraderStatus;
/**
* @var Puc_v4p11_StateStore
*/
protected $updateState;
/**
* @var array List of API errors triggered during the last checkForUpdates() call.
*/
protected $lastRequestApiErrors = array();
/**
* @var string|mixed The default is 0 because parse_url() can return NULL or FALSE.
*/
protected $cachedMetadataHost = 0;
/**
* @var Puc_v4p11_DebugBar_Extension|null
*/
protected $debugBarExtension = null;
public function __construct($metadataUrl, $directoryName, $slug = null, $checkPeriod = 12, $optionName = '') {
$this->debugMode = (bool)(constant('WP_DEBUG'));
$this->metadataUrl = $metadataUrl;
$this->directoryName = $directoryName;
$this->slug = !empty($slug) ? $slug : $this->directoryName;
$this->optionName = $optionName;
if ( empty($this->optionName) ) {
//BC: Initially the library only supported plugin updates and didn't use type prefixes
//in the option name. Lets use the same prefix-less name when possible.
if ( $this->filterSuffix === '' ) {
$this->optionName = 'external_updates-' . $this->slug;
} else {
$this->optionName = $this->getUniqueName('external_updates');
}
}
$this->package = $this->createInstalledPackage();
$this->scheduler = $this->createScheduler($checkPeriod);
$this->upgraderStatus = new Puc_v4p11_UpgraderStatus();
$this->updateState = new Puc_v4p11_StateStore($this->optionName);
if ( did_action('init') ) {
$this->loadTextDomain();
} else {
add_action('init', array($this, 'loadTextDomain'));
}
$this->installHooks();
}
/**
* @internal
*/
public function loadTextDomain() {
//We're not using load_plugin_textdomain() or its siblings because figuring out where
//the library is located (plugin, mu-plugin, theme, custom wp-content paths) is messy.
$domain = 'plugin-update-checker';
$locale = apply_filters(
'plugin_locale',
(is_admin() && function_exists('get_user_locale')) ? get_user_locale() : get_locale(),
$domain
);
$moFile = $domain . '-' . $locale . '.mo';
$path = realpath(dirname(__FILE__) . '/../../languages');
if ($path && file_exists($path)) {
load_textdomain($domain, $path . '/' . $moFile);
}
}
protected function installHooks() {
//Insert our update info into the update array maintained by WP.
add_filter('site_transient_' . $this->updateTransient, array($this,'injectUpdate'));
//Insert translation updates into the update list.
add_filter('site_transient_' . $this->updateTransient, array($this, 'injectTranslationUpdates'));
//Clear translation updates when WP clears the update cache.
//This needs to be done directly because the library doesn't actually remove obsolete plugin updates,
//it just hides them (see getUpdate()). We can't do that with translations - too much disk I/O.
add_action(
'delete_site_transient_' . $this->updateTransient,
array($this, 'clearCachedTranslationUpdates')
);
//Rename the update directory to be the same as the existing directory.
if ( $this->directoryName !== '.' ) {
add_filter('upgrader_source_selection', array($this, 'fixDirectoryName'), 10, 3);
}
//Allow HTTP requests to the metadata URL even if it's on a local host.
add_filter('http_request_host_is_external', array($this, 'allowMetadataHost'), 10, 2);
//DebugBar integration.
if ( did_action('plugins_loaded') ) {
$this->maybeInitDebugBar();
} else {
add_action('plugins_loaded', array($this, 'maybeInitDebugBar'));
}
}
/**
* Remove hooks that were added by this update checker instance.
*/
public function removeHooks() {
remove_filter('site_transient_' . $this->updateTransient, array($this,'injectUpdate'));
remove_filter('site_transient_' . $this->updateTransient, array($this, 'injectTranslationUpdates'));
remove_action(
'delete_site_transient_' . $this->updateTransient,
array($this, 'clearCachedTranslationUpdates')
);
remove_filter('upgrader_source_selection', array($this, 'fixDirectoryName'), 10);
remove_filter('http_request_host_is_external', array($this, 'allowMetadataHost'), 10);
remove_action('plugins_loaded', array($this, 'maybeInitDebugBar'));
remove_action('init', array($this, 'loadTextDomain'));
if ( $this->scheduler ) {
$this->scheduler->removeHooks();
}
if ( $this->debugBarExtension ) {
$this->debugBarExtension->removeHooks();
}
}
/**
* Check if the current user has the required permissions to install updates.
*
* @return bool
*/
abstract public function userCanInstallUpdates();
/**
* Explicitly allow HTTP requests to the metadata URL.
*
* WordPress has a security feature where the HTTP API will reject all requests that are sent to
* another site hosted on the same server as the current site (IP match), a local host, or a local
* IP, unless the host exactly matches the current site.
*
* This feature is opt-in (at least in WP 4.4). Apparently some people enable it.
*
* That can be a problem when you're developing your plugin and you decide to host the update information
* on the same server as your test site. Update requests will mysteriously fail.
*
* We fix that by adding an exception for the metadata host.
*
* @param bool $allow
* @param string $host
* @return bool
*/
public function allowMetadataHost($allow, $host) {
if ( $this->cachedMetadataHost === 0 ) {
$this->cachedMetadataHost = parse_url($this->metadataUrl, PHP_URL_HOST);
}
if ( is_string($this->cachedMetadataHost) && (strtolower($host) === strtolower($this->cachedMetadataHost)) ) {
return true;
}
return $allow;
}
/**
* Create a package instance that represents this plugin or theme.
*
* @return Puc_v4p11_InstalledPackage
*/
abstract protected function createInstalledPackage();
/**
* @return Puc_v4p11_InstalledPackage
*/
public function getInstalledPackage() {
return $this->package;
}
/**
* Create an instance of the scheduler.
*
* This is implemented as a method to make it possible for plugins to subclass the update checker
* and substitute their own scheduler.
*
* @param int $checkPeriod
* @return Puc_v4p11_Scheduler
*/
abstract protected function createScheduler($checkPeriod);
/**
* Check for updates. The results are stored in the DB option specified in $optionName.
*
* @return Puc_v4p11_Update|null
*/
public function checkForUpdates() {
$installedVersion = $this->getInstalledVersion();
//Fail silently if we can't find the plugin/theme or read its header.
if ( $installedVersion === null ) {
$this->triggerError(
sprintf('Skipping update check for %s - installed version unknown.', $this->slug),
E_USER_WARNING
);
return null;
}
//Start collecting API errors.
$this->lastRequestApiErrors = array();
add_action('puc_api_error', array($this, 'collectApiErrors'), 10, 4);
$state = $this->updateState;
$state->setLastCheckToNow()
->setCheckedVersion($installedVersion)
->save(); //Save before checking in case something goes wrong
$state->setUpdate($this->requestUpdate());
$state->save();
//Stop collecting API errors.
remove_action('puc_api_error', array($this, 'collectApiErrors'), 10);
return $this->getUpdate();
}
/**
* Load the update checker state from the DB.
*
* @return Puc_v4p11_StateStore
*/
public function getUpdateState() {
return $this->updateState->lazyLoad();
}
/**
* Reset update checker state - i.e. last check time, cached update data and so on.
*
* Call this when your plugin is being uninstalled, or if you want to
* clear the update cache.
*/
public function resetUpdateState() {
$this->updateState->delete();
}
/**
* Get the details of the currently available update, if any.
*
* If no updates are available, or if the last known update version is below or equal
* to the currently installed version, this method will return NULL.
*
* Uses cached update data. To retrieve update information straight from
* the metadata URL, call requestUpdate() instead.
*
* @return Puc_v4p11_Update|null
*/
public function getUpdate() {
$update = $this->updateState->getUpdate();
//Is there an update available?
if ( isset($update) ) {
//Check if the update is actually newer than the currently installed version.
$installedVersion = $this->getInstalledVersion();
if ( ($installedVersion !== null) && version_compare($update->version, $installedVersion, '>') ){
return $update;
}
}
return null;
}
/**
* Retrieve the latest update (if any) from the configured API endpoint.
*
* Subclasses should run the update through filterUpdateResult before returning it.
*
* @return Puc_v4p11_Update An instance of Update, or NULL when no updates are available.
*/
abstract public function requestUpdate();
/**
* Filter the result of a requestUpdate() call.
*
* @param Puc_v4p11_Update|null $update
* @param array|WP_Error|null $httpResult The value returned by wp_remote_get(), if any.
* @return Puc_v4p11_Update
*/
protected function filterUpdateResult($update, $httpResult = null) {
//Let plugins/themes modify the update.
$update = apply_filters($this->getUniqueName('request_update_result'), $update, $httpResult);
$this->fixSupportedWordpressVersion($update);
if ( isset($update, $update->translations) ) {
//Keep only those translation updates that apply to this site.
$update->translations = $this->filterApplicableTranslations($update->translations);
}
return $update;
}
/**
* The "Tested up to" field in the plugin metadata is supposed to be in the form of "major.minor",
* while WordPress core's list_plugin_updates() expects the $update->tested field to be an exact
* version, e.g. "major.minor.patch", to say it's compatible. In other case it shows
* "Compatibility: Unknown".
* The function mimics how wordpress.org API crafts the "tested" field out of "Tested up to".
*
* @param Puc_v4p11_Metadata|null $update
*/
protected function fixSupportedWordpressVersion(Puc_v4p11_Metadata $update = null) {
if ( !isset($update->tested) || !preg_match('/^\d++\.\d++$/', $update->tested) ) {
return;
}
$actualWpVersions = array();
$wpVersion = $GLOBALS['wp_version'];
if ( function_exists('get_core_updates') ) {
$coreUpdates = get_core_updates();
if ( is_array($coreUpdates) ) {
foreach ($coreUpdates as $coreUpdate) {
if ( isset($coreUpdate->current) ) {
$actualWpVersions[] = $coreUpdate->current;
}
}
}
}
$actualWpVersions[] = $wpVersion;
$actualWpPatchNumber = null;
foreach ($actualWpVersions as $version) {
if ( preg_match('/^(?P<majorMinor>\d++\.\d++)(?:\.(?P<patch>\d++))?/', $version, $versionParts) ) {
if ( $versionParts['majorMinor'] === $update->tested ) {
$patch = isset($versionParts['patch']) ? intval($versionParts['patch']) : 0;
if ( $actualWpPatchNumber === null ) {
$actualWpPatchNumber = $patch;
} else {
$actualWpPatchNumber = max($actualWpPatchNumber, $patch);
}
}
}
}
if ( $actualWpPatchNumber === null ) {
$actualWpPatchNumber = 999;
}
if ( $actualWpPatchNumber > 0 ) {
$update->tested .= '.' . $actualWpPatchNumber;
}
}
/**
* Get the currently installed version of the plugin or theme.
*
* @return string|null Version number.
*/
public function getInstalledVersion() {
return $this->package->getInstalledVersion();
}
/**
* Get the full path of the plugin or theme directory.
*
* @return string
*/
public function getAbsoluteDirectoryPath() {
return $this->package->getAbsoluteDirectoryPath();
}
/**
* Trigger a PHP error, but only when $debugMode is enabled.
*
* @param string $message
* @param int $errorType
*/
public function triggerError($message, $errorType) {
if ( $this->isDebugModeEnabled() ) {
trigger_error($message, $errorType);
}
}
/**
* @return bool
*/
protected function isDebugModeEnabled() {
if ( $this->debugMode === null ) {
$this->debugMode = (bool)(constant('WP_DEBUG'));
}
return $this->debugMode;
}
/**
* Get the full name of an update checker filter, action or DB entry.
*
* This method adds the "puc_" prefix and the "-$slug" suffix to the filter name.
* For example, "pre_inject_update" becomes "puc_pre_inject_update-plugin-slug".
*
* @param string $baseTag
* @return string
*/
public function getUniqueName($baseTag) {
$name = 'puc_' . $baseTag;
if ( $this->filterSuffix !== '' ) {
$name .= '_' . $this->filterSuffix;
}
return $name . '-' . $this->slug;
}
/**
* Store API errors that are generated when checking for updates.
*
* @internal
* @param WP_Error $error
* @param array|null $httpResponse
* @param string|null $url
* @param string|null $slug
*/
public function collectApiErrors($error, $httpResponse = null, $url = null, $slug = null) {
if ( isset($slug) && ($slug !== $this->slug) ) {
return;
}
$this->lastRequestApiErrors[] = array(
'error' => $error,
'httpResponse' => $httpResponse,
'url' => $url,
);
}
/**
* @return array
*/
public function getLastRequestApiErrors() {
return $this->lastRequestApiErrors;
}
/* -------------------------------------------------------------------
* PUC filters and filter utilities
* -------------------------------------------------------------------
*/
/**
* Register a callback for one of the update checker filters.
*
* Identical to add_filter(), except it automatically adds the "puc_" prefix
* and the "-$slug" suffix to the filter name. For example, "request_info_result"
* becomes "puc_request_info_result-your_plugin_slug".
*
* @param string $tag
* @param callable $callback
* @param int $priority
* @param int $acceptedArgs
*/
public function addFilter($tag, $callback, $priority = 10, $acceptedArgs = 1) {
add_filter($this->getUniqueName($tag), $callback, $priority, $acceptedArgs);
}
/* -------------------------------------------------------------------
* Inject updates
* -------------------------------------------------------------------
*/
/**
* Insert the latest update (if any) into the update list maintained by WP.
*
* @param stdClass $updates Update list.
* @return stdClass Modified update list.
*/
public function injectUpdate($updates) {
//Is there an update to insert?
$update = $this->getUpdate();
if ( !$this->shouldShowUpdates() ) {
$update = null;
}
if ( !empty($update) ) {
//Let plugins filter the update info before it's passed on to WordPress.
$update = apply_filters($this->getUniqueName('pre_inject_update'), $update);
$updates = $this->addUpdateToList($updates, $update->toWpFormat());
} else {
//Clean up any stale update info.
$updates = $this->removeUpdateFromList($updates);
//Add a placeholder item to the "no_update" list to enable auto-update support.
//If we don't do this, the option to enable automatic updates will only show up
//when an update is available.
$updates = $this->addNoUpdateItem($updates);
}
return $updates;
}
/**
* @param stdClass|null $updates
* @param stdClass|array $updateToAdd
* @return stdClass
*/
protected function addUpdateToList($updates, $updateToAdd) {
if ( !is_object($updates) ) {
$updates = new stdClass();
$updates->response = array();
}
$updates->response[$this->getUpdateListKey()] = $updateToAdd;
return $updates;
}
/**
* @param stdClass|null $updates
* @return stdClass|null
*/
protected function removeUpdateFromList($updates) {
if ( isset($updates, $updates->response) ) {
unset($updates->response[$this->getUpdateListKey()]);
}
return $updates;
}
/**
* See this post for more information:
* @link https://make.wordpress.org/core/2020/07/30/recommended-usage-of-the-updates-api-to-support-the-auto-updates-ui-for-plugins-and-themes-in-wordpress-5-5/
*
* @param stdClass|null $updates
* @return stdClass
*/
protected function addNoUpdateItem($updates) {
if ( !is_object($updates) ) {
$updates = new stdClass();
$updates->response = array();
$updates->no_update = array();
} else if ( !isset($updates->no_update) ) {
$updates->no_update = array();
}
$updates->no_update[$this->getUpdateListKey()] = (object) $this->getNoUpdateItemFields();
return $updates;
}
/**
* Subclasses should override this method to add fields that are specific to plugins or themes.
* @return array
*/
protected function getNoUpdateItemFields() {
return array(
'new_version' => $this->getInstalledVersion(),
'url' => '',
'package' => '',
'requires_php' => '',
);
}
/**
* Get the key that will be used when adding updates to the update list that's maintained
* by the WordPress core. The list is always an associative array, but the key is different
* for plugins and themes.
*
* @return string
*/
abstract protected function getUpdateListKey();
/**
* Should we show available updates?
*
* Usually the answer is "yes", but there are exceptions. For example, WordPress doesn't
* support automatic updates installation for mu-plugins, so PUC usually won't show update
* notifications in that case. See the plugin-specific subclass for details.
*
* Note: This method only applies to updates that are displayed (or not) in the WordPress
* admin. It doesn't affect APIs like requestUpdate and getUpdate.
*
* @return bool
*/
protected function shouldShowUpdates() {
return true;
}
/* -------------------------------------------------------------------
* JSON-based update API
* -------------------------------------------------------------------
*/
/**
* Retrieve plugin or theme metadata from the JSON document at $this->metadataUrl.
*
* @param string $metaClass Parse the JSON as an instance of this class. It must have a static fromJson method.
* @param string $filterRoot
* @param array $queryArgs Additional query arguments.
* @return array [Puc_v4p11_Metadata|null, array|WP_Error] A metadata instance and the value returned by wp_remote_get().
*/
protected function requestMetadata($metaClass, $filterRoot, $queryArgs = array()) {
//Query args to append to the URL. Plugins can add their own by using a filter callback (see addQueryArgFilter()).
$queryArgs = array_merge(
array(
'installed_version' => strval($this->getInstalledVersion()),
'php' => phpversion(),
'locale' => get_locale(),
),
$queryArgs
);
$queryArgs = apply_filters($this->getUniqueName($filterRoot . '_query_args'), $queryArgs);
//Various options for the wp_remote_get() call. Plugins can filter these, too.
$options = array(
'timeout' => 10, //seconds
'headers' => array(
'Accept' => 'application/json',
),
);
$options = apply_filters($this->getUniqueName($filterRoot . '_options'), $options);
//The metadata file should be at 'http://your-api.com/url/here/$slug/info.json'
$url = $this->metadataUrl;
if ( !empty($queryArgs) ){
$url = add_query_arg($queryArgs, $url);
}
$result = wp_remote_get($url, $options);
$result = apply_filters($this->getUniqueName('request_metadata_http_result'), $result, $url, $options);
//Try to parse the response
$status = $this->validateApiResponse($result);
$metadata = null;
if ( !is_wp_error($status) ){
if ( version_compare(PHP_VERSION, '5.3', '>=') && (strpos($metaClass, '\\') === false) ) {
$metaClass = __NAMESPACE__ . '\\' . $metaClass;
}
$metadata = call_user_func(array($metaClass, 'fromJson'), $result['body']);
} else {
do_action('puc_api_error', $status, $result, $url, $this->slug);
$this->triggerError(
sprintf('The URL %s does not point to a valid metadata file. ', $url)
. $status->get_error_message(),
E_USER_WARNING
);
}
return array($metadata, $result);
}
/**
* Check if $result is a successful update API response.
*
* @param array|WP_Error $result
* @return true|WP_Error
*/
protected function validateApiResponse($result) {
if ( is_wp_error($result) ) { /** @var WP_Error $result */
return new WP_Error($result->get_error_code(), 'WP HTTP Error: ' . $result->get_error_message());
}
if ( !isset($result['response']['code']) ) {
return new WP_Error(
'puc_no_response_code',
'wp_remote_get() returned an unexpected result.'
);
}
if ( $result['response']['code'] !== 200 ) {
return new WP_Error(
'puc_unexpected_response_code',
'HTTP response code is ' . $result['response']['code'] . ' (expected: 200)'
);
}
if ( empty($result['body']) ) {
return new WP_Error('puc_empty_response', 'The metadata file appears to be empty.');
}
return true;
}
/* -------------------------------------------------------------------
* Language packs / Translation updates
* -------------------------------------------------------------------
*/
/**
* Filter a list of translation updates and return a new list that contains only updates
* that apply to the current site.
*
* @param array $translations
* @return array
*/
protected function filterApplicableTranslations($translations) {
$languages = array_flip(array_values(get_available_languages()));
$installedTranslations = $this->getInstalledTranslations();
$applicableTranslations = array();
foreach ($translations as $translation) {
//Does it match one of the available core languages?
$isApplicable = array_key_exists($translation->language, $languages);
//Is it more recent than an already-installed translation?
if ( isset($installedTranslations[$translation->language]) ) {
$updateTimestamp = strtotime($translation->updated);
$installedTimestamp = strtotime($installedTranslations[$translation->language]['PO-Revision-Date']);
$isApplicable = $updateTimestamp > $installedTimestamp;
}
if ( $isApplicable ) {
$applicableTranslations[] = $translation;
}
}
return $applicableTranslations;
}
/**
* Get a list of installed translations for this plugin or theme.
*
* @return array
*/
protected function getInstalledTranslations() {
if ( !function_exists('wp_get_installed_translations') ) {
return array();
}
$installedTranslations = wp_get_installed_translations($this->translationType . 's');
if ( isset($installedTranslations[$this->directoryName]) ) {
$installedTranslations = $installedTranslations[$this->directoryName];
} else {
$installedTranslations = array();
}
return $installedTranslations;
}
/**
* Insert translation updates into the list maintained by WordPress.
*
* @param stdClass $updates
* @return stdClass
*/
public function injectTranslationUpdates($updates) {
$translationUpdates = $this->getTranslationUpdates();
if ( empty($translationUpdates) ) {
return $updates;
}
//Being defensive.
if ( !is_object($updates) ) {
$updates = new stdClass();
}
if ( !isset($updates->translations) ) {
$updates->translations = array();
}
//In case there's a name collision with a plugin or theme hosted on wordpress.org,
//remove any preexisting updates that match our thing.
$updates->translations = array_values(array_filter(
$updates->translations,
array($this, 'isNotMyTranslation')
));
//Add our updates to the list.
foreach($translationUpdates as $update) {
$convertedUpdate = array_merge(
array(
'type' => $this->translationType,
'slug' => $this->directoryName,
'autoupdate' => 0,
//AFAICT, WordPress doesn't actually use the "version" field for anything.
//But lets make sure it's there, just in case.
'version' => isset($update->version) ? $update->version : ('1.' . strtotime($update->updated)),
),
(array)$update
);
$updates->translations[] = $convertedUpdate;
}
return $updates;
}
/**
* Get a list of available translation updates.
*
* This method will return an empty array if there are no updates.
* Uses cached update data.
*
* @return array
*/
public function getTranslationUpdates() {
return $this->updateState->getTranslations();
}
/**
* Remove all cached translation updates.
*
* @see wp_clean_update_cache
*/
public function clearCachedTranslationUpdates() {
$this->updateState->setTranslations(array());
}
/**
* Filter callback. Keeps only translations that *don't* match this plugin or theme.
*
* @param array $translation
* @return bool
*/
protected function isNotMyTranslation($translation) {
$isMatch = isset($translation['type'], $translation['slug'])
&& ($translation['type'] === $this->translationType)
&& ($translation['slug'] === $this->directoryName);
return !$isMatch;
}
/* -------------------------------------------------------------------
* Fix directory name when installing updates
* -------------------------------------------------------------------
*/
/**
* Rename the update directory to match the existing plugin/theme directory.
*
* When WordPress installs a plugin or theme update, it assumes that the ZIP file will contain
* exactly one directory, and that the directory name will be the same as the directory where
* the plugin or theme is currently installed.
*
* GitHub and other repositories provide ZIP downloads, but they often use directory names like
* "project-branch" or "project-tag-hash". We need to change the name to the actual plugin folder.
*
* This is a hook callback. Don't call it from a plugin.
*
* @access protected
*
* @param string $source The directory to copy to /wp-content/plugins or /wp-content/themes. Usually a subdirectory of $remoteSource.
* @param string $remoteSource WordPress has extracted the update to this directory.
* @param WP_Upgrader $upgrader
* @return string|WP_Error
*/
public function fixDirectoryName($source, $remoteSource, $upgrader) {
global $wp_filesystem;
/** @var WP_Filesystem_Base $wp_filesystem */
//Basic sanity checks.
if ( !isset($source, $remoteSource, $upgrader, $upgrader->skin, $wp_filesystem) ) {
return $source;
}
//If WordPress is upgrading anything other than our plugin/theme, leave the directory name unchanged.
if ( !$this->isBeingUpgraded($upgrader) ) {
return $source;
}
//Rename the source to match the existing directory.
$correctedSource = trailingslashit($remoteSource) . $this->directoryName . '/';
if ( $source !== $correctedSource ) {
//The update archive should contain a single directory that contains the rest of plugin/theme files.
//Otherwise, WordPress will try to copy the entire working directory ($source == $remoteSource).
//We can't rename $remoteSource because that would break WordPress code that cleans up temporary files
//after update.
if ( $this->isBadDirectoryStructure($remoteSource) ) {
return new WP_Error(
'puc-incorrect-directory-structure',
sprintf(
'The directory structure of the update is incorrect. All files should be inside ' .
'a directory named <span class="code">%s</span>, not at the root of the ZIP archive.',
htmlentities($this->slug)
)
);
}
/** @var WP_Upgrader_Skin $upgrader ->skin */
$upgrader->skin->feedback(sprintf(
'Renaming %s to %s…',
'<span class="code">' . basename($source) . '</span>',
'<span class="code">' . $this->directoryName . '</span>'
));
if ( $wp_filesystem->move($source, $correctedSource, true) ) {
$upgrader->skin->feedback('Directory successfully renamed.');
return $correctedSource;
} else {
return new WP_Error(
'puc-rename-failed',
'Unable to rename the update to match the existing directory.'
);
}
}
return $source;
}
/**
* Is there an update being installed right now, for this plugin or theme?
*
* @param WP_Upgrader|null $upgrader The upgrader that's performing the current update.
* @return bool
*/
abstract public function isBeingUpgraded($upgrader = null);
/**
* Check for incorrect update directory structure. An update must contain a single directory,
* all other files should be inside that directory.
*
* @param string $remoteSource Directory path.
* @return bool
*/
protected function isBadDirectoryStructure($remoteSource) {
global $wp_filesystem;
/** @var WP_Filesystem_Base $wp_filesystem */
$sourceFiles = $wp_filesystem->dirlist($remoteSource);
if ( is_array($sourceFiles) ) {
$sourceFiles = array_keys($sourceFiles);
$firstFilePath = trailingslashit($remoteSource) . $sourceFiles[0];
return (count($sourceFiles) > 1) || (!$wp_filesystem->is_dir($firstFilePath));
}
//Assume it's fine.
return false;
}
/* -------------------------------------------------------------------
* DebugBar integration
* -------------------------------------------------------------------
*/
/**
* Initialize the update checker Debug Bar plugin/add-on thingy.
*/
public function maybeInitDebugBar() {
if ( class_exists('Debug_Bar', false) && file_exists(dirname(__FILE__) . '/DebugBar') ) {
$this->debugBarExtension = $this->createDebugBarExtension();
}
}
protected function createDebugBarExtension() {
return new Puc_v4p11_DebugBar_Extension($this);
}
/**
* Display additional configuration details in the Debug Bar panel.
*
* @param Puc_v4p11_DebugBar_Panel $panel
*/
public function onDisplayConfiguration($panel) {
//Do nothing. Subclasses can use this to add additional info to the panel.
}
}
endif;
| php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/admin/update/Puc/v4p11/Autoloader.php | admin/update/Puc/v4p11/Autoloader.php | <?php
if ( !class_exists('Puc_v4p11_Autoloader', false) ):
class Puc_v4p11_Autoloader {
private $prefix = '';
private $rootDir = '';
private $libraryDir = '';
private $staticMap;
public function __construct() {
$this->rootDir = dirname(__FILE__) . '/';
$nameParts = explode('_', __CLASS__, 3);
$this->prefix = $nameParts[0] . '_' . $nameParts[1] . '_';
$this->libraryDir = $this->rootDir . '../..';
if ( !self::isPhar() ) {
$this->libraryDir = realpath($this->libraryDir);
}
$this->libraryDir = $this->libraryDir . '/';
$this->staticMap = array(
'PucReadmeParser' => 'vendor/PucReadmeParser.php',
'Parsedown' => 'vendor/Parsedown.php',
'Puc_v4_Factory' => 'Puc/v4/Factory.php',
);
spl_autoload_register(array($this, 'autoload'));
}
/**
* Determine if this file is running as part of a Phar archive.
*
* @return bool
*/
private static function isPhar() {
//Check if the current file path starts with "phar://".
static $pharProtocol = 'phar://';
return (substr(__FILE__, 0, strlen($pharProtocol)) === $pharProtocol);
}
public function autoload($className) {
if ( isset($this->staticMap[$className]) && file_exists($this->libraryDir . $this->staticMap[$className]) ) {
/** @noinspection PhpIncludeInspection */
include ($this->libraryDir . $this->staticMap[$className]);
return;
}
if (strpos($className, $this->prefix) === 0) {
$path = substr($className, strlen($this->prefix));
$path = str_replace('_', '/', $path);
$path = $this->rootDir . $path . '.php';
if (file_exists($path)) {
/** @noinspection PhpIncludeInspection */
include $path;
}
}
}
}
endif;
| php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/admin/update/Puc/v4p11/StateStore.php | admin/update/Puc/v4p11/StateStore.php | <?php
if ( !class_exists('Puc_v4p11_StateStore', false) ):
class Puc_v4p11_StateStore {
/**
* @var int Last update check timestamp.
*/
protected $lastCheck = 0;
/**
* @var string Version number.
*/
protected $checkedVersion = '';
/**
* @var Puc_v4p11_Update|null Cached update.
*/
protected $update = null;
/**
* @var string Site option name.
*/
private $optionName = '';
/**
* @var bool Whether we've already tried to load the state from the database.
*/
private $isLoaded = false;
public function __construct($optionName) {
$this->optionName = $optionName;
}
/**
* Get time elapsed since the last update check.
*
* If there are no recorded update checks, this method returns a large arbitrary number
* (i.e. time since the Unix epoch).
*
* @return int Elapsed time in seconds.
*/
public function timeSinceLastCheck() {
$this->lazyLoad();
return time() - $this->lastCheck;
}
/**
* @return int
*/
public function getLastCheck() {
$this->lazyLoad();
return $this->lastCheck;
}
/**
* Set the time of the last update check to the current timestamp.
*
* @return $this
*/
public function setLastCheckToNow() {
$this->lazyLoad();
$this->lastCheck = time();
return $this;
}
/**
* @return null|Puc_v4p11_Update
*/
public function getUpdate() {
$this->lazyLoad();
return $this->update;
}
/**
* @param Puc_v4p11_Update|null $update
* @return $this
*/
public function setUpdate(Puc_v4p11_Update $update = null) {
$this->lazyLoad();
$this->update = $update;
return $this;
}
/**
* @return string
*/
public function getCheckedVersion() {
$this->lazyLoad();
return $this->checkedVersion;
}
/**
* @param string $version
* @return $this
*/
public function setCheckedVersion($version) {
$this->lazyLoad();
$this->checkedVersion = strval($version);
return $this;
}
/**
* Get translation updates.
*
* @return array
*/
public function getTranslations() {
$this->lazyLoad();
if ( isset($this->update, $this->update->translations) ) {
return $this->update->translations;
}
return array();
}
/**
* Set translation updates.
*
* @param array $translationUpdates
*/
public function setTranslations($translationUpdates) {
$this->lazyLoad();
if ( isset($this->update) ) {
$this->update->translations = $translationUpdates;
$this->save();
}
}
public function save() {
$state = new stdClass();
$state->lastCheck = $this->lastCheck;
$state->checkedVersion = $this->checkedVersion;
if ( isset($this->update)) {
$state->update = $this->update->toStdClass();
$updateClass = get_class($this->update);
$state->updateClass = $updateClass;
$prefix = $this->getLibPrefix();
if ( Puc_v4p11_Utils::startsWith($updateClass, $prefix) ) {
$state->updateBaseClass = substr($updateClass, strlen($prefix));
}
}
update_site_option($this->optionName, $state);
$this->isLoaded = true;
}
/**
* @return $this
*/
public function lazyLoad() {
if ( !$this->isLoaded ) {
$this->load();
}
return $this;
}
protected function load() {
$this->isLoaded = true;
$state = get_site_option($this->optionName, null);
if ( !is_object($state) ) {
$this->lastCheck = 0;
$this->checkedVersion = '';
$this->update = null;
return;
}
$this->lastCheck = intval(Puc_v4p11_Utils::get($state, 'lastCheck', 0));
$this->checkedVersion = Puc_v4p11_Utils::get($state, 'checkedVersion', '');
$this->update = null;
if ( isset($state->update) ) {
//This mess is due to the fact that the want the update class from this version
//of the library, not the version that saved the update.
$updateClass = null;
if ( isset($state->updateBaseClass) ) {
$updateClass = $this->getLibPrefix() . $state->updateBaseClass;
} else if ( isset($state->updateClass) && class_exists($state->updateClass) ) {
$updateClass = $state->updateClass;
}
if ( $updateClass !== null ) {
$this->update = call_user_func(array($updateClass, 'fromObject'), $state->update);
}
}
}
public function delete() {
delete_site_option($this->optionName);
$this->lastCheck = 0;
$this->checkedVersion = '';
$this->update = null;
}
private function getLibPrefix() {
$parts = explode('_', __CLASS__, 3);
return $parts[0] . '_' . $parts[1] . '_';
}
}
endif;
| php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/admin/update/Puc/v4p11/Utils.php | admin/update/Puc/v4p11/Utils.php | <?php
if ( !class_exists('Puc_v4p11_Utils', false) ):
class Puc_v4p11_Utils {
/**
* Get a value from a nested array or object based on a path.
*
* @param array|object|null $collection Get an entry from this array.
* @param array|string $path A list of array keys in hierarchy order, or a string path like "foo.bar.baz".
* @param mixed $default The value to return if the specified path is not found.
* @param string $separator Path element separator. Only applies to string paths.
* @return mixed
*/
public static function get($collection, $path, $default = null, $separator = '.') {
if ( is_string($path) ) {
$path = explode($separator, $path);
}
//Follow the $path into $input as far as possible.
$currentValue = $collection;
foreach ($path as $node) {
if ( is_array($currentValue) && isset($currentValue[$node]) ) {
$currentValue = $currentValue[$node];
} else if ( is_object($currentValue) && isset($currentValue->$node) ) {
$currentValue = $currentValue->$node;
} else {
return $default;
}
}
return $currentValue;
}
/**
* Get the first array element that is not empty.
*
* @param array $values
* @param mixed|null $default Returns this value if there are no non-empty elements.
* @return mixed|null
*/
public static function findNotEmpty($values, $default = null) {
if ( empty($values) ) {
return $default;
}
foreach ($values as $value) {
if ( !empty($value) ) {
return $value;
}
}
return $default;
}
/**
* Check if the input string starts with the specified prefix.
*
* @param string $input
* @param string $prefix
* @return bool
*/
public static function startsWith($input, $prefix) {
$length = strlen($prefix);
return (substr($input, 0, $length) === $prefix);
}
}
endif;
| php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/admin/update/Puc/v4p11/Scheduler.php | admin/update/Puc/v4p11/Scheduler.php | <?php
if ( !class_exists('Puc_v4p11_Scheduler', false) ):
/**
* The scheduler decides when and how often to check for updates.
* It calls @see Puc_v4p11_UpdateChecker::checkForUpdates() to perform the actual checks.
*/
class Puc_v4p11_Scheduler {
public $checkPeriod = 12; //How often to check for updates (in hours).
public $throttleRedundantChecks = false; //Check less often if we already know that an update is available.
public $throttledCheckPeriod = 72;
protected $hourlyCheckHooks = array('load-update.php');
/**
* @var Puc_v4p11_UpdateChecker
*/
protected $updateChecker;
private $cronHook = null;
/**
* Scheduler constructor.
*
* @param Puc_v4p11_UpdateChecker $updateChecker
* @param int $checkPeriod How often to check for updates (in hours).
* @param array $hourlyHooks
*/
public function __construct($updateChecker, $checkPeriod, $hourlyHooks = array('load-plugins.php')) {
$this->updateChecker = $updateChecker;
$this->checkPeriod = $checkPeriod;
//Set up the periodic update checks
$this->cronHook = $this->updateChecker->getUniqueName('cron_check_updates');
if ( $this->checkPeriod > 0 ){
//Trigger the check via Cron.
//Try to use one of the default schedules if possible as it's less likely to conflict
//with other plugins and their custom schedules.
$defaultSchedules = array(
1 => 'hourly',
12 => 'twicedaily',
24 => 'daily',
);
if ( array_key_exists($this->checkPeriod, $defaultSchedules) ) {
$scheduleName = $defaultSchedules[$this->checkPeriod];
} else {
//Use a custom cron schedule.
$scheduleName = 'every' . $this->checkPeriod . 'hours';
add_filter('cron_schedules', array($this, '_addCustomSchedule'));
}
if ( !wp_next_scheduled($this->cronHook) && !defined('WP_INSTALLING') ) {
//Randomly offset the schedule to help prevent update server traffic spikes. Without this
//most checks may happen during times of day when people are most likely to install new plugins.
$firstCheckTime = time() - rand(0, max($this->checkPeriod * 3600 - 15 * 60, 1));
$firstCheckTime = apply_filters(
$this->updateChecker->getUniqueName('first_check_time'),
$firstCheckTime
);
wp_schedule_event($firstCheckTime, $scheduleName, $this->cronHook);
}
add_action($this->cronHook, array($this, 'maybeCheckForUpdates'));
//In case Cron is disabled or unreliable, we also manually trigger
//the periodic checks while the user is browsing the Dashboard.
add_action( 'admin_init', array($this, 'maybeCheckForUpdates') );
//Like WordPress itself, we check more often on certain pages.
/** @see wp_update_plugins */
add_action('load-update-core.php', array($this, 'maybeCheckForUpdates'));
//"load-update.php" and "load-plugins.php" or "load-themes.php".
$this->hourlyCheckHooks = array_merge($this->hourlyCheckHooks, $hourlyHooks);
foreach($this->hourlyCheckHooks as $hook) {
add_action($hook, array($this, 'maybeCheckForUpdates'));
}
//This hook fires after a bulk update is complete.
add_action('upgrader_process_complete', array($this, 'upgraderProcessComplete'), 11, 2);
} else {
//Periodic checks are disabled.
wp_clear_scheduled_hook($this->cronHook);
}
}
/**
* Runs upon the WP action upgrader_process_complete.
*
* We look at the parameters to decide whether to call maybeCheckForUpdates() or not.
* We also check if the update checker has been removed by the update.
*
* @param WP_Upgrader $upgrader WP_Upgrader instance
* @param array $upgradeInfo extra information about the upgrade
*/
public function upgraderProcessComplete(
/** @noinspection PhpUnusedParameterInspection */
$upgrader, $upgradeInfo
) {
//Cancel all further actions if the current version of PUC has been deleted or overwritten
//by a different version during the upgrade. If we try to do anything more in that situation,
//we could trigger a fatal error by trying to autoload a deleted class.
clearstatcache();
if ( !file_exists(__FILE__) ) {
$this->removeHooks();
$this->updateChecker->removeHooks();
return;
}
//Sanity check and limitation to relevant types.
if (
!is_array($upgradeInfo) || !isset($upgradeInfo['type'], $upgradeInfo['action'])
|| 'update' !== $upgradeInfo['action'] || !in_array($upgradeInfo['type'], array('plugin', 'theme'))
) {
return;
}
//Filter out notifications of upgrades that should have no bearing upon whether or not our
//current info is up-to-date.
if ( is_a($this->updateChecker, 'Puc_v4p11_Theme_UpdateChecker') ) {
if ( 'theme' !== $upgradeInfo['type'] || !isset($upgradeInfo['themes']) ) {
return;
}
//Letting too many things going through for checks is not a real problem, so we compare widely.
if ( !in_array(
strtolower($this->updateChecker->directoryName),
array_map('strtolower', $upgradeInfo['themes'])
) ) {
return;
}
}
if ( is_a($this->updateChecker, 'Puc_v4p11_Plugin_UpdateChecker') ) {
if ( 'plugin' !== $upgradeInfo['type'] || !isset($upgradeInfo['plugins']) ) {
return;
}
//Themes pass in directory names in the information array, but plugins use the relative plugin path.
if ( !in_array(
strtolower($this->updateChecker->directoryName),
array_map('dirname', array_map('strtolower', $upgradeInfo['plugins']))
) ) {
return;
}
}
$this->maybeCheckForUpdates();
}
/**
* Check for updates if the configured check interval has already elapsed.
* Will use a shorter check interval on certain admin pages like "Dashboard -> Updates" or when doing cron.
*
* You can override the default behaviour by using the "puc_check_now-$slug" filter.
* The filter callback will be passed three parameters:
* - Current decision. TRUE = check updates now, FALSE = don't check now.
* - Last check time as a Unix timestamp.
* - Configured check period in hours.
* Return TRUE to check for updates immediately, or FALSE to cancel.
*
* This method is declared public because it's a hook callback. Calling it directly is not recommended.
*/
public function maybeCheckForUpdates() {
if ( empty($this->checkPeriod) ){
return;
}
$state = $this->updateChecker->getUpdateState();
$shouldCheck = ($state->timeSinceLastCheck() >= $this->getEffectiveCheckPeriod());
//Let plugin authors substitute their own algorithm.
$shouldCheck = apply_filters(
$this->updateChecker->getUniqueName('check_now'),
$shouldCheck,
$state->getLastCheck(),
$this->checkPeriod
);
if ( $shouldCheck ) {
$this->updateChecker->checkForUpdates();
}
}
/**
* Calculate the actual check period based on the current status and environment.
*
* @return int Check period in seconds.
*/
protected function getEffectiveCheckPeriod() {
$currentFilter = current_filter();
if ( in_array($currentFilter, array('load-update-core.php', 'upgrader_process_complete')) ) {
//Check more often when the user visits "Dashboard -> Updates" or does a bulk update.
$period = 60;
} else if ( in_array($currentFilter, $this->hourlyCheckHooks) ) {
//Also check more often on /wp-admin/update.php and the "Plugins" or "Themes" page.
$period = 3600;
} else if ( $this->throttleRedundantChecks && ($this->updateChecker->getUpdate() !== null) ) {
//Check less frequently if it's already known that an update is available.
$period = $this->throttledCheckPeriod * 3600;
} else if ( defined('DOING_CRON') && constant('DOING_CRON') ) {
//WordPress cron schedules are not exact, so lets do an update check even
//if slightly less than $checkPeriod hours have elapsed since the last check.
$cronFuzziness = 20 * 60;
$period = $this->checkPeriod * 3600 - $cronFuzziness;
} else {
$period = $this->checkPeriod * 3600;
}
return $period;
}
/**
* Add our custom schedule to the array of Cron schedules used by WP.
*
* @param array $schedules
* @return array
*/
public function _addCustomSchedule($schedules) {
if ( $this->checkPeriod && ($this->checkPeriod > 0) ){
$scheduleName = 'every' . $this->checkPeriod . 'hours';
$schedules[$scheduleName] = array(
'interval' => $this->checkPeriod * 3600,
'display' => sprintf('Every %d hours', $this->checkPeriod),
);
}
return $schedules;
}
/**
* Remove the scheduled cron event that the library uses to check for updates.
*
* @return void
*/
public function removeUpdaterCron() {
wp_clear_scheduled_hook($this->cronHook);
}
/**
* Get the name of the update checker's WP-cron hook. Mostly useful for debugging.
*
* @return string
*/
public function getCronHookName() {
return $this->cronHook;
}
/**
* Remove most hooks added by the scheduler.
*/
public function removeHooks() {
remove_filter('cron_schedules', array($this, '_addCustomSchedule'));
remove_action('admin_init', array($this, 'maybeCheckForUpdates'));
remove_action('load-update-core.php', array($this, 'maybeCheckForUpdates'));
if ( $this->cronHook !== null ) {
remove_action($this->cronHook, array($this, 'maybeCheckForUpdates'));
}
if ( !empty($this->hourlyCheckHooks) ) {
foreach ($this->hourlyCheckHooks as $hook) {
remove_action($hook, array($this, 'maybeCheckForUpdates'));
}
}
}
}
endif;
| php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/admin/update/Puc/v4p11/Factory.php | admin/update/Puc/v4p11/Factory.php | <?php
if ( !class_exists('Puc_v4p11_Factory', false) ):
/**
* A factory that builds update checker instances.
*
* When multiple versions of the same class have been loaded (e.g. PluginUpdateChecker 4.0
* and 4.1), this factory will always use the latest available minor version. Register class
* versions by calling {@link PucFactory::addVersion()}.
*
* At the moment it can only build instances of the UpdateChecker class. Other classes are
* intended mainly for internal use and refer directly to specific implementations.
*/
class Puc_v4p11_Factory {
protected static $classVersions = array();
protected static $sorted = false;
protected static $myMajorVersion = '';
protected static $latestCompatibleVersion = '';
/**
* A wrapper method for buildUpdateChecker() that reads the metadata URL from the plugin or theme header.
*
* @param string $fullPath Full path to the main plugin file or the theme's style.css.
* @param array $args Optional arguments. Keys should match the argument names of the buildUpdateChecker() method.
* @return Puc_v4p11_Plugin_UpdateChecker|Puc_v4p11_Theme_UpdateChecker|Puc_v4p11_Vcs_BaseChecker
*/
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);
}
/** @noinspection PhpUndefinedVariableInspection These variables are created by extract(), above. */
return self::buildUpdateChecker($metadataUrl, $fullPath, $slug, $checkPeriod, $optionName, $muPluginFile);
}
/**
* Create a new instance of the update checker.
*
* This method automatically detects if you're using it for a plugin or a theme and chooses
* the appropriate implementation for your update source (JSON file, GitHub, BitBucket, etc).
*
* @see Puc_v4p11_UpdateChecker::__construct
*
* @param string $metadataUrl The URL of the metadata file, a GitHub repository, or another supported update source.
* @param string $fullPath Full path to the main plugin file or to the theme directory.
* @param string $slug Custom slug. Defaults to the name of the main plugin file or the theme directory.
* @param int $checkPeriod How often to check for updates (in hours).
* @param string $optionName Where to store book-keeping info about update checks.
* @param string $muPluginFile The plugin filename relative to the mu-plugins directory.
* @return Puc_v4p11_Plugin_UpdateChecker|Puc_v4p11_Theme_UpdateChecker|Puc_v4p11_Vcs_BaseChecker
*/
public static function buildUpdateChecker($metadataUrl, $fullPath, $slug = '', $checkPeriod = 12, $optionName = '', $muPluginFile = '') {
$fullPath = self::normalizePath($fullPath);
$id = null;
//Plugin or theme?
$themeDirectory = self::getThemeDirectoryName($fullPath);
if ( self::isPluginFile($fullPath) ) {
$type = 'Plugin';
$id = $fullPath;
} else if ( $themeDirectory !== null ) {
$type = 'Theme';
$id = $themeDirectory;
} else {
throw new RuntimeException(sprintf(
'The update checker cannot determine if "%s" is a plugin or a theme. ' .
'This is a bug. Please contact the PUC developer.',
htmlentities($fullPath)
));
}
//Which hosting service does the URL point to?
$service = self::getVcsService($metadataUrl);
$apiClass = null;
if ( empty($service) ) {
//The default is to get update information from a remote JSON file.
$checkerClass = $type . '_UpdateChecker';
} else {
//You can also use a VCS repository like GitHub.
$checkerClass = 'Vcs_' . $type . 'UpdateChecker';
$apiClass = $service . 'Api';
}
$checkerClass = self::getCompatibleClassVersion($checkerClass);
if ( $checkerClass === null ) {
trigger_error(
sprintf(
'PUC %s does not support updates for %ss %s',
htmlentities(self::$latestCompatibleVersion),
strtolower($type),
$service ? ('hosted on ' . htmlentities($service)) : 'using JSON metadata'
),
E_USER_ERROR
);
return null;
}
//Add the current namespace to the class name(s).
if ( version_compare(PHP_VERSION, '5.3', '>=') ) {
$checkerClass = __NAMESPACE__ . '\\' . $checkerClass;
}
if ( !isset($apiClass) ) {
//Plain old update checker.
return new $checkerClass($metadataUrl, $id, $slug, $checkPeriod, $optionName, $muPluginFile);
} else {
//VCS checker + an API client.
$apiClass = self::getCompatibleClassVersion($apiClass);
if ( $apiClass === null ) {
trigger_error(sprintf(
'PUC %s does not support %s',
htmlentities(self::$latestCompatibleVersion),
htmlentities($service)
), E_USER_ERROR);
return null;
}
if ( version_compare(PHP_VERSION, '5.3', '>=') && (strpos($apiClass, '\\') === false) ) {
$apiClass = __NAMESPACE__ . '\\' . $apiClass;
}
return new $checkerClass(
new $apiClass($metadataUrl),
$id,
$slug,
$checkPeriod,
$optionName,
$muPluginFile
);
}
}
/**
*
* Normalize a filesystem path. Introduced in WP 3.9.
* Copying here allows use of the class on earlier versions.
* This version adapted from WP 4.8.2 (unchanged since 4.5.0)
*
* @param string $path Path to normalize.
* @return string Normalized path.
*/
public static function normalizePath($path) {
if ( function_exists('wp_normalize_path') ) {
return wp_normalize_path($path);
}
$path = str_replace('\\', '/', $path);
$path = preg_replace('|(?<=.)/+|', '/', $path);
if ( substr($path, 1, 1) === ':' ) {
$path = ucfirst($path);
}
return $path;
}
/**
* Check if the path points to a plugin file.
*
* @param string $absolutePath Normalized path.
* @return bool
*/
protected static function isPluginFile($absolutePath) {
//Is the file inside the "plugins" or "mu-plugins" directory?
$pluginDir = self::normalizePath(WP_PLUGIN_DIR);
$muPluginDir = self::normalizePath(WPMU_PLUGIN_DIR);
if ( (strpos($absolutePath, $pluginDir) === 0) || (strpos($absolutePath, $muPluginDir) === 0) ) {
return true;
}
//Is it a file at all? Caution: is_file() can fail if the parent dir. doesn't have the +x permission set.
if ( !is_file($absolutePath) ) {
return false;
}
//Does it have a valid plugin header?
//This is a last-ditch check for plugins symlinked from outside the WP root.
if ( function_exists('get_file_data') ) {
$headers = get_file_data($absolutePath, array('Name' => 'Plugin Name'), 'plugin');
return !empty($headers['Name']);
}
return false;
}
/**
* Get the name of the theme's directory from a full path to a file inside that directory.
* E.g. "/abc/public_html/wp-content/themes/foo/whatever.php" => "foo".
*
* Note that subdirectories are currently not supported. For example,
* "/xyz/wp-content/themes/my-theme/includes/whatever.php" => NULL.
*
* @param string $absolutePath Normalized path.
* @return string|null Directory name, or NULL if the path doesn't point to a theme.
*/
protected static function getThemeDirectoryName($absolutePath) {
if ( is_file($absolutePath) ) {
$absolutePath = dirname($absolutePath);
}
if ( file_exists($absolutePath . '/style.css') ) {
return basename($absolutePath);
}
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 throw an error.
throw new RuntimeException(
sprintf('Unable to locate URI in header of "%s"', htmlentities($fullPath))
);
}
/**
* Get the name of the hosting service that the URL points to.
*
* @param string $metadataUrl
* @return string|null
*/
private static function getVcsService($metadataUrl) {
$service = null;
//Which hosting service does the URL point to?
$host = parse_url($metadataUrl, PHP_URL_HOST);
$path = parse_url($metadataUrl, PHP_URL_PATH);
//Check if the path looks like "/user-name/repository".
//For GitLab.com it can also be "/user/group1/group2/.../repository".
$repoRegex = '@^/?([^/]+?)/([^/#?&]+?)/?$@';
if ( $host === 'gitlab.com' ) {
$repoRegex = '@^/?(?:[^/#?&]++/){1,20}(?:[^/#?&]++)/?$@';
}
if ( preg_match($repoRegex, $path) ) {
$knownServices = array(
'github.com' => 'GitHub',
'bitbucket.org' => 'BitBucket',
'gitlab.com' => 'GitLab',
);
if ( isset($knownServices[$host]) ) {
$service = $knownServices[$host];
}
}
return apply_filters('puc_get_vcs_service', $service, $host, $path, $metadataUrl);
}
/**
* Get the latest version of the specified class that has the same major version number
* as this factory class.
*
* @param string $class Partial class name.
* @return string|null Full class name.
*/
protected static function getCompatibleClassVersion($class) {
if ( isset(self::$classVersions[$class][self::$latestCompatibleVersion]) ) {
return self::$classVersions[$class][self::$latestCompatibleVersion];
}
return null;
}
/**
* Get the specific class name for the latest available version of a class.
*
* @param string $class
* @return null|string
*/
public static function getLatestClassVersion($class) {
if ( !self::$sorted ) {
self::sortVersions();
}
if ( isset(self::$classVersions[$class]) ) {
return reset(self::$classVersions[$class]);
} else {
return null;
}
}
/**
* Sort available class versions in descending order (i.e. newest first).
*/
protected static function sortVersions() {
foreach ( self::$classVersions as $class => $versions ) {
uksort($versions, array(__CLASS__, 'compareVersions'));
self::$classVersions[$class] = $versions;
}
self::$sorted = true;
}
protected static function compareVersions($a, $b) {
return -version_compare($a, $b);
}
/**
* Register a version of a class.
*
* @access private This method is only for internal use by the library.
*
* @param string $generalClass Class name without version numbers, e.g. 'PluginUpdateChecker'.
* @param string $versionedClass Actual class name, e.g. 'PluginUpdateChecker_1_2'.
* @param string $version Version number, e.g. '1.2'.
*/
public static function addVersion($generalClass, $versionedClass, $version) {
if ( empty(self::$myMajorVersion) ) {
$nameParts = explode('_', __CLASS__, 3);
self::$myMajorVersion = substr(ltrim($nameParts[1], 'v'), 0, 1);
}
//Store the greatest version number that matches our major version.
$components = explode('.', $version);
if ( $components[0] === self::$myMajorVersion ) {
if (
empty(self::$latestCompatibleVersion)
|| version_compare($version, self::$latestCompatibleVersion, '>')
) {
self::$latestCompatibleVersion = $version;
}
}
if ( !isset(self::$classVersions[$generalClass]) ) {
self::$classVersions[$generalClass] = array();
}
self::$classVersions[$generalClass][$version] = $versionedClass;
self::$sorted = false;
}
}
endif;
| php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/admin/update/Puc/v4p11/Theme/Update.php | admin/update/Puc/v4p11/Theme/Update.php | <?php
if ( !class_exists('Puc_v4p11_Theme_Update', false) ):
class Puc_v4p11_Theme_Update extends Puc_v4p11_Update {
public $details_url = '';
protected static $extraFields = array('details_url');
/**
* Transform the metadata into the format used by WordPress core.
* Note the inconsistency: WP stores plugin updates as objects and theme updates as arrays.
*
* @return array
*/
public function toWpFormat() {
$update = array(
'theme' => $this->slug,
'new_version' => $this->version,
'url' => $this->details_url,
);
if ( !empty($this->download_url) ) {
$update['package'] = $this->download_url;
}
return $update;
}
/**
* Create a new instance of Theme_Update from its JSON-encoded representation.
*
* @param string $json Valid JSON string representing a theme information object.
* @return self New instance of ThemeUpdate, or NULL on error.
*/
public static function fromJson($json) {
$instance = new self();
if ( !parent::createFromJson($json, $instance) ) {
return null;
}
return $instance;
}
/**
* Create a new instance by copying the necessary fields from another object.
*
* @param StdClass|Puc_v4p11_Theme_Update $object The source object.
* @return Puc_v4p11_Theme_Update The new copy.
*/
public static function fromObject($object) {
$update = new self();
$update->copyFields($object, $update);
return $update;
}
/**
* Basic validation.
*
* @param StdClass $apiResponse
* @return bool|WP_Error
*/
protected function validateMetadata($apiResponse) {
$required = array('version', 'details_url');
foreach($required as $key) {
if ( !isset($apiResponse->$key) || empty($apiResponse->$key) ) {
return new WP_Error(
'tuc-invalid-metadata',
sprintf('The theme metadata is missing the required "%s" key.', $key)
);
}
}
return true;
}
protected function getFieldNames() {
return array_merge(parent::getFieldNames(), self::$extraFields);
}
protected function getPrefixedFilter($tag) {
return parent::getPrefixedFilter($tag) . '_theme';
}
}
endif;
| php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/admin/update/Puc/v4p11/Theme/UpdateChecker.php | admin/update/Puc/v4p11/Theme/UpdateChecker.php | <?php
if ( !class_exists('Puc_v4p11_Theme_UpdateChecker', false) ):
class Puc_v4p11_Theme_UpdateChecker extends Puc_v4p11_UpdateChecker {
protected $filterSuffix = 'theme';
protected $updateTransient = 'update_themes';
protected $translationType = 'theme';
/**
* @var string Theme directory name.
*/
protected $stylesheet;
public function __construct($metadataUrl, $stylesheet = null, $customSlug = null, $checkPeriod = 12, $optionName = '') {
if ( $stylesheet === null ) {
$stylesheet = get_stylesheet();
}
$this->stylesheet = $stylesheet;
parent::__construct(
$metadataUrl,
$stylesheet,
$customSlug ? $customSlug : $stylesheet,
$checkPeriod,
$optionName
);
}
/**
* For themes, the update array is indexed by theme directory name.
*
* @return string
*/
protected function getUpdateListKey() {
return $this->directoryName;
}
/**
* Retrieve the latest update (if any) from the configured API endpoint.
*
* @return Puc_v4p11_Update|null An instance of Update, or NULL when no updates are available.
*/
public function requestUpdate() {
list($themeUpdate, $result) = $this->requestMetadata('Puc_v4p11_Theme_Update', 'request_update');
if ( $themeUpdate !== null ) {
/** @var Puc_v4p11_Theme_Update $themeUpdate */
$themeUpdate->slug = $this->slug;
}
$themeUpdate = $this->filterUpdateResult($themeUpdate, $result);
return $themeUpdate;
}
protected function getNoUpdateItemFields() {
return array_merge(
parent::getNoUpdateItemFields(),
array(
'theme' => $this->directoryName,
'requires' => '',
)
);
}
public function userCanInstallUpdates() {
return current_user_can('update_themes');
}
/**
* Create an instance of the scheduler.
*
* @param int $checkPeriod
* @return Puc_v4p11_Scheduler
*/
protected function createScheduler($checkPeriod) {
return new Puc_v4p11_Scheduler($this, $checkPeriod, array('load-themes.php'));
}
/**
* Is there an update being installed right now for this theme?
*
* @param WP_Upgrader|null $upgrader The upgrader that's performing the current update.
* @return bool
*/
public function isBeingUpgraded($upgrader = null) {
return $this->upgraderStatus->isThemeBeingUpgraded($this->stylesheet, $upgrader);
}
protected function createDebugBarExtension() {
return new Puc_v4p11_DebugBar_Extension($this, 'Puc_v4p11_DebugBar_ThemePanel');
}
/**
* Register a callback for filtering query arguments.
*
* The callback function should take one argument - an associative array of query arguments.
* It should return a modified array of query arguments.
*
* @param callable $callback
* @return void
*/
public function addQueryArgFilter($callback){
$this->addFilter('request_update_query_args', $callback);
}
/**
* Register a callback for filtering arguments passed to wp_remote_get().
*
* The callback function should take one argument - an associative array of arguments -
* and return a modified array or arguments. See the WP documentation on wp_remote_get()
* for details on what arguments are available and how they work.
*
* @uses add_filter() This method is a convenience wrapper for add_filter().
*
* @param callable $callback
* @return void
*/
public function addHttpRequestArgFilter($callback) {
$this->addFilter('request_update_options', $callback);
}
/**
* Register a callback for filtering theme updates retrieved from the external API.
*
* The callback function should take two arguments. If the theme update was retrieved
* successfully, the first argument passed will be an instance of Theme_Update. Otherwise,
* it will be NULL. The second argument will be the corresponding return value of
* wp_remote_get (see WP docs for details).
*
* The callback function should return a new or modified instance of Theme_Update or NULL.
*
* @uses add_filter() This method is a convenience wrapper for add_filter().
*
* @param callable $callback
* @return void
*/
public function addResultFilter($callback) {
$this->addFilter('request_update_result', $callback, 10, 2);
}
/**
* Create a package instance that represents this plugin or theme.
*
* @return Puc_v4p11_InstalledPackage
*/
protected function createInstalledPackage() {
return new Puc_v4p11_Theme_Package($this->stylesheet, $this);
}
}
endif;
| php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/admin/update/Puc/v4p11/Theme/Package.php | admin/update/Puc/v4p11/Theme/Package.php | <?php
if ( !class_exists('Puc_v4p11_Theme_Package', false) ):
class Puc_v4p11_Theme_Package extends Puc_v4p11_InstalledPackage {
/**
* @var string Theme directory name.
*/
protected $stylesheet;
/**
* @var WP_Theme Theme object.
*/
protected $theme;
public function __construct($stylesheet, $updateChecker) {
$this->stylesheet = $stylesheet;
$this->theme = wp_get_theme($this->stylesheet);
parent::__construct($updateChecker);
}
public function getInstalledVersion() {
return $this->theme->get('Version');
}
public function getAbsoluteDirectoryPath() {
if ( method_exists($this->theme, 'get_stylesheet_directory') ) {
return $this->theme->get_stylesheet_directory(); //Available since WP 3.4.
}
return get_theme_root($this->stylesheet) . '/' . $this->stylesheet;
}
/**
* Get the value of a specific plugin or theme header.
*
* @param string $headerName
* @param string $defaultValue
* @return string Either the value of the header, or $defaultValue if the header doesn't exist or is empty.
*/
public function getHeaderValue($headerName, $defaultValue = '') {
$value = $this->theme->get($headerName);
if ( ($headerName === false) || ($headerName === '') ) {
return $defaultValue;
}
return $value;
}
protected function getHeaderNames() {
return array(
'Name' => 'Theme Name',
'ThemeURI' => 'Theme URI',
'Description' => 'Description',
'Author' => 'Author',
'AuthorURI' => 'Author URI',
'Version' => 'Version',
'Template' => 'Template',
'Status' => 'Status',
'Tags' => 'Tags',
'TextDomain' => 'Text Domain',
'DomainPath' => 'Domain Path',
);
}
}
endif;
| php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/admin/update/Puc/v4p11/DebugBar/Extension.php | admin/update/Puc/v4p11/DebugBar/Extension.php | <?php
if ( !class_exists('Puc_v4p11_DebugBar_Extension', false) ):
class Puc_v4p11_DebugBar_Extension {
const RESPONSE_BODY_LENGTH_LIMIT = 4000;
/** @var Puc_v4p11_UpdateChecker */
protected $updateChecker;
protected $panelClass = 'Puc_v4p11_DebugBar_Panel';
public function __construct($updateChecker, $panelClass = null) {
$this->updateChecker = $updateChecker;
if ( isset($panelClass) ) {
$this->panelClass = $panelClass;
}
if ( version_compare(PHP_VERSION, '5.3', '>=') && (strpos($this->panelClass, '\\') === false) ) {
$this->panelClass = __NAMESPACE__ . '\\' . $this->panelClass;
}
add_filter('debug_bar_panels', array($this, 'addDebugBarPanel'));
add_action('debug_bar_enqueue_scripts', array($this, 'enqueuePanelDependencies'));
add_action('wp_ajax_puc_v4_debug_check_now', array($this, 'ajaxCheckNow'));
}
/**
* Register the PUC Debug Bar panel.
*
* @param array $panels
* @return array
*/
public function addDebugBarPanel($panels) {
if ( $this->updateChecker->userCanInstallUpdates() ) {
$panels[] = new $this->panelClass($this->updateChecker);
}
return $panels;
}
/**
* Enqueue our Debug Bar scripts and styles.
*/
public function enqueuePanelDependencies() {
wp_enqueue_style(
'puc-debug-bar-style-v4',
$this->getLibraryUrl(i_static()."/admin/update/css/puc-debug-bar.css"),
array('debug-bar'),
'20171124'
);
wp_enqueue_script(
'puc-debug-bar-js-v4',
$this->getLibraryUrl(i_static()."/admin/update/js/debug-bar.js"),
array('jquery'),
'20201209'
);
}
/**
* Run an update check and output the result. Useful for making sure that
* the update checking process works as expected.
*/
public function ajaxCheckNow() {
if ( $_POST['uid'] !== $this->updateChecker->getUniqueName('uid') ) {
return;
}
$this->preAjaxRequest();
$update = $this->updateChecker->checkForUpdates();
if ( $update !== null ) {
echo "An update is available:";
echo '<pre>', htmlentities(print_r($update, true)), '</pre>';
} else {
echo 'No updates found.';
}
$errors = $this->updateChecker->getLastRequestApiErrors();
if ( !empty($errors) ) {
printf('<p>The update checker encountered %d API error%s.</p>', count($errors), (count($errors) > 1) ? 's' : '');
foreach (array_values($errors) as $num => $item) {
$wpError = $item['error'];
/** @var WP_Error $wpError */
printf('<h4>%d) %s</h4>', $num + 1, esc_html($wpError->get_error_message()));
echo '<dl>';
printf('<dt>Error code:</dt><dd><code>%s</code></dd>', esc_html($wpError->get_error_code()));
if ( isset($item['url']) ) {
printf('<dt>Requested URL:</dt><dd><code>%s</code></dd>', esc_html($item['url']));
}
if ( isset($item['httpResponse']) ) {
if ( is_wp_error($item['httpResponse']) ) {
$httpError = $item['httpResponse'];
/** @var WP_Error $httpError */
printf(
'<dt>WordPress HTTP API error:</dt><dd>%s (<code>%s</code>)</dd>',
esc_html($httpError->get_error_message()),
esc_html($httpError->get_error_code())
);
} else {
//Status code.
printf(
'<dt>HTTP status:</dt><dd><code>%d %s</code></dd>',
wp_remote_retrieve_response_code($item['httpResponse']),
wp_remote_retrieve_response_message($item['httpResponse'])
);
//Headers.
echo '<dt>Response headers:</dt><dd><pre>';
foreach (wp_remote_retrieve_headers($item['httpResponse']) as $name => $value) {
printf("%s: %s\n", esc_html($name), esc_html($value));
}
echo '</pre></dd>';
//Body.
$body = wp_remote_retrieve_body($item['httpResponse']);
if ( $body === '' ) {
$body = '(Empty response.)';
} else if ( strlen($body) > self::RESPONSE_BODY_LENGTH_LIMIT ) {
$length = strlen($body);
$body = substr($body, 0, self::RESPONSE_BODY_LENGTH_LIMIT)
. sprintf("\n(Long string truncated. Total length: %d bytes.)", $length);
}
printf('<dt>Response body:</dt><dd><pre>%s</pre></dd>', esc_html($body));
}
}
echo '<dl>';
}
}
exit;
}
/**
* Check access permissions and enable error display (for debugging).
*/
protected function preAjaxRequest() {
if ( !$this->updateChecker->userCanInstallUpdates() ) {
die('Access denied');
}
check_ajax_referer('puc-ajax');
error_reporting(E_ALL);
@ini_set('display_errors', 'On');
}
/**
* Remove hooks that were added by this extension.
*/
public function removeHooks() {
remove_filter('debug_bar_panels', array($this, 'addDebugBarPanel'));
remove_action('debug_bar_enqueue_scripts', array($this, 'enqueuePanelDependencies'));
remove_action('wp_ajax_puc_v4_debug_check_now', array($this, 'ajaxCheckNow'));
}
/**
* @param string $filePath
* @return string
*/
private function getLibraryUrl($filePath) {
$absolutePath = realpath(dirname(__FILE__) . '/../../../' . ltrim($filePath, '/'));
//Where is the library located inside the WordPress directory structure?
$absolutePath = Puc_v4p11_Factory::normalizePath($absolutePath);
$pluginDir = Puc_v4p11_Factory::normalizePath(WP_PLUGIN_DIR);
$muPluginDir = Puc_v4p11_Factory::normalizePath(WPMU_PLUGIN_DIR);
$themeDir = Puc_v4p11_Factory::normalizePath(get_theme_root());
if ( (strpos($absolutePath, $pluginDir) === 0) || (strpos($absolutePath, $muPluginDir) === 0) ) {
//It's part of a plugin.
return plugins_url(basename($absolutePath), $absolutePath);
} else if ( strpos($absolutePath, $themeDir) === 0 ) {
//It's part of a theme.
$relativePath = substr($absolutePath, strlen($themeDir) + 1);
$template = substr($relativePath, 0, strpos($relativePath, '/'));
$baseUrl = get_theme_root_uri($template);
if ( !empty($baseUrl) && $relativePath ) {
return $baseUrl . '/' . $relativePath;
}
}
return '';
}
}
endif;
| php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/admin/update/Puc/v4p11/DebugBar/PluginExtension.php | admin/update/Puc/v4p11/DebugBar/PluginExtension.php | <?php
if ( !class_exists('Puc_v4p11_DebugBar_PluginExtension', false) ):
class Puc_v4p11_DebugBar_PluginExtension extends Puc_v4p11_DebugBar_Extension {
/** @var Puc_v4p11_Plugin_UpdateChecker */
protected $updateChecker;
public function __construct($updateChecker) {
parent::__construct($updateChecker, 'Puc_v4p11_DebugBar_PluginPanel');
add_action('wp_ajax_puc_v4_debug_request_info', array($this, 'ajaxRequestInfo'));
}
/**
* Request plugin info and output it.
*/
public function ajaxRequestInfo() {
if ( $_POST['uid'] !== $this->updateChecker->getUniqueName('uid') ) {
return;
}
$this->preAjaxRequest();
$info = $this->updateChecker->requestInfo();
if ( $info !== null ) {
echo 'Successfully retrieved plugin info from the metadata URL:';
echo '<pre>', htmlentities(print_r($info, true)), '</pre>';
} else {
echo 'Failed to retrieve plugin info from the metadata URL.';
}
exit;
}
}
endif;
| php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/admin/update/Puc/v4p11/DebugBar/PluginPanel.php | admin/update/Puc/v4p11/DebugBar/PluginPanel.php | <?php
if ( !class_exists('Puc_v4p11_DebugBar_PluginPanel', false) ):
class Puc_v4p11_DebugBar_PluginPanel extends Puc_v4p11_DebugBar_Panel {
/**
* @var Puc_v4p11_Plugin_UpdateChecker
*/
protected $updateChecker;
protected function displayConfigHeader() {
$this->row('Plugin file', htmlentities($this->updateChecker->pluginFile));
parent::displayConfigHeader();
}
protected function getMetadataButton() {
$requestInfoButton = '';
if ( function_exists('get_submit_button') ) {
$requestInfoButton = get_submit_button(
'Request Info',
'secondary',
'puc-request-info-button',
false,
array('id' => $this->updateChecker->getUniqueName('request-info-button'))
);
}
return $requestInfoButton;
}
protected function getUpdateFields() {
return array_merge(
parent::getUpdateFields(),
array('homepage', 'upgrade_notice', 'tested',)
);
}
}
endif;
| php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/admin/update/Puc/v4p11/DebugBar/Panel.php | admin/update/Puc/v4p11/DebugBar/Panel.php | <?php
if ( !class_exists('Puc_v4p11_DebugBar_Panel', false) && class_exists('Debug_Bar_Panel', false) ):
class Puc_v4p11_DebugBar_Panel extends Debug_Bar_Panel {
/** @var Puc_v4p11_UpdateChecker */
protected $updateChecker;
private $responseBox = '<div class="puc-ajax-response" style="display: none;"></div>';
public function __construct($updateChecker) {
$this->updateChecker = $updateChecker;
$title = sprintf(
'<span class="puc-debug-menu-link-%s">PUC (%s)</span>',
esc_attr($this->updateChecker->getUniqueName('uid')),
$this->updateChecker->slug
);
parent::__construct($title);
}
public function render() {
printf(
'<div class="puc-debug-bar-panel-v4" id="%1$s" data-slug="%2$s" data-uid="%3$s" data-nonce="%4$s">',
esc_attr($this->updateChecker->getUniqueName('debug-bar-panel')),
esc_attr($this->updateChecker->slug),
esc_attr($this->updateChecker->getUniqueName('uid')),
esc_attr(wp_create_nonce('puc-ajax'))
);
$this->displayConfiguration();
$this->displayStatus();
$this->displayCurrentUpdate();
echo '</div>';
}
private function displayConfiguration() {
echo '<h3>Configuration</h3>';
echo '<table class="puc-debug-data">';
$this->displayConfigHeader();
$this->row('Slug', htmlentities($this->updateChecker->slug));
$this->row('DB option', htmlentities($this->updateChecker->optionName));
$requestInfoButton = $this->getMetadataButton();
$this->row('Metadata URL', htmlentities($this->updateChecker->metadataUrl) . ' ' . $requestInfoButton . $this->responseBox);
$scheduler = $this->updateChecker->scheduler;
if ( $scheduler->checkPeriod > 0 ) {
$this->row('Automatic checks', 'Every ' . $scheduler->checkPeriod . ' hours');
} else {
$this->row('Automatic checks', 'Disabled');
}
if ( isset($scheduler->throttleRedundantChecks) ) {
if ( $scheduler->throttleRedundantChecks && ($scheduler->checkPeriod > 0) ) {
$this->row(
'Throttling',
sprintf(
'Enabled. If an update is already available, check for updates every %1$d hours instead of every %2$d hours.',
$scheduler->throttledCheckPeriod,
$scheduler->checkPeriod
)
);
} else {
$this->row('Throttling', 'Disabled');
}
}
$this->updateChecker->onDisplayConfiguration($this);
echo '</table>';
}
protected function displayConfigHeader() {
//Do nothing. This should be implemented in subclasses.
}
protected function getMetadataButton() {
return '';
}
private function displayStatus() {
echo '<h3>Status</h3>';
echo '<table class="puc-debug-data">';
$state = $this->updateChecker->getUpdateState();
$checkNowButton = '';
if ( function_exists('get_submit_button') ) {
$checkNowButton = get_submit_button(
'Check Now',
'secondary',
'puc-check-now-button',
false,
array('id' => $this->updateChecker->getUniqueName('check-now-button'))
);
}
if ( $state->getLastCheck() > 0 ) {
$this->row('Last check', $this->formatTimeWithDelta($state->getLastCheck()) . ' ' . $checkNowButton . $this->responseBox);
} else {
$this->row('Last check', 'Never');
}
$nextCheck = wp_next_scheduled($this->updateChecker->scheduler->getCronHookName());
$this->row('Next automatic check', $this->formatTimeWithDelta($nextCheck));
if ( $state->getCheckedVersion() !== '' ) {
$this->row('Checked version', htmlentities($state->getCheckedVersion()));
$this->row('Cached update', $state->getUpdate());
}
$this->row('Update checker class', htmlentities(get_class($this->updateChecker)));
echo '</table>';
}
private function displayCurrentUpdate() {
$update = $this->updateChecker->getUpdate();
if ( $update !== null ) {
echo '<h3>An Update Is Available</h3>';
echo '<table class="puc-debug-data">';
$fields = $this->getUpdateFields();
foreach($fields as $field) {
if ( property_exists($update, $field) ) {
$this->row(ucwords(str_replace('_', ' ', $field)), htmlentities($update->$field));
}
}
echo '</table>';
} else {
echo '<h3>No updates currently available</h3>';
}
}
protected function getUpdateFields() {
return array('version', 'download_url', 'slug',);
}
private function formatTimeWithDelta($unixTime) {
if ( empty($unixTime) ) {
return 'Never';
}
$delta = time() - $unixTime;
$result = human_time_diff(time(), $unixTime);
if ( $delta < 0 ) {
$result = 'after ' . $result;
} else {
$result = $result . ' ago';
}
$result .= ' (' . $this->formatTimestamp($unixTime) . ')';
return $result;
}
private function formatTimestamp($unixTime) {
return gmdate('Y-m-d H:i:s', $unixTime + (get_option('gmt_offset') * 3600));
}
public function row($name, $value) {
if ( is_object($value) || is_array($value) ) {
$value = '<pre>' . htmlentities(print_r($value, true)) . '</pre>';
} else if ($value === null) {
$value = '<code>null</code>';
}
printf('<tr><th scope="row">%1$s</th> <td>%2$s</td></tr>', $name, $value);
}
}
endif;
| php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/admin/update/Puc/v4p11/DebugBar/ThemePanel.php | admin/update/Puc/v4p11/DebugBar/ThemePanel.php | <?php
if ( !class_exists('Puc_v4p11_DebugBar_ThemePanel', false) ):
class Puc_v4p11_DebugBar_ThemePanel extends Puc_v4p11_DebugBar_Panel {
/**
* @var Puc_v4p11_Theme_UpdateChecker
*/
protected $updateChecker;
protected function displayConfigHeader() {
$this->row('Theme directory', htmlentities($this->updateChecker->directoryName));
parent::displayConfigHeader();
}
protected function getUpdateFields() {
return array_merge(parent::getUpdateFields(), array('details_url'));
}
}
endif;
| php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/admin/update/Puc/v4p11/Plugin/Ui.php | admin/update/Puc/v4p11/Plugin/Ui.php | <?php
if ( !class_exists('Puc_v4p11_Plugin_Ui', false) ):
/**
* Additional UI elements for plugins.
*/
class Puc_v4p11_Plugin_Ui {
private $updateChecker;
private $manualCheckErrorTransient = '';
/**
* @param Puc_v4p11_Plugin_UpdateChecker $updateChecker
*/
public function __construct($updateChecker) {
$this->updateChecker = $updateChecker;
$this->manualCheckErrorTransient = $this->updateChecker->getUniqueName('manual_check_errors');
add_action('admin_init', array($this, 'onAdminInit'));
}
public function onAdminInit() {
if ( $this->updateChecker->userCanInstallUpdates() ) {
$this->handleManualCheck();
add_filter('plugin_row_meta', array($this, 'addViewDetailsLink'), 10, 3);
add_filter('plugin_row_meta', array($this, 'addCheckForUpdatesLink'), 10, 2);
add_action('all_admin_notices', array($this, 'displayManualCheckResult'));
}
}
/**
* Add a "View Details" link to the plugin row in the "Plugins" page. By default,
* the new link will appear before the "Visit plugin site" link (if present).
*
* You can change the link text by using the "puc_view_details_link-$slug" filter.
* Returning an empty string from the filter will disable the link.
*
* You can change the position of the link using the
* "puc_view_details_link_position-$slug" filter.
* Returning 'before' or 'after' will place the link immediately before/after
* the "Visit plugin site" link.
* Returning 'append' places the link after any existing links at the time of the hook.
* Returning 'replace' replaces the "Visit plugin site" link.
* Returning anything else disables the link when there is a "Visit plugin site" link.
*
* If there is no "Visit plugin site" link 'append' is always used!
*
* @param array $pluginMeta Array of meta links.
* @param string $pluginFile
* @param array $pluginData Array of plugin header data.
* @return array
*/
public function addViewDetailsLink($pluginMeta, $pluginFile, $pluginData = array()) {
if ( $this->isMyPluginFile($pluginFile) && !isset($pluginData['slug']) ) {
$linkText = apply_filters($this->updateChecker->getUniqueName('view_details_link'), __('View details'));
if ( !empty($linkText) ) {
$viewDetailsLinkPosition = 'append';
//Find the "Visit plugin site" link (if present).
$visitPluginSiteLinkIndex = count($pluginMeta) - 1;
if ( $pluginData['PluginURI'] ) {
$escapedPluginUri = esc_url($pluginData['PluginURI']);
foreach ($pluginMeta as $linkIndex => $existingLink) {
if ( strpos($existingLink, $escapedPluginUri) !== false ) {
$visitPluginSiteLinkIndex = $linkIndex;
$viewDetailsLinkPosition = apply_filters(
$this->updateChecker->getUniqueName('view_details_link_position'),
'before'
);
break;
}
}
}
$viewDetailsLink = sprintf('<a href="%s" class="thickbox open-plugin-details-modal" aria-label="%s" data-title="%s">%s</a>',
esc_url(network_admin_url('plugin-install.php?tab=plugin-information&plugin=' . urlencode($this->updateChecker->slug) .
'&TB_iframe=true&width=600&height=550')),
esc_attr(sprintf(__('More information about %s'), $pluginData['Name'])),
esc_attr($pluginData['Name']),
$linkText
);
switch ($viewDetailsLinkPosition) {
case 'before':
array_splice($pluginMeta, $visitPluginSiteLinkIndex, 0, $viewDetailsLink);
break;
case 'after':
array_splice($pluginMeta, $visitPluginSiteLinkIndex + 1, 0, $viewDetailsLink);
break;
case 'replace':
$pluginMeta[$visitPluginSiteLinkIndex] = $viewDetailsLink;
break;
case 'append':
default:
$pluginMeta[] = $viewDetailsLink;
break;
}
}
}
return $pluginMeta;
}
/**
* Add a "Check for updates" link to the plugin row in the "Plugins" page. By default,
* the new link will appear after the "Visit plugin site" link if present, otherwise
* after the "View plugin details" link.
*
* You can change the link text by using the "puc_manual_check_link-$slug" filter.
* Returning an empty string from the filter will disable the link.
*
* @param array $pluginMeta Array of meta links.
* @param string $pluginFile
* @return array
*/
public function addCheckForUpdatesLink($pluginMeta, $pluginFile) {
if ( $this->isMyPluginFile($pluginFile) ) {
$linkUrl = wp_nonce_url(
add_query_arg(
array(
'puc_check_for_updates' => 1,
'puc_slug' => $this->updateChecker->slug,
),
self_admin_url('plugins.php')
),
'puc_check_for_updates'
);
$linkText = apply_filters(
$this->updateChecker->getUniqueName('manual_check_link'),
__('Check for updates', 'plugin-update-checker')
);
if ( !empty($linkText) ) {
/** @noinspection HtmlUnknownTarget */
$pluginMeta[] = sprintf('<a href="%s">%s</a>', esc_attr($linkUrl), $linkText);
}
}
return $pluginMeta;
}
protected function isMyPluginFile($pluginFile) {
return ($pluginFile == $this->updateChecker->pluginFile)
|| (!empty($this->updateChecker->muPluginFile) && ($pluginFile == $this->updateChecker->muPluginFile));
}
/**
* Check for updates when the user clicks the "Check for updates" link.
*
* @see self::addCheckForUpdatesLink()
*
* @return void
*/
public function handleManualCheck() {
$shouldCheck =
isset($_GET['puc_check_for_updates'], $_GET['puc_slug'])
&& $_GET['puc_slug'] == $this->updateChecker->slug
&& check_admin_referer('puc_check_for_updates');
if ( $shouldCheck ) {
$update = $this->updateChecker->checkForUpdates();
$status = ($update === null) ? 'no_update' : 'update_available';
$lastRequestApiErrors = $this->updateChecker->getLastRequestApiErrors();
if ( ($update === null) && !empty($lastRequestApiErrors) ) {
//Some errors are not critical. For example, if PUC tries to retrieve the readme.txt
//file from GitHub and gets a 404, that's an API error, but it doesn't prevent updates
//from working. Maybe the plugin simply doesn't have a readme.
//Let's only show important errors.
$foundCriticalErrors = false;
$questionableErrorCodes = array(
'puc-github-http-error',
'puc-gitlab-http-error',
'puc-bitbucket-http-error',
);
foreach ($lastRequestApiErrors as $item) {
$wpError = $item['error'];
/** @var WP_Error $wpError */
if ( !in_array($wpError->get_error_code(), $questionableErrorCodes) ) {
$foundCriticalErrors = true;
break;
}
}
if ( $foundCriticalErrors ) {
$status = 'error';
set_site_transient($this->manualCheckErrorTransient, $lastRequestApiErrors, 60);
}
}
wp_redirect(add_query_arg(
array(
'puc_update_check_result' => $status,
'puc_slug' => $this->updateChecker->slug,
),
self_admin_url('plugins.php')
));
exit;
}
}
/**
* Display the results of a manual update check.
*
* @see self::handleManualCheck()
*
* You can change the result message by using the "puc_manual_check_message-$slug" filter.
*/
public function displayManualCheckResult() {
if ( isset($_GET['puc_update_check_result'], $_GET['puc_slug']) && ($_GET['puc_slug'] == $this->updateChecker->slug) ) {
$status = strval($_GET['puc_update_check_result']);
$title = $this->updateChecker->getInstalledPackage()->getPluginTitle();
$noticeClass = 'updated notice-success';
$details = '';
if ( $status == 'no_update' ) {
$message = sprintf(_x('The %s plugin is up to date.', 'the plugin title', 'plugin-update-checker'), $title);
} else if ( $status == 'update_available' ) {
$message = sprintf(_x('A new version of the %s plugin is available.', 'the plugin title', 'plugin-update-checker'), $title);
} else if ( $status === 'error' ) {
$message = sprintf(_x('Could not determine if updates are available for %s.', 'the plugin title', 'plugin-update-checker'), $title);
$noticeClass = 'error notice-error';
$details = $this->formatManualCheckErrors(get_site_transient($this->manualCheckErrorTransient));
delete_site_transient($this->manualCheckErrorTransient);
} else {
$message = sprintf(__('Unknown update checker status "%s"', 'plugin-update-checker'), htmlentities($status));
$noticeClass = 'error notice-error';
}
printf(
'<div class="notice %s is-dismissible"><p>%s</p>%s</div>',
$noticeClass,
apply_filters($this->updateChecker->getUniqueName('manual_check_message'), $message, $status),
$details
);
}
}
/**
* Format the list of errors that were thrown during an update check.
*
* @param array $errors
* @return string
*/
protected function formatManualCheckErrors($errors) {
if ( empty($errors) ) {
return '';
}
$output = '';
$showAsList = count($errors) > 1;
if ( $showAsList ) {
$output .= '<ol>';
$formatString = '<li>%1$s <code>%2$s</code></li>';
} else {
$formatString = '<p>%1$s <code>%2$s</code></p>';
}
foreach ($errors as $item) {
$wpError = $item['error'];
/** @var WP_Error $wpError */
$output .= sprintf(
$formatString,
$wpError->get_error_message(),
$wpError->get_error_code()
);
}
if ( $showAsList ) {
$output .= '</ol>';
}
return $output;
}
public function removeHooks() {
remove_action('admin_init', array($this, 'onAdminInit'));
remove_filter('plugin_row_meta', array($this, 'addViewDetailsLink'), 10);
remove_filter('plugin_row_meta', array($this, 'addCheckForUpdatesLink'), 10);
remove_action('all_admin_notices', array($this, 'displayManualCheckResult'));
}
}
endif;
| php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/admin/update/Puc/v4p11/Plugin/Update.php | admin/update/Puc/v4p11/Plugin/Update.php | <?php
if ( !class_exists('Puc_v4p11_Plugin_Update', false) ):
/**
* A simple container class for holding information about an available update.
*
* @author Janis Elsts
* @copyright 2016
* @access public
*/
class Puc_v4p11_Plugin_Update extends Puc_v4p11_Update {
public $id = 0;
public $homepage;
public $upgrade_notice;
public $tested;
public $requires_php = false;
public $icons = array();
public $filename; //Plugin filename relative to the plugins directory.
protected static $extraFields = array(
'id', 'homepage', 'tested', 'requires_php', 'upgrade_notice', 'icons', 'filename',
);
/**
* Create a new instance of PluginUpdate from its JSON-encoded representation.
*
* @param string $json
* @return Puc_v4p11_Plugin_Update|null
*/
public static function fromJson($json){
//Since update-related information is simply a subset of the full plugin info,
//we can parse the update JSON as if it was a plugin info string, then copy over
//the parts that we care about.
$pluginInfo = Puc_v4p11_Plugin_Info::fromJson($json);
if ( $pluginInfo !== null ) {
return self::fromPluginInfo($pluginInfo);
} else {
return null;
}
}
/**
* Create a new instance of PluginUpdate based on an instance of PluginInfo.
* Basically, this just copies a subset of fields from one object to another.
*
* @param Puc_v4p11_Plugin_Info $info
* @return Puc_v4p11_Plugin_Update
*/
public static function fromPluginInfo($info){
return self::fromObject($info);
}
/**
* Create a new instance by copying the necessary fields from another object.
*
* @param StdClass|Puc_v4p11_Plugin_Info|Puc_v4p11_Plugin_Update $object The source object.
* @return Puc_v4p11_Plugin_Update The new copy.
*/
public static function fromObject($object) {
$update = new self();
$update->copyFields($object, $update);
return $update;
}
/**
* @return string[]
*/
protected function getFieldNames() {
return array_merge(parent::getFieldNames(), self::$extraFields);
}
/**
* Transform the update into the format used by WordPress native plugin API.
*
* @return object
*/
public function toWpFormat() {
$update = parent::toWpFormat();
$update->id = $this->id;
$update->url = $this->homepage;
$update->tested = $this->tested;
$update->requires_php = $this->requires_php;
$update->plugin = $this->filename;
if ( !empty($this->upgrade_notice) ) {
$update->upgrade_notice = $this->upgrade_notice;
}
if ( !empty($this->icons) && is_array($this->icons) ) {
//This should be an array with up to 4 keys: 'svg', '1x', '2x' and 'default'.
//Docs: https://developer.wordpress.org/plugins/wordpress-org/plugin-assets/#plugin-icons
$icons = array_intersect_key(
$this->icons,
array('svg' => true, '1x' => true, '2x' => true, 'default' => true)
);
if ( !empty($icons) ) {
$update->icons = $icons;
//It appears that the 'default' icon isn't used anywhere in WordPress 4.9,
//but lets set it just in case a future release needs it.
if ( !isset($update->icons['default']) ) {
$update->icons['default'] = current($update->icons);
}
}
}
return $update;
}
}
endif;
| php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/admin/update/Puc/v4p11/Plugin/UpdateChecker.php | admin/update/Puc/v4p11/Plugin/UpdateChecker.php | <?php
if ( !class_exists('Puc_v4p11_Plugin_UpdateChecker', false) ):
/**
* A custom plugin update checker.
*
* @author Janis Elsts
* @copyright 2018
* @access public
*/
class Puc_v4p11_Plugin_UpdateChecker extends Puc_v4p11_UpdateChecker {
protected $updateTransient = 'update_plugins';
protected $translationType = 'plugin';
public $pluginAbsolutePath = ''; //Full path of the main plugin file.
public $pluginFile = ''; //Plugin filename relative to the plugins directory. Many WP APIs use this to identify plugins.
public $muPluginFile = ''; //For MU plugins, the plugin filename relative to the mu-plugins directory.
/**
* @var Puc_v4p11_Plugin_Package
*/
protected $package;
private $extraUi = null;
/**
* Class constructor.
*
* @param string $metadataUrl The URL of the plugin's metadata file.
* @param string $pluginFile Fully qualified path to the main plugin file.
* @param string $slug The plugin's 'slug'. If not specified, the filename part of $pluginFile sans '.php' will be used as the slug.
* @param integer $checkPeriod How often to check for updates (in hours). Defaults to checking every 12 hours. Set to 0 to disable automatic update checks.
* @param string $optionName Where to store book-keeping info about update checks. Defaults to 'external_updates-$slug'.
* @param string $muPluginFile Optional. The plugin filename relative to the mu-plugins directory.
*/
public function __construct($metadataUrl, $pluginFile, $slug = '', $checkPeriod = 12, $optionName = '', $muPluginFile = ''){
$this->pluginAbsolutePath = $pluginFile;
$this->pluginFile = plugin_basename($this->pluginAbsolutePath);
$this->muPluginFile = $muPluginFile;
//If no slug is specified, use the name of the main plugin file as the slug.
//For example, 'my-cool-plugin/cool-plugin.php' becomes 'cool-plugin'.
if ( empty($slug) ){
$slug = basename($this->pluginFile, '.php');
}
//Plugin slugs must be unique.
$slugCheckFilter = 'puc_is_slug_in_use-' . $slug;
$slugUsedBy = apply_filters($slugCheckFilter, false);
if ( $slugUsedBy ) {
$this->triggerError(sprintf(
'Plugin slug "%s" is already in use by %s. Slugs must be unique.',
htmlentities($slug),
htmlentities($slugUsedBy)
), E_USER_ERROR);
}
add_filter($slugCheckFilter, array($this, 'getAbsolutePath'));
parent::__construct($metadataUrl, dirname($this->pluginFile), $slug, $checkPeriod, $optionName);
//Backwards compatibility: If the plugin is a mu-plugin but no $muPluginFile is specified, assume
//it's the same as $pluginFile given that it's not in a subdirectory (WP only looks in the base dir).
if ( (strpbrk($this->pluginFile, '/\\') === false) && $this->isUnknownMuPlugin() ) {
$this->muPluginFile = $this->pluginFile;
}
//To prevent a crash during plugin uninstallation, remove updater hooks when the user removes the plugin.
//Details: https://github.com/YahnisElsts/plugin-update-checker/issues/138#issuecomment-335590964
add_action('uninstall_' . $this->pluginFile, array($this, 'removeHooks'));
$this->extraUi = new Puc_v4p11_Plugin_Ui($this);
}
/**
* Create an instance of the scheduler.
*
* @param int $checkPeriod
* @return Puc_v4p11_Scheduler
*/
protected function createScheduler($checkPeriod) {
$scheduler = new Puc_v4p11_Scheduler($this, $checkPeriod, array('load-plugins.php'));
register_deactivation_hook($this->pluginFile, array($scheduler, 'removeUpdaterCron'));
return $scheduler;
}
/**
* Install the hooks required to run periodic update checks and inject update info
* into WP data structures.
*
* @return void
*/
protected function installHooks(){
//Override requests for plugin information
add_filter('plugins_api', array($this, 'injectInfo'), 20, 3);
parent::installHooks();
}
/**
* Remove update checker hooks.
*
* The intent is to prevent a fatal error that can happen if the plugin has an uninstall
* hook. During uninstallation, WP includes the main plugin file (which creates a PUC instance),
* the uninstall hook runs, WP deletes the plugin files and then updates some transients.
* If PUC hooks are still around at this time, they could throw an error while trying to
* autoload classes from files that no longer exist.
*
* The "site_transient_{$transient}" filter is the main problem here, but let's also remove
* most other PUC hooks to be safe.
*
* @internal
*/
public function removeHooks() {
parent::removeHooks();
$this->extraUi->removeHooks();
$this->package->removeHooks();
remove_filter('plugins_api', array($this, 'injectInfo'), 20);
}
/**
* Retrieve plugin info from the configured API endpoint.
*
* @uses wp_remote_get()
*
* @param array $queryArgs Additional query arguments to append to the request. Optional.
* @return Puc_v4p11_Plugin_Info
*/
public function requestInfo($queryArgs = array()) {
list($pluginInfo, $result) = $this->requestMetadata('Puc_v4p11_Plugin_Info', 'request_info', $queryArgs);
if ( $pluginInfo !== null ) {
/** @var Puc_v4p11_Plugin_Info $pluginInfo */
$pluginInfo->filename = $this->pluginFile;
$pluginInfo->slug = $this->slug;
}
$pluginInfo = apply_filters($this->getUniqueName('request_info_result'), $pluginInfo, $result);
return $pluginInfo;
}
/**
* Retrieve the latest update (if any) from the configured API endpoint.
*
* @uses PluginUpdateChecker::requestInfo()
*
* @return Puc_v4p11_Update|null An instance of Plugin_Update, or NULL when no updates are available.
*/
public function requestUpdate() {
//For the sake of simplicity, this function just calls requestInfo()
//and transforms the result accordingly.
$pluginInfo = $this->requestInfo(array('checking_for_updates' => '1'));
if ( $pluginInfo === null ){
return null;
}
$update = Puc_v4p11_Plugin_Update::fromPluginInfo($pluginInfo);
$update = $this->filterUpdateResult($update);
return $update;
}
/**
* Intercept plugins_api() calls that request information about our plugin and
* use the configured API endpoint to satisfy them.
*
* @see plugins_api()
*
* @param mixed $result
* @param string $action
* @param array|object $args
* @return mixed
*/
public function injectInfo($result, $action = null, $args = null){
$relevant = ($action == 'plugin_information') && isset($args->slug) && (
($args->slug == $this->slug) || ($args->slug == dirname($this->pluginFile))
);
if ( !$relevant ) {
return $result;
}
$pluginInfo = $this->requestInfo();
$this->fixSupportedWordpressVersion($pluginInfo);
$pluginInfo = apply_filters($this->getUniqueName('pre_inject_info'), $pluginInfo);
if ( $pluginInfo ) {
return $pluginInfo->toWpFormat();
}
return $result;
}
protected function shouldShowUpdates() {
//No update notifications for mu-plugins unless explicitly enabled. The MU plugin file
//is usually different from the main plugin file so the update wouldn't show up properly anyway.
return !$this->isUnknownMuPlugin();
}
/**
* @param stdClass|null $updates
* @param stdClass $updateToAdd
* @return stdClass
*/
protected function addUpdateToList($updates, $updateToAdd) {
if ( $this->package->isMuPlugin() ) {
//WP does not support automatic update installation for mu-plugins, but we can
//still display a notice.
$updateToAdd->package = null;
}
return parent::addUpdateToList($updates, $updateToAdd);
}
/**
* @param stdClass|null $updates
* @return stdClass|null
*/
protected function removeUpdateFromList($updates) {
$updates = parent::removeUpdateFromList($updates);
if ( !empty($this->muPluginFile) && isset($updates, $updates->response) ) {
unset($updates->response[$this->muPluginFile]);
}
return $updates;
}
/**
* For plugins, the update array is indexed by the plugin filename relative to the "plugins"
* directory. Example: "plugin-name/plugin.php".
*
* @return string
*/
protected function getUpdateListKey() {
if ( $this->package->isMuPlugin() ) {
return $this->muPluginFile;
}
return $this->pluginFile;
}
protected function getNoUpdateItemFields() {
return array_merge(
parent::getNoUpdateItemFields(),
array(
'id' => $this->pluginFile,
'slug' => $this->slug,
'plugin' => $this->pluginFile,
'icons' => array(),
'banners' => array(),
'banners_rtl' => array(),
'tested' => '',
'compatibility' => new stdClass(),
)
);
}
/**
* Alias for isBeingUpgraded().
*
* @deprecated
* @param WP_Upgrader|null $upgrader The upgrader that's performing the current update.
* @return bool
*/
public function isPluginBeingUpgraded($upgrader = null) {
return $this->isBeingUpgraded($upgrader);
}
/**
* Is there an update being installed for this plugin, right now?
*
* @param WP_Upgrader|null $upgrader
* @return bool
*/
public function isBeingUpgraded($upgrader = null) {
return $this->upgraderStatus->isPluginBeingUpgraded($this->pluginFile, $upgrader);
}
/**
* Get the details of the currently available update, if any.
*
* If no updates are available, or if the last known update version is below or equal
* to the currently installed version, this method will return NULL.
*
* Uses cached update data. To retrieve update information straight from
* the metadata URL, call requestUpdate() instead.
*
* @return Puc_v4p11_Plugin_Update|null
*/
public function getUpdate() {
$update = parent::getUpdate();
if ( isset($update) ) {
/** @var Puc_v4p11_Plugin_Update $update */
$update->filename = $this->pluginFile;
}
return $update;
}
/**
* Get the translated plugin title.
*
* @deprecated
* @return string
*/
public function getPluginTitle() {
return $this->package->getPluginTitle();
}
/**
* Check if the current user has the required permissions to install updates.
*
* @return bool
*/
public function userCanInstallUpdates() {
return current_user_can('update_plugins');
}
/**
* Check if the plugin file is inside the mu-plugins directory.
*
* @deprecated
* @return bool
*/
protected function isMuPlugin() {
return $this->package->isMuPlugin();
}
/**
* MU plugins are partially supported, but only when we know which file in mu-plugins
* corresponds to this plugin.
*
* @return bool
*/
protected function isUnknownMuPlugin() {
return empty($this->muPluginFile) && $this->package->isMuPlugin();
}
/**
* Get absolute path to the main plugin file.
*
* @return string
*/
public function getAbsolutePath() {
return $this->pluginAbsolutePath;
}
/**
* Register a callback for filtering query arguments.
*
* The callback function should take one argument - an associative array of query arguments.
* It should return a modified array of query arguments.
*
* @uses add_filter() This method is a convenience wrapper for add_filter().
*
* @param callable $callback
* @return void
*/
public function addQueryArgFilter($callback){
$this->addFilter('request_info_query_args', $callback);
}
/**
* Register a callback for filtering arguments passed to wp_remote_get().
*
* The callback function should take one argument - an associative array of arguments -
* and return a modified array or arguments. See the WP documentation on wp_remote_get()
* for details on what arguments are available and how they work.
*
* @uses add_filter() This method is a convenience wrapper for add_filter().
*
* @param callable $callback
* @return void
*/
public function addHttpRequestArgFilter($callback) {
$this->addFilter('request_info_options', $callback);
}
/**
* Register a callback for filtering the plugin info retrieved from the external API.
*
* The callback function should take two arguments. If the plugin info was retrieved
* successfully, the first argument passed will be an instance of PluginInfo. Otherwise,
* it will be NULL. The second argument will be the corresponding return value of
* wp_remote_get (see WP docs for details).
*
* The callback function should return a new or modified instance of PluginInfo or NULL.
*
* @uses add_filter() This method is a convenience wrapper for add_filter().
*
* @param callable $callback
* @return void
*/
public function addResultFilter($callback) {
$this->addFilter('request_info_result', $callback, 10, 2);
}
protected function createDebugBarExtension() {
return new Puc_v4p11_DebugBar_PluginExtension($this);
}
/**
* Create a package instance that represents this plugin or theme.
*
* @return Puc_v4p11_InstalledPackage
*/
protected function createInstalledPackage() {
return new Puc_v4p11_Plugin_Package($this->pluginAbsolutePath, $this);
}
/**
* @return Puc_v4p11_Plugin_Package
*/
public function getInstalledPackage() {
return $this->package;
}
}
endif;
| php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/admin/update/Puc/v4p11/Plugin/Info.php | admin/update/Puc/v4p11/Plugin/Info.php | <?php
if ( !class_exists('Puc_v4p11_Plugin_Info', false) ):
/**
* A container class for holding and transforming various plugin metadata.
*
* @author Janis Elsts
* @copyright 2016
* @access public
*/
class Puc_v4p11_Plugin_Info extends Puc_v4p11_Metadata {
//Most fields map directly to the contents of the plugin's info.json file.
//See the relevant docs for a description of their meaning.
public $name;
public $slug;
public $version;
public $homepage;
public $sections = array();
public $download_url;
public $banners;
public $icons = array();
public $translations = array();
public $author;
public $author_homepage;
public $requires;
public $tested;
public $requires_php;
public $upgrade_notice;
public $rating;
public $num_ratings;
public $downloaded;
public $active_installs;
public $last_updated;
public $id = 0; //The native WP.org API returns numeric plugin IDs, but they're not used for anything.
public $filename; //Plugin filename relative to the plugins directory.
/**
* Create a new instance of Plugin Info from JSON-encoded plugin info
* returned by an external update API.
*
* @param string $json Valid JSON string representing plugin info.
* @return self|null New instance of Plugin Info, or NULL on error.
*/
public static function fromJson($json){
$instance = new self();
if ( !parent::createFromJson($json, $instance) ) {
return null;
}
//json_decode decodes assoc. arrays as objects. We want them as arrays.
$instance->sections = (array)$instance->sections;
$instance->icons = (array)$instance->icons;
return $instance;
}
/**
* Very, very basic validation.
*
* @param StdClass $apiResponse
* @return bool|WP_Error
*/
protected function validateMetadata($apiResponse) {
if (
!isset($apiResponse->name, $apiResponse->version)
|| empty($apiResponse->name)
|| empty($apiResponse->version)
) {
return new WP_Error(
'puc-invalid-metadata',
"The plugin metadata file does not contain the required 'name' and/or 'version' keys."
);
}
return true;
}
/**
* Transform plugin info into the format used by the native WordPress.org API
*
* @return object
*/
public function toWpFormat(){
$info = new stdClass;
//The custom update API is built so that many fields have the same name and format
//as those returned by the native WordPress.org API. These can be assigned directly.
$sameFormat = array(
'name', 'slug', 'version', 'requires', 'tested', 'rating', 'upgrade_notice',
'num_ratings', 'downloaded', 'active_installs', 'homepage', 'last_updated',
'requires_php',
);
foreach($sameFormat as $field){
if ( isset($this->$field) ) {
$info->$field = $this->$field;
} else {
$info->$field = null;
}
}
//Other fields need to be renamed and/or transformed.
$info->download_link = $this->download_url;
$info->author = $this->getFormattedAuthor();
$info->sections = array_merge(array('description' => ''), $this->sections);
if ( !empty($this->banners) ) {
//WP expects an array with two keys: "high" and "low". Both are optional.
//Docs: https://wordpress.org/plugins/about/faq/#banners
$info->banners = is_object($this->banners) ? get_object_vars($this->banners) : $this->banners;
$info->banners = array_intersect_key($info->banners, array('high' => true, 'low' => true));
}
return $info;
}
protected function getFormattedAuthor() {
if ( !empty($this->author_homepage) ){
/** @noinspection HtmlUnknownTarget */
return sprintf('<a href="%s">%s</a>', $this->author_homepage, $this->author);
}
return $this->author;
}
}
endif;
| php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/admin/update/Puc/v4p11/Plugin/Package.php | admin/update/Puc/v4p11/Plugin/Package.php | <?php
if ( !class_exists('Puc_v4p11_Plugin_Package', false) ):
class Puc_v4p11_Plugin_Package extends Puc_v4p11_InstalledPackage {
/**
* @var Puc_v4p11_Plugin_UpdateChecker
*/
protected $updateChecker;
/**
* @var string Full path of the main plugin file.
*/
protected $pluginAbsolutePath = '';
/**
* @var string Plugin basename.
*/
private $pluginFile;
/**
* @var string|null
*/
private $cachedInstalledVersion = null;
public function __construct($pluginAbsolutePath, $updateChecker) {
$this->pluginAbsolutePath = $pluginAbsolutePath;
$this->pluginFile = plugin_basename($this->pluginAbsolutePath);
parent::__construct($updateChecker);
//Clear the version number cache when something - anything - is upgraded or WP clears the update cache.
add_filter('upgrader_post_install', array($this, 'clearCachedVersion'));
add_action('delete_site_transient_update_plugins', array($this, 'clearCachedVersion'));
}
public function getInstalledVersion() {
if ( isset($this->cachedInstalledVersion) ) {
return $this->cachedInstalledVersion;
}
$pluginHeader = $this->getPluginHeader();
if ( isset($pluginHeader['Version']) ) {
$this->cachedInstalledVersion = $pluginHeader['Version'];
return $pluginHeader['Version'];
} else {
//This can happen if the filename points to something that is not a plugin.
$this->updateChecker->triggerError(
sprintf(
"Can't to read the Version header for '%s'. The filename is incorrect or is not a plugin.",
$this->updateChecker->pluginFile
),
E_USER_WARNING
);
return null;
}
}
/**
* Clear the cached plugin version. This method can be set up as a filter (hook) and will
* return the filter argument unmodified.
*
* @param mixed $filterArgument
* @return mixed
*/
public function clearCachedVersion($filterArgument = null) {
$this->cachedInstalledVersion = null;
return $filterArgument;
}
public function getAbsoluteDirectoryPath() {
return dirname($this->pluginAbsolutePath);
}
/**
* Get the value of a specific plugin or theme header.
*
* @param string $headerName
* @param string $defaultValue
* @return string Either the value of the header, or $defaultValue if the header doesn't exist or is empty.
*/
public function getHeaderValue($headerName, $defaultValue = '') {
$headers = $this->getPluginHeader();
if ( isset($headers[$headerName]) && ($headers[$headerName] !== '') ) {
return $headers[$headerName];
}
return $defaultValue;
}
protected function getHeaderNames() {
return array(
'Name' => 'Plugin Name',
'PluginURI' => 'Plugin URI',
'Version' => 'Version',
'Description' => 'Description',
'Author' => 'Author',
'AuthorURI' => 'Author URI',
'TextDomain' => 'Text Domain',
'DomainPath' => 'Domain Path',
'Network' => 'Network',
//The newest WordPress version that this plugin requires or has been tested with.
//We support several different formats for compatibility with other libraries.
'Tested WP' => 'Tested WP',
'Requires WP' => 'Requires WP',
'Tested up to' => 'Tested up to',
'Requires at least' => 'Requires at least',
);
}
/**
* Get the translated plugin title.
*
* @return string
*/
public function getPluginTitle() {
$title = '';
$header = $this->getPluginHeader();
if ( $header && !empty($header['Name']) && isset($header['TextDomain']) ) {
$title = translate($header['Name'], $header['TextDomain']);
}
return $title;
}
/**
* Get plugin's metadata from its file header.
*
* @return array
*/
public function getPluginHeader() {
if ( !is_file($this->pluginAbsolutePath) ) {
//This can happen if the plugin filename is wrong.
$this->updateChecker->triggerError(
sprintf(
"Can't to read the plugin header for '%s'. The file does not exist.",
$this->updateChecker->pluginFile
),
E_USER_WARNING
);
return array();
}
if ( !function_exists('get_plugin_data') ) {
/** @noinspection PhpIncludeInspection */
require_once(ABSPATH . '/wp-admin/includes/plugin.php');
}
return get_plugin_data($this->pluginAbsolutePath, false, false);
}
public function removeHooks() {
remove_filter('upgrader_post_install', array($this, 'clearCachedVersion'));
remove_action('delete_site_transient_update_plugins', array($this, 'clearCachedVersion'));
}
/**
* Check if the plugin file is inside the mu-plugins directory.
*
* @return bool
*/
public function isMuPlugin() {
static $cachedResult = null;
if ( $cachedResult === null ) {
if ( !defined('WPMU_PLUGIN_DIR') || !is_string(WPMU_PLUGIN_DIR) ) {
$cachedResult = false;
return $cachedResult;
}
//Convert both paths to the canonical form before comparison.
$muPluginDir = realpath(WPMU_PLUGIN_DIR);
$pluginPath = realpath($this->pluginAbsolutePath);
//If realpath() fails, just normalize the syntax instead.
if (($muPluginDir === false) || ($pluginPath === false)) {
$muPluginDir = Puc_v4p11_Factory::normalizePath(WPMU_PLUGIN_DIR);
$pluginPath = Puc_v4p11_Factory::normalizePath($this->pluginAbsolutePath);
}
$cachedResult = (strpos($pluginPath, $muPluginDir) === 0);
}
return $cachedResult;
}
}
endif;
| php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/admin/update/Puc/v4p11/Vcs/Api.php | admin/update/Puc/v4p11/Vcs/Api.php | <?php
if ( !class_exists('Puc_v4p11_Vcs_Api') ):
abstract class Puc_v4p11_Vcs_Api {
protected $tagNameProperty = 'name';
protected $slug = '';
/**
* @var string
*/
protected $repositoryUrl = '';
/**
* @var mixed Authentication details for private repositories. Format depends on service.
*/
protected $credentials = null;
/**
* @var string The filter tag that's used to filter options passed to wp_remote_get.
* For example, "puc_request_info_options-slug" or "puc_request_update_options_theme-slug".
*/
protected $httpFilterName = '';
/**
* @var string|null
*/
protected $localDirectory = null;
/**
* Puc_v4p11_Vcs_Api constructor.
*
* @param string $repositoryUrl
* @param array|string|null $credentials
*/
public function __construct($repositoryUrl, $credentials = null) {
$this->repositoryUrl = $repositoryUrl;
$this->setAuthentication($credentials);
}
/**
* @return string
*/
public function getRepositoryUrl() {
return $this->repositoryUrl;
}
/**
* Figure out which reference (i.e tag or branch) contains the latest version.
*
* @param string $configBranch Start looking in this branch.
* @return null|Puc_v4p11_Vcs_Reference
*/
abstract public function chooseReference($configBranch);
/**
* Get the readme.txt file from the remote repository and parse it
* according to the plugin readme standard.
*
* @param string $ref Tag or branch name.
* @return array Parsed readme.
*/
public function getRemoteReadme($ref = 'master') {
$fileContents = $this->getRemoteFile($this->getLocalReadmeName(), $ref);
if ( empty($fileContents) ) {
return array();
}
$parser = new PucReadmeParser();
return $parser->parse_readme_contents($fileContents);
}
/**
* Get the case-sensitive name of the local readme.txt file.
*
* In most cases it should just be called "readme.txt", but some plugins call it "README.txt",
* "README.TXT", or even "Readme.txt". Most VCS are case-sensitive so we need to know the correct
* capitalization.
*
* Defaults to "readme.txt" (all lowercase).
*
* @return string
*/
public function getLocalReadmeName() {
static $fileName = null;
if ( $fileName !== null ) {
return $fileName;
}
$fileName = 'readme.txt';
if ( isset($this->localDirectory) ) {
$files = scandir($this->localDirectory);
if ( !empty($files) ) {
foreach ($files as $possibleFileName) {
if ( strcasecmp($possibleFileName, 'readme.txt') === 0 ) {
$fileName = $possibleFileName;
break;
}
}
}
}
return $fileName;
}
/**
* Get a branch.
*
* @param string $branchName
* @return Puc_v4p11_Vcs_Reference|null
*/
abstract public function getBranch($branchName);
/**
* Get a specific tag.
*
* @param string $tagName
* @return Puc_v4p11_Vcs_Reference|null
*/
abstract public function getTag($tagName);
/**
* Get the tag that looks like the highest version number.
* (Implementations should skip pre-release versions if possible.)
*
* @return Puc_v4p11_Vcs_Reference|null
*/
abstract public function getLatestTag();
/**
* Check if a tag name string looks like a version number.
*
* @param string $name
* @return bool
*/
protected function looksLikeVersion($name) {
//Tag names may be prefixed with "v", e.g. "v1.2.3".
$name = ltrim($name, 'v');
//The version string must start with a number.
if ( !is_numeric(substr($name, 0, 1)) ) {
return false;
}
//The goal is to accept any SemVer-compatible or "PHP-standardized" version number.
return (preg_match('@^(\d{1,5}?)(\.\d{1,10}?){0,4}?($|[abrdp+_\-]|\s)@i', $name) === 1);
}
/**
* Check if a tag appears to be named like a version number.
*
* @param stdClass $tag
* @return bool
*/
protected function isVersionTag($tag) {
$property = $this->tagNameProperty;
return isset($tag->$property) && $this->looksLikeVersion($tag->$property);
}
/**
* Sort a list of tags as if they were version numbers.
* Tags that don't look like version number will be removed.
*
* @param stdClass[] $tags Array of tag objects.
* @return stdClass[] Filtered array of tags sorted in descending order.
*/
protected function sortTagsByVersion($tags) {
//Keep only those tags that look like version numbers.
$versionTags = array_filter($tags, array($this, 'isVersionTag'));
//Sort them in descending order.
usort($versionTags, array($this, 'compareTagNames'));
return $versionTags;
}
/**
* Compare two tags as if they were version number.
*
* @param stdClass $tag1 Tag object.
* @param stdClass $tag2 Another tag object.
* @return int
*/
protected function compareTagNames($tag1, $tag2) {
$property = $this->tagNameProperty;
if ( !isset($tag1->$property) ) {
return 1;
}
if ( !isset($tag2->$property) ) {
return -1;
}
return -version_compare(ltrim($tag1->$property, 'v'), ltrim($tag2->$property, 'v'));
}
/**
* Get the contents of a file from a specific branch or tag.
*
* @param string $path File name.
* @param string $ref
* @return null|string Either the contents of the file, or null if the file doesn't exist or there's an error.
*/
abstract public function getRemoteFile($path, $ref = 'master');
/**
* Get the timestamp of the latest commit that changed the specified branch or tag.
*
* @param string $ref Reference name (e.g. branch or tag).
* @return string|null
*/
abstract public function getLatestCommitTime($ref);
/**
* Get the contents of the changelog file from the repository.
*
* @param string $ref
* @param string $localDirectory Full path to the local plugin or theme directory.
* @return null|string The HTML contents of the changelog.
*/
public function getRemoteChangelog($ref, $localDirectory) {
$filename = $this->findChangelogName($localDirectory);
if ( empty($filename) ) {
return null;
}
$changelog = $this->getRemoteFile($filename, $ref);
if ( $changelog === null ) {
return null;
}
/** @noinspection PhpUndefinedClassInspection */
return Parsedown::instance()->text($changelog);
}
/**
* Guess the name of the changelog file.
*
* @param string $directory
* @return string|null
*/
protected function findChangelogName($directory = null) {
if ( !isset($directory) ) {
$directory = $this->localDirectory;
}
if ( empty($directory) || !is_dir($directory) || ($directory === '.') ) {
return null;
}
$possibleNames = array('CHANGES.md', 'CHANGELOG.md', 'changes.md', 'changelog.md');
$files = scandir($directory);
$foundNames = array_intersect($possibleNames, $files);
if ( !empty($foundNames) ) {
return reset($foundNames);
}
return null;
}
/**
* Set authentication credentials.
*
* @param $credentials
*/
public function setAuthentication($credentials) {
$this->credentials = $credentials;
}
public function isAuthenticationEnabled() {
return !empty($this->credentials);
}
/**
* @param string $url
* @return string
*/
public function signDownloadUrl($url) {
return $url;
}
/**
* @param string $filterName
*/
public function setHttpFilterName($filterName) {
$this->httpFilterName = $filterName;
}
/**
* @param string $directory
*/
public function setLocalDirectory($directory) {
if ( empty($directory) || !is_dir($directory) || ($directory === '.') ) {
$this->localDirectory = null;
} else {
$this->localDirectory = $directory;
}
}
/**
* @param string $slug
*/
public function setSlug($slug) {
$this->slug = $slug;
}
}
endif;
| php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/admin/update/Puc/v4p11/Vcs/PluginUpdateChecker.php | admin/update/Puc/v4p11/Vcs/PluginUpdateChecker.php | <?php
if ( !class_exists('Puc_v4p11_Vcs_PluginUpdateChecker') ):
class Puc_v4p11_Vcs_PluginUpdateChecker extends Puc_v4p11_Plugin_UpdateChecker implements Puc_v4p11_Vcs_BaseChecker {
/**
* @var string The branch where to look for updates. Defaults to "master".
*/
protected $branch = 'master';
/**
* @var Puc_v4p11_Vcs_Api Repository API client.
*/
protected $api = null;
/**
* Puc_v4p11_Vcs_PluginUpdateChecker constructor.
*
* @param Puc_v4p11_Vcs_Api $api
* @param string $pluginFile
* @param string $slug
* @param int $checkPeriod
* @param string $optionName
* @param string $muPluginFile
*/
public function __construct($api, $pluginFile, $slug = '', $checkPeriod = 12, $optionName = '', $muPluginFile = '') {
$this->api = $api;
$this->api->setHttpFilterName($this->getUniqueName('request_info_options'));
parent::__construct($api->getRepositoryUrl(), $pluginFile, $slug, $checkPeriod, $optionName, $muPluginFile);
$this->api->setSlug($this->slug);
}
public function requestInfo($unusedParameter = null) {
//We have to make several remote API requests to gather all the necessary info
//which can take a while on slow networks.
if ( function_exists('set_time_limit') ) {
@set_time_limit(60);
}
$api = $this->api;
$api->setLocalDirectory($this->package->getAbsoluteDirectoryPath());
$info = new Puc_v4p11_Plugin_Info();
$info->filename = $this->pluginFile;
$info->slug = $this->slug;
$this->setInfoFromHeader($this->package->getPluginHeader(), $info);
$this->setIconsFromLocalAssets($info);
$this->setBannersFromLocalAssets($info);
//Pick a branch or tag.
$updateSource = $api->chooseReference($this->branch);
if ( $updateSource ) {
$ref = $updateSource->name;
$info->version = $updateSource->version;
$info->last_updated = $updateSource->updated;
$info->download_url = $updateSource->downloadUrl;
if ( !empty($updateSource->changelog) ) {
$info->sections['changelog'] = $updateSource->changelog;
}
if ( isset($updateSource->downloadCount) ) {
$info->downloaded = $updateSource->downloadCount;
}
} else {
//There's probably a network problem or an authentication error.
do_action(
'puc_api_error',
new WP_Error(
'puc-no-update-source',
'Could not retrieve version information from the repository. '
. 'This usually means that the update checker either can\'t connect '
. 'to the repository or it\'s configured incorrectly.'
),
null, null, $this->slug
);
return null;
}
//Get headers from the main plugin file in this branch/tag. Its "Version" header and other metadata
//are what the WordPress install will actually see after upgrading, so they take precedence over releases/tags.
$mainPluginFile = basename($this->pluginFile);
$remotePlugin = $api->getRemoteFile($mainPluginFile, $ref);
if ( !empty($remotePlugin) ) {
$remoteHeader = $this->package->getFileHeader($remotePlugin);
$this->setInfoFromHeader($remoteHeader, $info);
}
//Try parsing readme.txt. If it's formatted according to WordPress.org standards, it will contain
//a lot of useful information like the required/tested WP version, changelog, and so on.
if ( $this->readmeTxtExistsLocally() ) {
$this->setInfoFromRemoteReadme($ref, $info);
}
//The changelog might be in a separate file.
if ( empty($info->sections['changelog']) ) {
$info->sections['changelog'] = $api->getRemoteChangelog($ref, $this->package->getAbsoluteDirectoryPath());
if ( empty($info->sections['changelog']) ) {
$info->sections['changelog'] = __('There is no changelog available.', 'plugin-update-checker');
}
}
if ( empty($info->last_updated) ) {
//Fetch the latest commit that changed the tag or branch and use it as the "last_updated" date.
$latestCommitTime = $api->getLatestCommitTime($ref);
if ( $latestCommitTime !== null ) {
$info->last_updated = $latestCommitTime;
}
}
$info = apply_filters($this->getUniqueName('request_info_result'), $info, null);
return $info;
}
/**
* Check if the currently installed version has a readme.txt file.
*
* @return bool
*/
protected function readmeTxtExistsLocally() {
return $this->package->fileExists($this->api->getLocalReadmeName());
}
/**
* Copy plugin metadata from a file header to a Plugin Info object.
*
* @param array $fileHeader
* @param Puc_v4p11_Plugin_Info $pluginInfo
*/
protected function setInfoFromHeader($fileHeader, $pluginInfo) {
$headerToPropertyMap = array(
'Version' => 'version',
'Name' => 'name',
'PluginURI' => 'homepage',
'Author' => 'author',
'AuthorName' => 'author',
'AuthorURI' => 'author_homepage',
'Requires WP' => 'requires',
'Tested WP' => 'tested',
'Requires at least' => 'requires',
'Tested up to' => 'tested',
'Requires PHP' => 'requires_php',
);
foreach ($headerToPropertyMap as $headerName => $property) {
if ( isset($fileHeader[$headerName]) && !empty($fileHeader[$headerName]) ) {
$pluginInfo->$property = $fileHeader[$headerName];
}
}
if ( !empty($fileHeader['Description']) ) {
$pluginInfo->sections['description'] = $fileHeader['Description'];
}
}
/**
* Copy plugin metadata from the remote readme.txt file.
*
* @param string $ref GitHub tag or branch where to look for the readme.
* @param Puc_v4p11_Plugin_Info $pluginInfo
*/
protected function setInfoFromRemoteReadme($ref, $pluginInfo) {
$readme = $this->api->getRemoteReadme($ref);
if ( empty($readme) ) {
return;
}
if ( isset($readme['sections']) ) {
$pluginInfo->sections = array_merge($pluginInfo->sections, $readme['sections']);
}
if ( !empty($readme['tested_up_to']) ) {
$pluginInfo->tested = $readme['tested_up_to'];
}
if ( !empty($readme['requires_at_least']) ) {
$pluginInfo->requires = $readme['requires_at_least'];
}
if ( !empty($readme['requires_php']) ) {
$pluginInfo->requires_php = $readme['requires_php'];
}
if ( isset($readme['upgrade_notice'], $readme['upgrade_notice'][$pluginInfo->version]) ) {
$pluginInfo->upgrade_notice = $readme['upgrade_notice'][$pluginInfo->version];
}
}
/**
* Add icons from the currently installed version to a Plugin Info object.
*
* The icons should be in a subdirectory named "assets". Supported image formats
* and file names are described here:
* @link https://developer.wordpress.org/plugins/wordpress-org/plugin-assets/#plugin-icons
*
* @param Puc_v4p11_Plugin_Info $pluginInfo
*/
protected function setIconsFromLocalAssets($pluginInfo) {
$icons = $this->getLocalAssetUrls(array(
'icon.svg' => 'svg',
'icon-256x256.png' => '2x',
'icon-256x256.jpg' => '2x',
'icon-128x128.png' => '1x',
'icon-128x128.jpg' => '1x',
));
if ( !empty($icons) ) {
//The "default" key seems to be used only as last-resort fallback in WP core (5.8/5.9),
//but we'll set it anyway in case some code somewhere needs it.
reset($icons);
$firstKey = key($icons);
$icons['default'] = $icons[$firstKey];
$pluginInfo->icons = $icons;
}
}
/**
* Add banners from the currently installed version to a Plugin Info object.
*
* The banners should be in a subdirectory named "assets". Supported image formats
* and file names are described here:
* @link https://developer.wordpress.org/plugins/wordpress-org/plugin-assets/#plugin-headers
*
* @param Puc_v4p11_Plugin_Info $pluginInfo
*/
protected function setBannersFromLocalAssets($pluginInfo) {
$banners = $this->getLocalAssetUrls(array(
'banner-772x250.png' => 'high',
'banner-772x250.jpg' => 'high',
'banner-1544x500.png' => 'low',
'banner-1544x500.jpg' => 'low',
));
if ( !empty($banners) ) {
$pluginInfo->banners = $banners;
}
}
/**
* @param array<string, string> $filesToKeys
* @return array<string, string>
*/
protected function getLocalAssetUrls($filesToKeys) {
$assetDirectory = $this->package->getAbsoluteDirectoryPath() . DIRECTORY_SEPARATOR . 'assets';
if ( !is_dir($assetDirectory) ) {
return array();
}
$assetBaseUrl = trailingslashit(plugins_url('', $assetDirectory . '/imaginary.file'));
$foundAssets = array();
foreach ($filesToKeys as $fileName => $key) {
$fullBannerPath = $assetDirectory . DIRECTORY_SEPARATOR . $fileName;
if ( !isset($icons[$key]) && is_file($fullBannerPath) ) {
$foundAssets[$key] = $assetBaseUrl . $fileName;
}
}
return $foundAssets;
}
public function setBranch($branch) {
$this->branch = $branch;
return $this;
}
public function setAuthentication($credentials) {
$this->api->setAuthentication($credentials);
return $this;
}
public function getVcsApi() {
return $this->api;
}
public function getUpdate() {
$update = parent::getUpdate();
if ( isset($update) && !empty($update->download_url) ) {
$update->download_url = $this->api->signDownloadUrl($update->download_url);
}
return $update;
}
public function onDisplayConfiguration($panel) {
parent::onDisplayConfiguration($panel);
$panel->row('Branch', $this->branch);
$panel->row('Authentication enabled', $this->api->isAuthenticationEnabled() ? 'Yes' : 'No');
$panel->row('API client', get_class($this->api));
}
}
endif;
| php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/admin/update/Puc/v4p11/Vcs/GitHubApi.php | admin/update/Puc/v4p11/Vcs/GitHubApi.php | <?php
if ( !class_exists('Puc_v4p11_Vcs_GitHubApi', false) ):
class Puc_v4p11_Vcs_GitHubApi extends Puc_v4p11_Vcs_Api {
/**
* @var string GitHub username.
*/
protected $userName;
/**
* @var string GitHub repository name.
*/
protected $repositoryName;
/**
* @var string Either a fully qualified repository URL, or just "user/repo-name".
*/
protected $repositoryUrl;
/**
* @var string GitHub authentication token. Optional.
*/
protected $accessToken;
/**
* @var bool Whether to download release assets instead of the auto-generated source code archives.
*/
protected $releaseAssetsEnabled = false;
/**
* @var string|null Regular expression that's used to filter release assets by name. Optional.
*/
protected $assetFilterRegex = null;
/**
* @var string|null The unchanging part of a release asset URL. Used to identify download attempts.
*/
protected $assetApiBaseUrl = null;
/**
* @var bool
*/
private $downloadFilterAdded = false;
public function __construct($repositoryUrl, $accessToken = null) {
$path = parse_url($repositoryUrl, PHP_URL_PATH);
if ( preg_match('@^/?(?P<username>[^/]+?)/(?P<repository>[^/#?&]+?)/?$@', $path, $matches) ) {
$this->userName = $matches['username'];
$this->repositoryName = $matches['repository'];
} else {
throw new InvalidArgumentException('Invalid GitHub repository URL: "' . $repositoryUrl . '"');
}
parent::__construct($repositoryUrl, $accessToken);
}
/**
* Get the latest release from GitHub.
*
* @return Puc_v4p11_Vcs_Reference|null
*/
public function getLatestRelease() {
$release = $this->api('/repos/:user/:repo/releases/latest');
if ( is_wp_error($release) || !is_object($release) || !isset($release->tag_name) ) {
return null;
}
$reference = new Puc_v4p11_Vcs_Reference(array(
'name' => $release->tag_name,
'version' => ltrim($release->tag_name, 'v'), //Remove the "v" prefix from "v1.2.3".
'downloadUrl' => $release->zipball_url,
'updated' => $release->created_at,
'apiResponse' => $release,
));
if ( isset($release->assets[0]) ) {
$reference->downloadCount = $release->assets[0]->download_count;
}
if ( $this->releaseAssetsEnabled && isset($release->assets, $release->assets[0]) ) {
//Use the first release asset that matches the specified regular expression.
$matchingAssets = array_filter($release->assets, array($this, 'matchesAssetFilter'));
if ( !empty($matchingAssets) ) {
if ( $this->isAuthenticationEnabled() ) {
/**
* Keep in mind that we'll need to add an "Accept" header to download this asset.
*
* @see setUpdateDownloadHeaders()
*/
$reference->downloadUrl = $matchingAssets[0]->url;
} else {
//It seems that browser_download_url only works for public repositories.
//Using an access_token doesn't help. Maybe OAuth would work?
$reference->downloadUrl = $matchingAssets[0]->browser_download_url;
}
$reference->downloadCount = $matchingAssets[0]->download_count;
}
}
if ( !empty($release->body) ) {
/** @noinspection PhpUndefinedClassInspection */
$reference->changelog = Parsedown::instance()->text($release->body);
}
return $reference;
}
/**
* Get the tag that looks like the highest version number.
*
* @return Puc_v4p11_Vcs_Reference|null
*/
public function getLatestTag() {
$tags = $this->api('/repos/:user/:repo/tags');
if ( is_wp_error($tags) || !is_array($tags) ) {
return null;
}
$versionTags = $this->sortTagsByVersion($tags);
if ( empty($versionTags) ) {
return null;
}
$tag = $versionTags[0];
return new Puc_v4p11_Vcs_Reference(array(
'name' => $tag->name,
'version' => ltrim($tag->name, 'v'),
'downloadUrl' => $tag->zipball_url,
'apiResponse' => $tag,
));
}
/**
* Get a branch by name.
*
* @param string $branchName
* @return null|Puc_v4p11_Vcs_Reference
*/
public function getBranch($branchName) {
$branch = $this->api('/repos/:user/:repo/branches/' . $branchName);
if ( is_wp_error($branch) || empty($branch) ) {
return null;
}
$reference = new Puc_v4p11_Vcs_Reference(array(
'name' => $branch->name,
'downloadUrl' => $this->buildArchiveDownloadUrl($branch->name),
'apiResponse' => $branch,
));
if ( isset($branch->commit, $branch->commit->commit, $branch->commit->commit->author->date) ) {
$reference->updated = $branch->commit->commit->author->date;
}
return $reference;
}
/**
* Get the latest commit that changed the specified file.
*
* @param string $filename
* @param string $ref Reference name (e.g. branch or tag).
* @return StdClass|null
*/
public function getLatestCommit($filename, $ref = 'master') {
$commits = $this->api(
'/repos/:user/:repo/commits',
array(
'path' => $filename,
'sha' => $ref,
)
);
if ( !is_wp_error($commits) && isset($commits[0]) ) {
return $commits[0];
}
return null;
}
/**
* Get the timestamp of the latest commit that changed the specified branch or tag.
*
* @param string $ref Reference name (e.g. branch or tag).
* @return string|null
*/
public function getLatestCommitTime($ref) {
$commits = $this->api('/repos/:user/:repo/commits', array('sha' => $ref));
if ( !is_wp_error($commits) && isset($commits[0]) ) {
return $commits[0]->commit->author->date;
}
return null;
}
/**
* Perform a GitHub API request.
*
* @param string $url
* @param array $queryParams
* @return mixed|WP_Error
*/
protected function api($url, $queryParams = array()) {
$baseUrl = $url;
$url = $this->buildApiUrl($url, $queryParams);
$options = array('timeout' => 10);
if ( $this->isAuthenticationEnabled() ) {
$options['headers'] = array('Authorization' => $this->getAuthorizationHeader());
}
if ( !empty($this->httpFilterName) ) {
$options = apply_filters($this->httpFilterName, $options);
}
$response = wp_remote_get($url, $options);
if ( is_wp_error($response) ) {
do_action('puc_api_error', $response, null, $url, $this->slug);
return $response;
}
$code = wp_remote_retrieve_response_code($response);
$body = wp_remote_retrieve_body($response);
if ( $code === 200 ) {
$document = json_decode($body);
return $document;
}
$error = new WP_Error(
'puc-github-http-error',
sprintf('GitHub API error. Base URL: "%s", HTTP status code: %d.', $baseUrl, $code)
);
do_action('puc_api_error', $error, $response, $url, $this->slug);
return $error;
}
/**
* Build a fully qualified URL for an API request.
*
* @param string $url
* @param array $queryParams
* @return string
*/
protected function buildApiUrl($url, $queryParams) {
$variables = array(
'user' => $this->userName,
'repo' => $this->repositoryName,
);
foreach ($variables as $name => $value) {
$url = str_replace('/:' . $name, '/' . urlencode($value), $url);
}
$url = 'https://api.github.com' . $url;
if ( !empty($queryParams) ) {
$url = add_query_arg($queryParams, $url);
}
return $url;
}
/**
* Get the contents of a file from a specific branch or tag.
*
* @param string $path File name.
* @param string $ref
* @return null|string Either the contents of the file, or null if the file doesn't exist or there's an error.
*/
public function getRemoteFile($path, $ref = 'master') {
$apiUrl = '/repos/:user/:repo/contents/' . $path;
$response = $this->api($apiUrl, array('ref' => $ref));
if ( is_wp_error($response) || !isset($response->content) || ($response->encoding !== 'base64') ) {
return null;
}
return base64_decode($response->content);
}
/**
* Generate a URL to download a ZIP archive of the specified branch/tag/etc.
*
* @param string $ref
* @return string
*/
public function buildArchiveDownloadUrl($ref = 'master') {
$url = sprintf(
'https://api.github.com/repos/%1$s/%2$s/zipball/%3$s',
urlencode($this->userName),
urlencode($this->repositoryName),
urlencode($ref)
);
return $url;
}
/**
* Get a specific tag.
*
* @param string $tagName
* @return void
*/
public function getTag($tagName) {
//The current GitHub update checker doesn't use getTag, so I didn't bother to implement it.
throw new LogicException('The ' . __METHOD__ . ' method is not implemented and should not be used.');
}
public function setAuthentication($credentials) {
parent::setAuthentication($credentials);
$this->accessToken = is_string($credentials) ? $credentials : null;
//Optimization: Instead of filtering all HTTP requests, let's do it only when
//WordPress is about to download an update.
add_filter('upgrader_pre_download', array($this, 'addHttpRequestFilter'), 10, 1); //WP 3.7+
}
/**
* Figure out which reference (i.e tag or branch) contains the latest version.
*
* @param string $configBranch Start looking in this branch.
* @return null|Puc_v4p11_Vcs_Reference
*/
public function chooseReference($configBranch) {
$updateSource = null;
if ( $configBranch === 'master' ) {
//Use the latest release.
$updateSource = $this->getLatestRelease();
if ( $updateSource === null ) {
//Failing that, use the tag with the highest version number.
$updateSource = $this->getLatestTag();
}
}
//Alternatively, just use the branch itself.
if ( empty($updateSource) ) {
$updateSource = $this->getBranch($configBranch);
}
return $updateSource;
}
/**
* Enable updating via release assets.
*
* If the latest release contains no usable assets, the update checker
* will fall back to using the automatically generated ZIP archive.
*
* Private repositories will only work with WordPress 3.7 or later.
*
* @param string|null $fileNameRegex Optional. Use only those assets where the file name matches this regex.
*/
public function enableReleaseAssets($fileNameRegex = null) {
$this->releaseAssetsEnabled = true;
$this->assetFilterRegex = $fileNameRegex;
$this->assetApiBaseUrl = sprintf(
'//api.github.com/repos/%1$s/%2$s/releases/assets/',
$this->userName,
$this->repositoryName
);
}
/**
* Does this asset match the file name regex?
*
* @param stdClass $releaseAsset
* @return bool
*/
protected function matchesAssetFilter($releaseAsset) {
if ( $this->assetFilterRegex === null ) {
//The default is to accept all assets.
return true;
}
return isset($releaseAsset->name) && preg_match($this->assetFilterRegex, $releaseAsset->name);
}
/**
* @internal
* @param bool $result
* @return bool
*/
public function addHttpRequestFilter($result) {
if ( !$this->downloadFilterAdded && $this->isAuthenticationEnabled() ) {
add_filter('http_request_args', array($this, 'setUpdateDownloadHeaders'), 10, 2);
add_action('requests-requests.before_redirect', array($this, 'removeAuthHeaderFromRedirects'), 10, 4);
$this->downloadFilterAdded = true;
}
return $result;
}
/**
* Set the HTTP headers that are necessary to download updates from private repositories.
*
* See GitHub docs:
* @link https://developer.github.com/v3/repos/releases/#get-a-single-release-asset
* @link https://developer.github.com/v3/auth/#basic-authentication
*
* @internal
* @param array $requestArgs
* @param string $url
* @return array
*/
public function setUpdateDownloadHeaders($requestArgs, $url = '') {
//Is WordPress trying to download one of our release assets?
if ( $this->releaseAssetsEnabled && (strpos($url, $this->assetApiBaseUrl) !== false) ) {
$requestArgs['headers']['Accept'] = 'application/octet-stream';
}
//Use Basic authentication, but only if the download is from our repository.
$repoApiBaseUrl = $this->buildApiUrl('/repos/:user/:repo/', array());
if ( $this->isAuthenticationEnabled() && (strpos($url, $repoApiBaseUrl)) === 0 ) {
$requestArgs['headers']['Authorization'] = $this->getAuthorizationHeader();
}
return $requestArgs;
}
/**
* When following a redirect, the Requests library will automatically forward
* the authorization header to other hosts. We don't want that because it breaks
* AWS downloads and can leak authorization information.
*
* @internal
* @param string $location
* @param array $headers
*/
public function removeAuthHeaderFromRedirects(&$location, &$headers) {
$repoApiBaseUrl = $this->buildApiUrl('/repos/:user/:repo/', array());
if ( strpos($location, $repoApiBaseUrl) === 0 ) {
return; //This request is going to GitHub, so it's fine.
}
//Remove the header.
if ( isset($headers['Authorization']) ) {
unset($headers['Authorization']);
}
}
/**
* Generate the value of the "Authorization" header.
*
* @return string
*/
protected function getAuthorizationHeader() {
return 'Basic ' . base64_encode($this->userName . ':' . $this->accessToken);
}
}
endif;
| php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/admin/update/Puc/v4p11/Vcs/BaseChecker.php | admin/update/Puc/v4p11/Vcs/BaseChecker.php | <?php
if ( !interface_exists('Puc_v4p11_Vcs_BaseChecker', false) ):
interface Puc_v4p11_Vcs_BaseChecker {
/**
* Set the repository branch to use for updates. Defaults to 'master'.
*
* @param string $branch
* @return $this
*/
public function setBranch($branch);
/**
* Set authentication credentials.
*
* @param array|string $credentials
* @return $this
*/
public function setAuthentication($credentials);
/**
* @return Puc_v4p11_Vcs_Api
*/
public function getVcsApi();
}
endif;
| php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/admin/update/Puc/v4p11/Vcs/Reference.php | admin/update/Puc/v4p11/Vcs/Reference.php | <?php
if ( !class_exists('Puc_v4p11_Vcs_Reference', false) ):
/**
* This class represents a VCS branch or tag. It's intended as a read only, short-lived container
* that only exists to provide a limited degree of type checking.
*
* @property string $name
* @property string|null version
* @property string $downloadUrl
* @property string $updated
*
* @property string|null $changelog
* @property int|null $downloadCount
*/
class Puc_v4p11_Vcs_Reference {
private $properties = array();
public function __construct($properties = array()) {
$this->properties = $properties;
}
/**
* @param string $name
* @return mixed|null
*/
public function __get($name) {
return array_key_exists($name, $this->properties) ? $this->properties[$name] : null;
}
/**
* @param string $name
* @param mixed $value
*/
public function __set($name, $value) {
$this->properties[$name] = $value;
}
/**
* @param string $name
* @return bool
*/
public function __isset($name) {
return isset($this->properties[$name]);
}
}
endif;
| php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/admin/update/Puc/v4p11/Vcs/BitBucketApi.php | admin/update/Puc/v4p11/Vcs/BitBucketApi.php | <?php
if ( !class_exists('Puc_v4p11_Vcs_BitBucketApi', false) ):
class Puc_v4p11_Vcs_BitBucketApi extends Puc_v4p11_Vcs_Api {
/**
* @var Puc_v4p11_OAuthSignature
*/
private $oauth = null;
/**
* @var string
*/
private $username;
/**
* @var string
*/
private $repository;
public function __construct($repositoryUrl, $credentials = array()) {
$path = parse_url($repositoryUrl, PHP_URL_PATH);
if ( preg_match('@^/?(?P<username>[^/]+?)/(?P<repository>[^/#?&]+?)/?$@', $path, $matches) ) {
$this->username = $matches['username'];
$this->repository = $matches['repository'];
} else {
throw new InvalidArgumentException('Invalid BitBucket repository URL: "' . $repositoryUrl . '"');
}
parent::__construct($repositoryUrl, $credentials);
}
/**
* Figure out which reference (i.e tag or branch) contains the latest version.
*
* @param string $configBranch Start looking in this branch.
* @return null|Puc_v4p11_Vcs_Reference
*/
public function chooseReference($configBranch) {
$updateSource = null;
//Check if there's a "Stable tag: 1.2.3" header that points to a valid tag.
$updateSource = $this->getStableTag($configBranch);
//Look for version-like tags.
if ( !$updateSource && ($configBranch === 'master' || $configBranch === 'main') ) {
$updateSource = $this->getLatestTag();
}
//If all else fails, use the specified branch itself.
if ( !$updateSource ) {
$updateSource = $this->getBranch($configBranch);
}
return $updateSource;
}
public function getBranch($branchName) {
$branch = $this->api('/refs/branches/' . $branchName);
if ( is_wp_error($branch) || empty($branch) ) {
return null;
}
//The "/src/{stuff}/{path}" endpoint doesn't seem to handle branch names that contain slashes.
//If we don't encode the slash, we get a 404. If we encode it as "%2F", we get a 401.
//To avoid issues, if the branch name is not URL-safe, let's use the commit hash instead.
$ref = $branch->name;
if ((urlencode($ref) !== $ref) && isset($branch->target->hash)) {
$ref = $branch->target->hash;
}
return new Puc_v4p11_Vcs_Reference(array(
'name' => $ref,
'updated' => $branch->target->date,
'downloadUrl' => $this->getDownloadUrl($branch->name),
));
}
/**
* Get a specific tag.
*
* @param string $tagName
* @return Puc_v4p11_Vcs_Reference|null
*/
public function getTag($tagName) {
$tag = $this->api('/refs/tags/' . $tagName);
if ( is_wp_error($tag) || empty($tag) ) {
return null;
}
return new Puc_v4p11_Vcs_Reference(array(
'name' => $tag->name,
'version' => ltrim($tag->name, 'v'),
'updated' => $tag->target->date,
'downloadUrl' => $this->getDownloadUrl($tag->name),
));
}
/**
* Get the tag that looks like the highest version number.
*
* @return Puc_v4p11_Vcs_Reference|null
*/
public function getLatestTag() {
$tags = $this->api('/refs/tags?sort=-target.date');
if ( !isset($tags, $tags->values) || !is_array($tags->values) ) {
return null;
}
//Filter and sort the list of tags.
$versionTags = $this->sortTagsByVersion($tags->values);
//Return the first result.
if ( !empty($versionTags) ) {
$tag = $versionTags[0];
return new Puc_v4p11_Vcs_Reference(array(
'name' => $tag->name,
'version' => ltrim($tag->name, 'v'),
'updated' => $tag->target->date,
'downloadUrl' => $this->getDownloadUrl($tag->name),
));
}
return null;
}
/**
* Get the tag/ref specified by the "Stable tag" header in the readme.txt of a given branch.
*
* @param string $branch
* @return null|Puc_v4p11_Vcs_Reference
*/
protected function getStableTag($branch) {
$remoteReadme = $this->getRemoteReadme($branch);
if ( !empty($remoteReadme['stable_tag']) ) {
$tag = $remoteReadme['stable_tag'];
//You can explicitly opt out of using tags by setting "Stable tag" to
//"trunk" or the name of the current branch.
if ( ($tag === $branch) || ($tag === 'trunk') ) {
return $this->getBranch($branch);
}
return $this->getTag($tag);
}
return null;
}
/**
* @param string $ref
* @return string
*/
protected function getDownloadUrl($ref) {
return sprintf(
'https://bitbucket.org/%s/%s/get/%s.zip',
$this->username,
$this->repository,
$ref
);
}
/**
* Get the contents of a file from a specific branch or tag.
*
* @param string $path File name.
* @param string $ref
* @return null|string Either the contents of the file, or null if the file doesn't exist or there's an error.
*/
public function getRemoteFile($path, $ref = 'master') {
$response = $this->api('src/' . $ref . '/' . ltrim($path));
if ( is_wp_error($response) || !is_string($response) ) {
return null;
}
return $response;
}
/**
* Get the timestamp of the latest commit that changed the specified branch or tag.
*
* @param string $ref Reference name (e.g. branch or tag).
* @return string|null
*/
public function getLatestCommitTime($ref) {
$response = $this->api('commits/' . $ref);
if ( isset($response->values, $response->values[0], $response->values[0]->date) ) {
return $response->values[0]->date;
}
return null;
}
/**
* Perform a BitBucket API 2.0 request.
*
* @param string $url
* @param string $version
* @return mixed|WP_Error
*/
public function api($url, $version = '2.0') {
$url = ltrim($url, '/');
$isSrcResource = Puc_v4p11_Utils::startsWith($url, 'src/');
$url = implode('/', array(
'https://api.bitbucket.org',
$version,
'repositories',
$this->username,
$this->repository,
$url
));
$baseUrl = $url;
if ( $this->oauth ) {
$url = $this->oauth->sign($url,'GET');
}
$options = array('timeout' => 10);
if ( !empty($this->httpFilterName) ) {
$options = apply_filters($this->httpFilterName, $options);
}
$response = wp_remote_get($url, $options);
if ( is_wp_error($response) ) {
do_action('puc_api_error', $response, null, $url, $this->slug);
return $response;
}
$code = wp_remote_retrieve_response_code($response);
$body = wp_remote_retrieve_body($response);
if ( $code === 200 ) {
if ( $isSrcResource ) {
//Most responses are JSON-encoded, but src resources just
//return raw file contents.
$document = $body;
} else {
$document = json_decode($body);
}
return $document;
}
$error = new WP_Error(
'puc-bitbucket-http-error',
sprintf('BitBucket API error. Base URL: "%s", HTTP status code: %d.', $baseUrl, $code)
);
do_action('puc_api_error', $error, $response, $url, $this->slug);
return $error;
}
/**
* @param array $credentials
*/
public function setAuthentication($credentials) {
parent::setAuthentication($credentials);
if ( !empty($credentials) && !empty($credentials['consumer_key']) ) {
$this->oauth = new Puc_v4p11_OAuthSignature(
$credentials['consumer_key'],
$credentials['consumer_secret']
);
} else {
$this->oauth = null;
}
}
public function signDownloadUrl($url) {
//Add authentication data to download URLs. Since OAuth signatures incorporate
//timestamps, we have to do this immediately before inserting the update. Otherwise
//authentication could fail due to a stale timestamp.
if ( $this->oauth ) {
$url = $this->oauth->sign($url);
}
return $url;
}
}
endif;
| php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/admin/update/Puc/v4p11/Vcs/ThemeUpdateChecker.php | admin/update/Puc/v4p11/Vcs/ThemeUpdateChecker.php | <?php
if ( !class_exists('Puc_v4p11_Vcs_ThemeUpdateChecker', false) ):
class Puc_v4p11_Vcs_ThemeUpdateChecker extends Puc_v4p11_Theme_UpdateChecker implements Puc_v4p11_Vcs_BaseChecker {
/**
* @var string The branch where to look for updates. Defaults to "master".
*/
protected $branch = 'master';
/**
* @var Puc_v4p11_Vcs_Api Repository API client.
*/
protected $api = null;
/**
* Puc_v4p11_Vcs_ThemeUpdateChecker constructor.
*
* @param Puc_v4p11_Vcs_Api $api
* @param null $stylesheet
* @param null $customSlug
* @param int $checkPeriod
* @param string $optionName
*/
public function __construct($api, $stylesheet = null, $customSlug = null, $checkPeriod = 12, $optionName = '') {
$this->api = $api;
$this->api->setHttpFilterName($this->getUniqueName('request_update_options'));
parent::__construct($api->getRepositoryUrl(), $stylesheet, $customSlug, $checkPeriod, $optionName);
$this->api->setSlug($this->slug);
}
public function requestUpdate() {
$api = $this->api;
$api->setLocalDirectory($this->package->getAbsoluteDirectoryPath());
$update = new Puc_v4p11_Theme_Update();
$update->slug = $this->slug;
//Figure out which reference (tag or branch) we'll use to get the latest version of the theme.
$updateSource = $api->chooseReference($this->branch);
if ( $updateSource ) {
$ref = $updateSource->name;
$update->download_url = $updateSource->downloadUrl;
} else {
do_action(
'puc_api_error',
new WP_Error(
'puc-no-update-source',
'Could not retrieve version information from the repository. '
. 'This usually means that the update checker either can\'t connect '
. 'to the repository or it\'s configured incorrectly.'
),
null, null, $this->slug
);
$ref = $this->branch;
}
//Get headers from the main stylesheet in this branch/tag. Its "Version" header and other metadata
//are what the WordPress install will actually see after upgrading, so they take precedence over releases/tags.
$remoteHeader = $this->package->getFileHeader($api->getRemoteFile('style.css', $ref));
$update->version = Puc_v4p11_Utils::findNotEmpty(array(
$remoteHeader['Version'],
Puc_v4p11_Utils::get($updateSource, 'version'),
));
//The details URL defaults to the Theme URI header or the repository URL.
$update->details_url = Puc_v4p11_Utils::findNotEmpty(array(
$remoteHeader['ThemeURI'],
$this->package->getHeaderValue('ThemeURI'),
$this->metadataUrl,
));
if ( empty($update->version) ) {
//It looks like we didn't find a valid update after all.
$update = null;
}
$update = $this->filterUpdateResult($update);
return $update;
}
//FIXME: This is duplicated code. Both theme and plugin subclasses that use VCS share these methods.
public function setBranch($branch) {
$this->branch = $branch;
return $this;
}
public function setAuthentication($credentials) {
$this->api->setAuthentication($credentials);
return $this;
}
public function getVcsApi() {
return $this->api;
}
public function getUpdate() {
$update = parent::getUpdate();
if ( isset($update) && !empty($update->download_url) ) {
$update->download_url = $this->api->signDownloadUrl($update->download_url);
}
return $update;
}
public function onDisplayConfiguration($panel) {
parent::onDisplayConfiguration($panel);
$panel->row('Branch', $this->branch);
$panel->row('Authentication enabled', $this->api->isAuthenticationEnabled() ? 'Yes' : 'No');
$panel->row('API client', get_class($this->api));
}
}
endif;
| php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/admin/update/Puc/v4p11/Vcs/GitLabApi.php | admin/update/Puc/v4p11/Vcs/GitLabApi.php | <?php
if ( !class_exists('Puc_v4p11_Vcs_GitLabApi', false) ):
class Puc_v4p11_Vcs_GitLabApi extends Puc_v4p11_Vcs_Api {
/**
* @var string GitLab username.
*/
protected $userName;
/**
* @var string GitLab server host.
*/
protected $repositoryHost;
/**
* @var string Protocol used by this GitLab server: "http" or "https".
*/
protected $repositoryProtocol = 'https';
/**
* @var string GitLab repository name.
*/
protected $repositoryName;
/**
* @var string GitLab authentication token. Optional.
*/
protected $accessToken;
/**
* @var bool Whether to download release assets instead of the auto-generated source code archives.
*/
protected $releaseAssetsEnabled = false;
/**
* @var bool Whether to download release asset package rather than release asset source.
*/
protected $releasePackageEnabled = false;
public function __construct($repositoryUrl, $accessToken = null, $subgroup = null) {
//Parse the repository host to support custom hosts.
$port = parse_url($repositoryUrl, PHP_URL_PORT);
if ( !empty($port) ) {
$port = ':' . $port;
}
$this->repositoryHost = parse_url($repositoryUrl, PHP_URL_HOST) . $port;
if ( $this->repositoryHost !== 'gitlab.com' ) {
$this->repositoryProtocol = parse_url($repositoryUrl, PHP_URL_SCHEME);
}
//Find the repository information
$path = parse_url($repositoryUrl, PHP_URL_PATH);
if ( preg_match('@^/?(?P<username>[^/]+?)/(?P<repository>[^/#?&]+?)/?$@', $path, $matches) ) {
$this->userName = $matches['username'];
$this->repositoryName = $matches['repository'];
} elseif ( ($this->repositoryHost === 'gitlab.com') ) {
//This is probably a repository in a subgroup, e.g. "/organization/category/repo".
$parts = explode('/', trim($path, '/'));
if ( count($parts) < 3 ) {
throw new InvalidArgumentException('Invalid GitLab.com repository URL: "' . $repositoryUrl . '"');
}
$lastPart = array_pop($parts);
$this->userName = implode('/', $parts);
$this->repositoryName = $lastPart;
} else {
//There could be subgroups in the URL: gitlab.domain.com/group/subgroup/subgroup2/repository
if ( $subgroup !== null ) {
$path = str_replace(trailingslashit($subgroup), '', $path);
}
//This is not a traditional url, it could be gitlab is in a deeper subdirectory.
//Get the path segments.
$segments = explode('/', untrailingslashit(ltrim($path, '/')));
//We need at least /user-name/repository-name/
if ( count($segments) < 2 ) {
throw new InvalidArgumentException('Invalid GitLab repository URL: "' . $repositoryUrl . '"');
}
//Get the username and repository name.
$usernameRepo = array_splice($segments, -2, 2);
$this->userName = $usernameRepo[0];
$this->repositoryName = $usernameRepo[1];
//Append the remaining segments to the host if there are segments left.
if ( count($segments) > 0 ) {
$this->repositoryHost = trailingslashit($this->repositoryHost) . implode('/', $segments);
}
//Add subgroups to username.
if ( $subgroup !== null ) {
$this->userName = $usernameRepo[0] . '/' . untrailingslashit($subgroup);
}
}
parent::__construct($repositoryUrl, $accessToken);
}
/**
* Get the latest release from GitLab.
*
* @return Puc_v4p11_Vcs_Reference|null
*/
public function getLatestRelease() {
$releases = $this->api('/:id/releases');
if ( is_wp_error($releases) || empty($releases) || !is_array($releases) ) {
return null;
}
foreach ($releases as $release) {
if ( true !== $release->upcoming_release ) {
break 1; //Break the loop on the first release we find that isn't an upcoming release
}
}
if ( is_wp_error($release) || !is_object($release) || !isset($release->tag_name) ) {
return null;
}
$reference = new Puc_v4p11_Vcs_Reference(array(
'name' => $release->tag_name,
'version' => ltrim($release->tag_name, 'v'), //Remove the "v" prefix from "v1.2.3".
'downloadUrl' => '',
'updated' => $release->released_at,
'apiResponse' => $release,
));
$download_url = false;
if ( $this->releasePackageEnabled && isset($release->assets, $release->assets->links) ) {
/**
* Use the first asset LINK that is a zip format file generated by a Gitlab Release Pipeline
*
* @link https://gist.github.com/timwiel/9dfd3526c768efad4973254085e065ce
*/
foreach ($release->assets->links as $link) {
//TODO: Check the "format" property instead of the URL suffix.
if ( 'zip' === substr($link->url, -3) ) {
$download_url = $link->url;
break 1;
}
}
if ( empty( $download_url ) ) {
return null;
}
if ( ! empty( $this->accessToken ) ) {
$download_url = add_query_arg('private_token', $this->accessToken, $download_url);
}
$reference->downloadUrl = $download_url;
return $reference;
} elseif ( isset($release->assets) ) {
/**
* Use the first asset SOURCE file that is a zip format from a Gitlab Release which should be a zip file
*/
foreach ($release->assets->sources as $source) {
if ( 'zip' === $source->format ) {
$download_url = $source->url;
break 1;
}
}
if ( empty( $download_url ) ) {
return null;
}
if ( ! empty( $this->accessToken ) ) {
$download_url = add_query_arg('private_token', $this->accessToken, $download_url);
}
$reference->downloadUrl = $download_url;
return $reference;
}
//If we get this far without a return then obviosuly noi release download urls were found
return null;
}
/**
* Get the tag that looks like the highest version number.
*
* @return Puc_v4p11_Vcs_Reference|null
*/
public function getLatestTag() {
$tags = $this->api('/:id/repository/tags');
if ( is_wp_error($tags) || empty($tags) || !is_array($tags) ) {
return null;
}
$versionTags = $this->sortTagsByVersion($tags);
if ( empty($versionTags) ) {
return null;
}
$tag = $versionTags[0];
return new Puc_v4p11_Vcs_Reference(array(
'name' => $tag->name,
'version' => ltrim($tag->name, 'v'),
'downloadUrl' => $this->buildArchiveDownloadUrl($tag->name),
'apiResponse' => $tag,
));
}
/**
* Get a branch by name.
*
* @param string $branchName
* @return null|Puc_v4p11_Vcs_Reference
*/
public function getBranch($branchName) {
$branch = $this->api('/:id/repository/branches/' . $branchName);
if ( is_wp_error($branch) || empty($branch) ) {
return null;
}
$reference = new Puc_v4p11_Vcs_Reference(array(
'name' => $branch->name,
'downloadUrl' => $this->buildArchiveDownloadUrl($branch->name),
'apiResponse' => $branch,
));
if ( isset($branch->commit, $branch->commit->committed_date) ) {
$reference->updated = $branch->commit->committed_date;
}
return $reference;
}
/**
* Get the timestamp of the latest commit that changed the specified branch or tag.
*
* @param string $ref Reference name (e.g. branch or tag).
* @return string|null
*/
public function getLatestCommitTime($ref) {
$commits = $this->api('/:id/repository/commits/', array('ref_name' => $ref));
if ( is_wp_error($commits) || !is_array($commits) || !isset($commits[0]) ) {
return null;
}
return $commits[0]->committed_date;
}
/**
* Perform a GitLab API request.
*
* @param string $url
* @param array $queryParams
* @return mixed|WP_Error
*/
protected function api($url, $queryParams = array()) {
$baseUrl = $url;
$url = $this->buildApiUrl($url, $queryParams);
$options = array('timeout' => 10);
if ( !empty($this->httpFilterName) ) {
$options = apply_filters($this->httpFilterName, $options);
}
$response = wp_remote_get($url, $options);
if ( is_wp_error($response) ) {
do_action('puc_api_error', $response, null, $url, $this->slug);
return $response;
}
$code = wp_remote_retrieve_response_code($response);
$body = wp_remote_retrieve_body($response);
if ( $code === 200 ) {
return json_decode($body);
}
$error = new WP_Error(
'puc-gitlab-http-error',
sprintf('GitLab API error. URL: "%s", HTTP status code: %d.', $baseUrl, $code)
);
do_action('puc_api_error', $error, $response, $url, $this->slug);
return $error;
}
/**
* Build a fully qualified URL for an API request.
*
* @param string $url
* @param array $queryParams
* @return string
*/
protected function buildApiUrl($url, $queryParams) {
$variables = array(
'user' => $this->userName,
'repo' => $this->repositoryName,
'id' => $this->userName . '/' . $this->repositoryName,
);
foreach ($variables as $name => $value) {
$url = str_replace("/:{$name}", '/' . urlencode($value), $url);
}
$url = substr($url, 1);
$url = sprintf('%1$s://%2$s/api/v4/projects/%3$s', $this->repositoryProtocol, $this->repositoryHost, $url);
if ( !empty($this->accessToken) ) {
$queryParams['private_token'] = $this->accessToken;
}
if ( !empty($queryParams) ) {
$url = add_query_arg($queryParams, $url);
}
return $url;
}
/**
* Get the contents of a file from a specific branch or tag.
*
* @param string $path File name.
* @param string $ref
* @return null|string Either the contents of the file, or null if the file doesn't exist or there's an error.
*/
public function getRemoteFile($path, $ref = 'master') {
$response = $this->api('/:id/repository/files/' . $path, array('ref' => $ref));
if ( is_wp_error($response) || !isset($response->content) || $response->encoding !== 'base64' ) {
return null;
}
return base64_decode($response->content);
}
/**
* Generate a URL to download a ZIP archive of the specified branch/tag/etc.
*
* @param string $ref
* @return string
*/
public function buildArchiveDownloadUrl($ref = 'master') {
$url = sprintf(
'%1$s://%2$s/api/v4/projects/%3$s/repository/archive.zip',
$this->repositoryProtocol,
$this->repositoryHost,
urlencode($this->userName . '/' . $this->repositoryName)
);
$url = add_query_arg('sha', urlencode($ref), $url);
if ( !empty($this->accessToken) ) {
$url = add_query_arg('private_token', $this->accessToken, $url);
}
return $url;
}
/**
* Get a specific tag.
*
* @param string $tagName
* @return void
*/
public function getTag($tagName) {
throw new LogicException('The ' . __METHOD__ . ' method is not implemented and should not be used.');
}
/**
* Figure out which reference (i.e tag or branch) contains the latest version.
*
* @param string $configBranch Start looking in this branch.
* @return null|Puc_v4p11_Vcs_Reference
*/
public function chooseReference($configBranch) {
if ( $configBranch === 'main' || $configBranch === 'master' ) {
//Use the latest release.
$updateSource = $this->getLatestRelease();
if ( $updateSource === null ) {
//Failing that, use the tag with the highest version number.
$updateSource = $this->getLatestTag();
}
}
//Alternatively, just use the branch itself.
if ( empty($updateSource) ) {
$updateSource = $this->getBranch($configBranch);
}
return $updateSource;
}
public function setAuthentication($credentials) {
parent::setAuthentication($credentials);
$this->accessToken = is_string($credentials) ? $credentials : null;
}
public function enableReleaseAssets() {
$this->releaseAssetsEnabled = true;
$this->releasePackageEnabled = false;
}
public function enableReleasePackages() {
$this->releaseAssetsEnabled = false;
$this->releasePackageEnabled = true;
}
}
endif;
| php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/inc/home.php | inc/home.php | <link rel="stylesheet" href="<?php echo i_static(); ?>/swiper/swiper-bundle.min.css">
<?php if(get_option("i_swiper")) {
$swiper_query = new WP_Query(array(
'post__in' => explode(',',get_option("i_swiper")),
'post__not_in' => get_option('sticky_posts'),
'orderby' => 'post__in',
'showposts' => 10,
)) ?>
<?php if(get_option("i_recommend")) { ?>
<div class="container wrapper-home wrapper-recommend">
<div class="swiper">
<div class="swiper-wrapper">
<?php $i=0; ?>
<?php if($swiper_query->have_posts()) : while($swiper_query->have_posts()) : $swiper_query->the_post(); ?>
<div class="swiper-slide">
<a href="<?php the_permalink() ?>">
<?php if (has_post_thumbnail()) { ?>
<?php the_post_thumbnail('large'); ?>
<?php } else {?>
<img src="<?php if(get_option("i_loading_pic")) { echo get_option("i_loading_pic");} else{ echo i_loading_pic(); } ?>"
data-original="<?php echo catch_that_image(); ?>?top=<?php $i++; echo $i; ?>" />
<?php } ?>
</a>
<a href="<?php the_permalink(); ?>" class="swiper-img-mask">
<div class="title" <?php if(get_option("i_swiper_effect") != 'fade'){echo 'data-swiper-parallax="200"';} ?> data-swiper-parallax-duration="600"><?php the_title(); ?></div>
</a>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
<div class="swiper-pagination"></div>
</div>
<?php
$recommend_query = new WP_Query(array(
'post__in' => explode(',',get_option("i_recommend")),
'post__not_in' => get_option('sticky_posts'),
'orderby' => 'post__in',
'showposts' => 2,
))
?>
<div class="recommend">
<?php if($recommend_query->have_posts()) : while($recommend_query->have_posts()) : $recommend_query->the_post(); ?>
<div class="box">
<a href="<?php the_permalink() ?>">
<?php if (has_post_thumbnail()) { ?>
<?php the_post_thumbnail(); ?>
<?php } else {?>
<img src="<?php if(get_option("i_loading_pic")) { echo get_option("i_loading_pic");} else{ echo i_loading_pic(); } ?>"
data-original="<?php echo catch_that_image(); ?>?top=<?php $i++; echo $i; ?>" />
<?php } ?>
<div class="title"><p><?php the_title(); ?></p></div>
</a>
<div class="recommend-text">推荐</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
<?php } else { ?>
<div class="container wrapper-home">
<div class="swiper">
<div class="swiper-wrapper">
<?php $i=0; ?>
<?php if($swiper_query->have_posts()) : while($swiper_query->have_posts()) : $swiper_query->the_post(); ?>
<div class="swiper-slide">
<a href="<?php the_permalink() ?>">
<?php if (has_post_thumbnail()) { ?>
<?php the_post_thumbnail('large'); ?>
<?php } else {?>
<img src="<?php if(get_option("i_loading_pic")) { echo get_option("i_loading_pic");} else{ echo i_loading_pic(); } ?>"
data-original="<?php echo catch_that_image(); ?>?top=<?php $i++; echo $i; ?>" />
<?php } ?>
</a>
<a href="<?php the_permalink(); ?>" class="swiper-img-mask">
<div class="title" <?php if(get_option("i_swiper_effect") != 'fade'){echo 'data-swiper-parallax="200"';} ?> data-swiper-parallax-duration="600"><?php the_title(); ?></div>
</a>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
<div class="swiper-pagination"></div>
</div>
</div>
<?php } ?>
<?php } else {?>
<div class="wrapper">
<div class="banner"></div>
<div class="content-wrapper">
<div class="text-wrapper">
<?php if(get_option("i_wrapper_text") || get_option("i_wrapper_name")) { ?>
<?php if(get_option("i_wrapper_text")){echo "<h2>".get_option("i_wrapper_text")."</h2>";}; ?>
<?php if(get_option("i_wrapper_name")){echo "<i>".get_option("i_wrapper_name")."</i>";}; ?>
<?php } else { ?>
<h2><?php echo '<p id="hitokoto"><span id="hitokoto_text">一言加载中...</span></p>'; ?></h2>
<i><?php echo '<p id="hitokoto_author"></p>'; ?></i>
<script src="<?php echo i_static(); ?>/js/yiyan.js"></script>
<?php } ?>
</div>
</div>
</div>
<?php } ?>
<div class="container main-content main">
<div class="content">
<?php
if(get_option("i_notice")) {?>
<div class="notice">
<div class="notice-box">
<span></span>
<p><?php echo get_option("i_notice"); ?></p>
</div>
</div>
<?php }
?>
<div class="title-part">
<p id="title-part">最新发布</p>
<?php get_search_form(); ?>
</div>
<?php
if(get_option("i_blog_to_column") == 1) {
get_template_part('inc/home-blog');
} else{
if(get_option("i_blog_auto_column") == 1) {
get_template_part('inc/home-card');
get_template_part('inc/home-blog');
?>
<style>
.home-2{
display: none;
}
@media screen and (max-width: 640px) {
.home-2 {
display: block;
}
.main-part {
display: none;
}
}
</style>
<?php
} else {
get_template_part('inc/home-card');
}
}
?>
</div>
</div>
<script src="<?php echo i_static(); ?>/swiper/swiper-bundle.min.js"></script>
<?php
if(get_option("i_swiper_effect")) {
$swiper_effect = get_option("i_swiper_effect");
} else {
$swiper_effect = 'slide';
}
?>
<script type="text/javascript">
// 轮播图
var mySwiper = new Swiper ('.swiper', {
loop: true,
parallax : true,
effect: '<?php echo $swiper_effect; ?>',
spaceBetween: 10,
speed: 600,
autoplay: {
delay: 3000,
disableOnInteraction: false,
pauseOnMouseEnter: true,
},
pagination: {
el: '.swiper-pagination',
clickable : true,
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
})
</script> | php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/inc/page-author.php | inc/page-author.php | <div class="wrapper">
<div class="banner"></div>
<div class="content-wrapper">
<div class="text-wrapper">
<h2 class="h2-title"><?php the_author_nickname(); ?></h2>
<?php
if(get_the_author_description()) {
echo '<i>'.get_the_author_description().'</i>';
} else {
echo '<i>这家伙很懒,什么都没写</i>';
}
?>
</div>
</div>
</div>
<div class="container main-content main">
<div class="content">
<?php
if(get_option("i_notice")) {?>
<div class="notice">
<div class="notice-box">
<span></span>
<p><?php echo get_option("i_notice"); ?></p>
</div>
</div>
<?php }
?>
<div class="title-part">
<p class="name"><span><?php the_author_nickname(); ?></span>发布的内容</p>
<?php get_search_form(); ?>
</div>
<?php
if(get_option("i_blog_to_column") == 1) {
get_template_part('inc/home-blog');
} else{
if(get_option("i_blog_auto_column") == 1) {
get_template_part('inc/home-card');
get_template_part('inc/home-blog');
?>
<style>
.home-2{
display: none;
}
@media screen and (max-width: 640px) {
.home-2 {
display: block;
}
.main-part {
display: none;
}
}
</style>
<?php
} else {
get_template_part('inc/home-card');
}
}
?>
</div>
</div>
| php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/inc/frame.php | inc/frame.php | <!DOCTYPE html>
<html lang="<?php echo get_bloginfo('language'); ?>">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<meta name="renderer" content="webkit" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<?php if(is_home()) { ?>
<meta name="keywords" content="<?=get_option("i_keywords") ? get_option("i_keywords") : 'iFalse主题'?>" />
<meta name="description" content="<?php bloginfo('description'); ?>" />
<?php } else { ?>
<meta name="keywords" content="<?=the_title()?>" />
<?php }
?>
<title><?php if(function_exists('show_wp_title')){show_wp_title();} ?></title>
<link rel="shortcut icon" href="<?php site_icon_url(); ?>" type="image/x-icon" />
<link rel="stylesheet" href="<?php echo i_static(); ?>/css/theme.min.css">
<link rel="stylesheet" href="<?php echo i_static(); ?>/iconfont/iconfont.css">
<?php if(get_option("i_custom_html_head")){echo get_option("i_custom_html_head");}; ?>
<style><?php if(get_option("i_custom_css_head")){echo get_option("i_custom_css_head");}; ?></style>
<?php if(get_option("i_plane") == 1) { ?><link rel="stylesheet" href="<?php echo i_static(); ?>/css/style-plane.min.css">
<?php } ?>
<?php if(get_option("i_color")) { ?>
<style>
:root{
--theme:<?php echo get_option("i_color"); ?>;
--theme-sub:<?php
if(get_option("i_color")){
if(get_option("i_color_sub")){
echo get_option("i_color_sub");
}else if(get_option("i_color") && !get_option("i_color_sub")) {
echo get_option("i_color");
}else {
echo "#58b3f5";
}
}else if(!get_option("i_color") && get_option("i_color_sub")) {
echo "#58b3f5";
} else {
echo "#58b3f5";
}
?>;
--theme-1:<?php echo get_option("i_color").'1a'; ?>;
--theme-2:<?php echo get_option("i_color").'33'; ?>;
--theme-3:<?php echo get_option("i_color").'4d'; ?>;
--theme-5:<?php echo get_option("i_color").'80'; ?>;
--notice: <?php echo get_option("i_color").'26'; ?>;
}
</style>
<? } ?>
<?php if(get_option("i_night") == 2) {
if(date("m") >= 3 && date("m") <= 8) { ?>
<script>var judge = new Date().getHours() >= 20 || new Date().getHours() <= 5;</script>
<?php } else if(date("m") >= 9 && date("m") <= 12 || date("m") <= 2) { ?>
<script>var judge = new Date().getHours() >= 19 || new Date().getHours() <= 6;</script>
<?php } ?>
<?php } else if(get_option("i_night") == 1) { ?><script>var judge = true;</script>
<?php } else { ?><script>var judge = false;</script>
<?php } ?>
<script src="<?php echo i_static(); ?>/js/jquery.min.js"></script>
<script src="<?php echo i_static(); ?>/js/changeNight.js"></script>
</head>
| php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/inc/random-img.php | inc/random-img.php | <?php
$path = '../static/images/login/'; //存有图片的文件夹imgs
$handle = opendir($path);
while (false !== ($file = readdir($handle))) {
list($filesname,$typeOfImg)=explode(".",$file);
if(
$typeOfImg=="jpg" or
$typeOfImg=="jpeg" or
$typeOfImg=="png" or
$typeOfImg=="gif" or
$typeOfImg=="JPG" or
$typeOfImg=="JPEG" or
$typeOfImg=="PNG" or
$typeOfImg=="GIF"
) {
if (!is_dir('./'.$file)) {
$array[]=$file;
}
}
}
$str = array_rand($array);
header('Location:'.$path.$array[$str]);
?> | php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/inc/article.php | inc/article.php | <link rel="stylesheet" href="<?php echo i_static(); ?>/fancybox/fancybox.css">
<link rel="stylesheet" href="<?php echo i_static(); ?>/highlight/styles/vs2015.min.css">
<div class="container single-top article-top">
<div class="single-banner">
<div class="single-cate">
<div class="single-cate-box">
<span class="iconfont icon-zhinanzhen"></span>
<b><?php echo the_category(' ') ?></b>
</div>
</div>
<div class="single-title"><h1><?php the_title(); ?></h1></div>
<div class="single-detail">
<div class="author">
<a href="<?php the_post();home_url();echo '/author/';echo get_the_author_meta('user_login');rewind_posts(); ?>"><?php the_post();echo get_avatar( get_the_author_ID() );rewind_posts(); ?></a>
<span><?php the_author_posts_link(); ?></span>
</div>
<div class="other">
<span class="date"><?php echo get_the_date(); ?> <?php the_time(); ?></span>
<span class="views"><?php setPostViews(get_the_ID()) ?><?php echo getPostViews(get_the_ID()) ?></span>
<?php if(get_option("i_comments_article") == 1){ ?><span class="comments"><?php if(post_password_required()){echo '已加密';}elseif(comments_open()){comments_popup_link('沙发','1','%');}else{echo '已关闭';} ?></span><?php } ?>
<?php edit_post_link('编辑文章') ?>
</div>
</div>
<div class="breadcrumb"><?php if ( function_exists('i_breadcrumb') ) i_breadcrumb();?></div>
</div>
</div>
<div class="container single-main article-main">
<div class="left">
<div class="post-content">
<?php the_content(); ?>
<?php if(get_the_tag_list()){ ?><div class="the-tag"><?php echo get_the_tag_list('<span>',' ','</span>'); ?></div><?php } ?>
<?php if(get_option("i_plane") != 1) { ?>
<div class="the-end"><i></i><span>THE END</span><i></i></div>
<?php } ?>
<?php if(get_option("i_post_copyright") == 1) { ?>
<div class="post-copyright">
<div class="post-copyright-title">© 版权声明</div>
<div class="post-copyright-text"><?php if(get_option("i_post_copyright_text")){echo get_option("i_post_copyright_text");}else{echo '分享是一种美德,转载请保留原链接';} ?></div>
</div>
<?php } ?>
</div>
<?php if(get_option("i_next_post") == 1) { ?>
<div class="post-context">
<div class="post-context_text">上下篇章</div>
<div class="post-prev-next">
<div class="post-prev"><span>← 上一章节</span><p><?php if(get_previous_post()){previous_post_link('%link');}else{echo "这已经是第一章内容了";}; ?></p></div><i></i>
<div class="post-next"><span>下一章节 →</span><p><?php if(get_next_post()){next_post_link('%link');}else{echo "这已经是最后章内容了";}; ?></p></div>
</div>
</div>
<?php } ?>
<?php
if(get_option("i_comments_article") == 1) { ?>
<?php comments_template('/comments.php'); ?>
<?php
}
?>
</div>
<?php if(get_option("i_post_sidebar") == 1) { ?>
<style>
.single-main .left {
width: 100%;
padding-right: 0;
border-right: none;
}
</style>
<?php } else { ?>
<div class="right">
<?php get_template_part('sidebar-article')?>
</div>
<?php } ?>
</div>
<div class="post-menu-btn">
<div>
<span class="iconfont icon-nav-list"></span>
</div>
</div>
<script src="<?php echo i_static(); ?>/fancybox/fancybox.umd.js"></script>
<script src="<?php echo i_static(); ?>/highlight/highlight.min.js"></script> | php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/inc/page-tag.php | inc/page-tag.php | <div class="wrapper">
<div class="banner"></div>
<div class="content-wrapper">
<div class="text-wrapper">
<h2 class="h2-title"><?php single_cat_title(); ?></h2>
</div>
</div>
</div>
<div class="container main-content main">
<div class="content">
<?php
if(get_option("i_notice")) {?>
<div class="notice">
<div class="notice-box">
<span></span>
<p><?php echo get_option("i_notice"); ?></p>
</div>
</div>
<?php }
?>
<div class="title-part">
<p class="tag"><span><?php single_cat_title(); ?></span> 标签下的内容</p>
<?php get_search_form(); ?>
</div>
<?php
if(get_option("i_blog_to_column") == 1) {
get_template_part('inc/home-blog');
} else{
if(get_option("i_blog_auto_column") == 1) {
get_template_part('inc/home-card');
get_template_part('inc/home-blog');
?>
<style>
.home-2{
display: none;
}
@media screen and (max-width: 640px) {
.home-2 {
display: block;
}
.main-part {
display: none;
}
}
</style>
<?php
} else {
get_template_part('inc/home-card');
}
}
?>
</div>
</div>
| php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/inc/home-blog.php | inc/home-blog.php | <div class="home-2">
<div class="home-2-mian">
<ul>
<?php $i=0; ?>
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?>
<li>
<div class="home-2-pic">
<?php if (is_sticky()) {echo '<span class="post-top">置顶</span>';} ?>
<a href="<?php the_permalink(); ?>">
<div class="mask-pic"><span class="iconfont icon-chakan"></span></div>
<?php if (has_post_thumbnail()) { ?>
<?php the_post_thumbnail('large'); ?>
<?php } else {?>
<img src="<?php if(get_option("i_loading_pic")) {echo get_option("i_loading_pic");} else{echo i_loading_pic(); } ?>"
data-original="<?php echo catch_that_image(); ?>?<?php $i++; echo $i; ?>" />
<?php } ?>
</a>
</div>
<div class="home-2-detail">
<div class="home-2-detail-top-title"><h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2></div>
<div class="home-2-detail-top-abstract"><?php the_excerpt(); ?></div>
<div class="home-2-detail-bottom">
<div class="home-2-detail-bottom-cate"></span><?php echo the_category(' ') ?></div>
<div class="home-2-detail-bottom-msg">
<a href="<?php home_url();echo '/author/';echo get_the_author_meta('user_login'); ?>"><?php echo get_avatar( get_the_author_ID() );?></a>
<div class="home-2-detail-time"><?php echo get_the_date(); ?></div>
<div class="home-2-detail-views"><?php echo getPostViews(get_the_ID()) ?></div>
<?php if(get_option("i_comments_article") == 1){ ?><div class="home-2-detail-comments"><?php if(post_password_required()){echo '已加密';}elseif(comments_open()){comments_popup_link('沙发','1','%');}else{echo '已关闭';} ?></div><?php } ?>
</div>
</div>
</div>
</li>
<?php endwhile; ?>
<?php endif; ?>
</ul>
<?php if(get_next_posts_link()) { ?>
<div class="page-nav-bar">
<div class="page-nav">
<?php wp_pagenavi(); ?>
</div>
</div>
<?php } else { ?>
<p class="haveNoMore"><span class="iconfont icon-tishi1"> 没有更多内容</span></p>
<?php if(get_previous_posts_link()) { ?>
<div class="page-nav-bar">
<div class="page-nav">
<?php wp_pagenavi(); ?>
</div>
</div>
<?php } ?>
<?php } ?>
</div>
<div class="home-2-sidebar">
<?php get_sidebar(); ?>
</div>
</div>
| php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/inc/page-template.php | inc/page-template.php | <link rel="stylesheet" href="<?php echo i_static(); ?>/fancybox/fancybox.css">
<link rel="stylesheet" href="<?php echo i_static(); ?>/highlight/styles/vs2015.min.css">
<div class="container single-top page-top">
<div class="page-banner">
<div class="page-title"><h1><?php the_title(); ?></h1></div>
<div class="page-detail">
<div class="author">
<a href="<?php the_post();home_url();echo '/author/';echo get_the_author_meta('user_login');rewind_posts(); ?>"><?php the_post();echo get_avatar( get_the_author_ID() );rewind_posts(); ?></a>
<span><?php the_author_posts_link(); ?></span>
</div>
<div class="other">
<span class="date"><?php echo get_the_date(); ?> <?php the_time(); ?></span>
<span class="views"><?php setPostViews(get_the_ID()) ?><?php echo getPostViews(get_the_ID()) ?></span>
<?php if(get_option("i_comments_page") == 1){ ?><span class="comments"><?php if(post_password_required()){echo '已加密';}elseif(comments_open()){comments_popup_link('沙发','1','%');}else{echo '已关闭';} ?></span><?php } ?>
<?php edit_post_link('编辑页面') ?>
</div>
</div>
</div>
</div>
<div class="container single-main page-main">
<div class="left">
<div class="post-content">
<?php the_content(); ?>
</div>
<?php
if(get_option("i_comments_page") == 1) { ?>
<?php comments_template('/comments.php');?>
<?php
}
?>
</div>
</div>
<script src="<?php echo i_static(); ?>/fancybox/fancybox.umd.js"></script>
<script src="<?php echo i_static(); ?>/highlight/highlight.min.js"></script> | php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/inc/home-card.php | inc/home-card.php | <div class="main-part">
<ul>
<?php $i=0; ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<li>
<div class="home-pic">
<?php if (is_sticky()) {echo '<span class="post-top">置顶</span>';} ?>
<a href="<?php the_permalink(); ?>">
<div class="mask-pic"><span class="iconfont icon-chakan"></span></div>
<?php if (has_post_thumbnail()) { ?>
<?php the_post_thumbnail('large'); ?>
<?php } else {?>
<img src="<?php if(get_option("i_loading_pic")) {echo get_option("i_loading_pic");} else{echo i_loading_pic(); } ?>"
data-original="<?php echo catch_that_image(); ?>?<?php $i++; echo $i; ?>" />
<?php } ?>
</a>
</div>
<div class="home-detail">
<div class="home-cate"><?php echo the_category(' ') ?></div>
<div class="home-title"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></div>
<div class="home-msg">
<a href="<?php home_url();echo '/author/';echo get_the_author_meta('user_login'); ?>"><?php echo get_avatar( get_the_author_ID() );?></a>
<div class="home-date"><?php echo get_the_date(); ?></div>
<div class="home-watch"><?php echo getPostViews(get_the_ID()) ?></div>
<?php if(get_option("i_comments_article") == 1){ ?><div class="home-comments"><?php if(post_password_required()){echo '已加密';}elseif(comments_open()){comments_popup_link('沙发','1','%');}else{echo '已关闭';} ?></div><?php } ?>
</div>
</div>
</li>
<?php endwhile; ?>
<?php endif; ?>
</ul>
<?php if(get_next_posts_link()) { ?>
<div class="page-nav-bar">
<div class="page-nav">
<?php wp_pagenavi(); ?>
</div>
</div>
<?php } else { ?>
<p class="haveNoMore"><span class="iconfont icon-tishi1"> 没有更多内容</span></p>
<?php if(get_previous_posts_link()) { ?>
<div class="page-nav-bar">
<div class="page-nav">
<?php wp_pagenavi(); ?>
</div>
</div>
<?php } ?>
<?php } ?>
</div> | php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/inc/frame-js.php | inc/frame-js.php | <script src="<?php echo i_static(); ?>/js/jquery.lazyload.js"></script>
<script src="<?php echo i_static(); ?>/iconfont/iconfont.js"></script>
<script src="<?php echo i_static(); ?>/js/theme.js"></script>
<script src="<?php echo i_static(); ?>/js/SmoothScroll.js"></script>
<script><?php if(get_option("i_custom_js_footer")){echo get_option("i_custom_js_footer");}; ?></script>
<?php if(get_option("i_custom_html_tongji")){echo get_option("i_custom_html_tongji");}; ?>
<script type="text/javascript">
// 延迟加载
jQuery(function() {
jQuery("img").lazyload({
effect : "fadeIn",
failure_limit : 50,
threshold : 200,
});
});
console.log('%c iFalse %c https://github.com/kannafay/iFalse', 'background: linear-gradient(120deg, #8183ff, #58b3f5);color:#fff;border-radius:2px;', '');
console.log('<?php echo get_num_queries(); ?> queries in <?php timer_stop(7); ?>s');
</script> | php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/inc/page-search.php | inc/page-search.php | <div class="wrapper">
<div class="banner"></div>
<div class="content-wrapper">
<div class="text-wrapper">
<h2 class="h2-title"><?php the_search_query(); ?></h2>
</div>
</div>
</div>
<div class="container main-content main">
<div class="content">
<?php
if(get_option("i_notice")) {?>
<div class="notice">
<div class="notice-box">
<span></span>
<p><?php echo get_option("i_notice"); ?></p>
</div>
</div>
<?php }
?>
<div class="title-part">
<p class="search">搜索关键词:<span><?php the_search_query(); ?></span></p>
<?php get_search_form(); ?>
</div>
<?php
if(get_option("i_blog_to_column") == 1) {
get_template_part('inc/home-blog');
} else{
if(get_option("i_blog_auto_column") == 1) {
get_template_part('inc/home-card');
get_template_part('inc/home-blog');
?>
<style>
.home-2{
display: none;
}
@media screen and (max-width: 640px) {
.home-2 {
display: block;
}
.main-part {
display: none;
}
}
</style>
<?php
} else {
get_template_part('inc/home-card');
}
}
?>
</div>
</div>
| php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/inc/page-archive.php | inc/page-archive.php | <div class="wrapper">
<div class="banner"></div>
<div class="content-wrapper">
<div class="text-wrapper">
<h2 class="h2-title"><?php single_cat_title(); ?></h2>
</div>
</div>
</div>
<div class="container main-content main">
<div class="content">
<?php
if(get_option("i_notice")) {?>
<div class="notice">
<div class="notice-box">
<span></span>
<p><?php echo get_option("i_notice"); ?></p>
</div>
</div>
<?php }
?>
<div class="title-part">
<p class="cate"><span><?php single_cat_title(); ?></span>分类下的内容</p>
<?php get_search_form(); ?>
</div>
<?php
if(get_option("i_blog_to_column") == 1) {
get_template_part('inc/home-blog');
} else{
if(get_option("i_blog_auto_column") == 1) {
get_template_part('inc/home-card');
get_template_part('inc/home-blog');
?>
<style>
.home-2{
display: none;
}
@media screen and (max-width: 640px) {
.home-2 {
display: block;
}
.main-part {
display: none;
}
}
</style>
<?php
} else {
get_template_part('inc/home-card');
}
}
?>
</div>
</div>
| php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/inc/article-shuoshuo.php | inc/article-shuoshuo.php | <link rel="stylesheet" href="<?php echo i_static(); ?>/fancybox/fancybox.css">
<link rel="stylesheet" href="<?php echo i_static(); ?>/highlight/styles/vs2015.min.css">
<div class="container single-top article-top say-top">
<div class="single-banner">
<div class="single-title"><h1><?php the_title(); ?></h1></div>
<div class="single-detail">
<div class="author">
<a href="<?php the_post();home_url();echo '/author/';echo get_the_author_meta('user_login');rewind_posts(); ?>"><?php the_post();echo get_avatar( get_the_author_ID() );rewind_posts(); ?></a>
<span><?php the_author_posts_link(); ?></span>
</div>
<div class="other">
<span class="date"><?php echo get_the_date(); ?> <?php the_time(); ?></span>
<span class="views"><?php setPostViews(get_the_ID()) ?><?php echo getPostViews(get_the_ID()) ?></span>
<?php if(get_option("i_comments_article") == 1){ ?><span class="comments"><?php if(post_password_required()){echo '已加密';}elseif(comments_open()){comments_popup_link('沙发','1','%');}else{echo '已关闭';} ?></span><?php } ?>
<?php edit_post_link('编辑说说') ?>
</div>
</div>
</div>
</div>
<div class="container single-main article-main say-main">
<div class="left">
<div class="post-content">
<?php the_content(); ?>
<?php if(get_option("i_plane") != 1) { ?>
<div class="the-end"><i></i><span>THE END</span><i></i></div>
<?php } ?>
</div>
<?php
if(get_option("i_comments_article") == 1) { ?>
<?php comments_template('/comments.php');?>
<?php
}
?>
</div>
<?php if(get_option("i_post_sidebar") == 1) { ?>
<style>
.single-main .left {
width: 100%;
padding-right: 0;
border-right: none;
}
</style>
<?php } else { ?>
<div class="right">
<?php get_template_part('sidebar-article')?>
</div>
<?php } ?>
</div>
<div class="post-menu-btn">
<div>
<span class="iconfont icon-nav-list"></span>
</div>
</div>
<script src="<?php echo i_static(); ?>/fancybox/fancybox.umd.js"></script>
<script src="<?php echo i_static(); ?>/highlight/highlight.min.js"></script> | php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/inc/page-date.php | inc/page-date.php | <div class="wrapper">
<div class="banner"></div>
<div class="content-wrapper">
<div class="text-wrapper">
<h2 class="h2-title"><?php the_time('Y年m月'); ?></h2>
</div>
</div>
</div>
<div class="container main-content main">
<div class="content">
<?php
if(get_option("i_notice")) {?>
<div class="notice">
<div class="notice-box">
<span></span>
<p><?php echo get_option("i_notice"); ?></p>
</div>
</div>
<?php }
?>
<div class="title-part">
<p class="date"><span><?php the_time('Y年m月'); ?></span>发布的内容</p>
<?php get_search_form(); ?>
</div>
<?php
if(get_option("i_blog_to_column") == 1) {
get_template_part('inc/home-blog');
} else{
if(get_option("i_blog_auto_column") == 1) {
get_template_part('inc/home-card');
get_template_part('inc/home-blog');
?>
<style>
.home-2{
display: none;
}
@media screen and (max-width: 640px) {
.home-2 {
display: block;
}
.main-part {
display: none;
}
}
</style>
<?php
} else {
get_template_part('inc/home-card');
}
}
?>
</div>
</div>
| php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/template/forget.php | template/forget.php | <?php
/*Template Name: 找回密码*/
?>
<?php i_frame(); ?>
<style>
body {
overflow: hidden;
}
.progress-wrap,
.active-progress {
display: none !important;
}
</style>
<body>
<?php
if(get_option("i_forget_turn") == 1) {
if(is_user_logged_in()) {
if(current_user_can('level_10')) {
wp_redirect(home_url() . '/wp-admin', 302);
} else {
wp_redirect(home_url(), 302);
}
} else ?>
<?php get_header(); ?>
<div class="login-page">
<div class="login-main">
<img class="login-img" src="<?php echo get_template_directory_uri(); ?>/inc/random-img.php" alt="login">
<div class="login-msg">
<div class="forget-box">
<h2>找回密码!</h2>
<div class="des">Retrieve password!</div>
<p style="color:#D43030"></p>
<form name="lostpasswordform" method="POST" action="<?php home_url(); ?>/wp-login.php?action=lostpassword">
<div class="form-item form-email">
<span class="iconfont icon-email"></span>
<input type="email" name="user_login" placeholder="邮箱" size="20" required="required" autofocus />
</div>
<div class="form-other">
<?php if(get_option("i_register_turn") == 1) {echo '<span>还没有账户?<a href="' . home_url(). '/register">立即注册</a></span>';} else{} ?>
<span><a href="<?php home_url(); ?>/login">返回登录</a></span>
</div>
<div class="form-item">
<button type="submit" name="wp-submit">获取新密码</button>
</div>
</form>
</div>
</div>
</div>
</div>
<?php i_frame_js(); ?>
<?php
} else {
wp_redirect(home_url() . '/404', 302);
}
?>
</body>
</html> | php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/template/say.php | template/say.php | <?php
/*Template Name: 动态说说*/
?>
<?php i_frame(); ?>
<link rel="stylesheet" href="<?php echo i_static(); ?>/fancybox/fancybox.css">
<?php get_header(); ?>
<section>
<div class="say container-small">
<div class="say-banner">
<img src="<?php if(get_option("i_loading_pic")) {echo get_option("i_loading_pic");} else {echo i_loading_pic(); } ?>"
data-original="<?php if(get_option("i_say_img")) {echo get_option("i_say_img");} else {echo i_cover_pic(); } ?>" alt="">
</div>
<div class="say-author">
<div class="say-author-box">
<?php if(current_user_can('level_1')) { ?>
<div class="say-author-logo">
<?php echo get_user_avatar(); ?>
</div>
<div class="say-author-name">
<?php global $current_user, $display_name;
get_currentuserinfo();
echo $current_user -> display_name;
?>
</div>
<div class="say-author-des">
<?php
global $current_user, $user_description;
get_currentuserinfo();
echo $current_user -> user_description;
if($current_user -> user_description == null) {
echo '这家伙很懒,什么都没写';
}
?>
</div>
<?php } else { ?>
<div class="say-author-logo">
<?php echo get_avatar(1); ?>
</div>
<div class="say-author-name"><?php echo get_the_author_meta('nickname', 1); ?></div>
<div class="say-author-des">
<?php
if(get_the_author_meta('description',1)) {
echo get_the_author_meta('description',1);
} else {
echo '这家伙很懒,什么都没写';
}
?>
</div>
<?php } ?>
</div>
</div>
<div class="say-content">
<ul>
<?php
$limit = get_option('posts_per_page');$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts('post_type=shuoshuo&post_status=publish&paged=' . $paged);
if (have_posts()) : while (have_posts()) : the_post();
?>
<li class="say-post-item">
<div class="say-post-box">
<div class="say-author-logo">
<a href="<?php home_url();echo '/author/';echo get_the_author_meta('user_login'); ?>"><?php echo get_avatar( get_the_author_ID() );?></a>
</div>
<div class="say-post-msg">
<div class="say-author-name"><?php the_author_posts_link(); ?><?php if( wp_get_current_user()->ID == get_the_author_ID() ){echo '<span>自己</span>';} ?></div>
<div class="say-post-time"><?php echo get_the_date(); ?> <?php the_time(); ?></div>
</div>
</div>
<div class="say-post-content-box">
<div class="say-post-content">
<?php the_content(); ?>
</div>
<div class="say-post-comments">
<?php if(comments_open()){comments_popup_link('0评论','1评论','%评论');}else{echo '<a class="close">已关闭</a>';}; ?>
</div>
</div>
</li>
<?php
endwhile;
endif;
?>
</ul>
<?php if(get_next_posts_link()) { ?>
<div class="say-page-nav">
<div class="say-page-nav-bar">
<?php wp_pagenavi(); ?>
</div>
</div>
<?php } else { ?>
<p class="haveNoMore-say"><span class="iconfont icon-tishi1"> 没有更多内容</span></p>
<?php if(get_previous_posts_link()) { ?>
<div class="say-page-nav">
<div class="say-page-nav-bar">
<?php wp_pagenavi(); ?>
</div>
</div>
<?php } ?>
<?php } ?>
</div>
</div>
<?php get_footer(); ?>
</section>
<script src="<?php echo i_static(); ?>/fancybox/fancybox.umd.js"></script>
<?php i_frame_js(); ?>
| php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/template/login.php | template/login.php | <?php
/*Template Name: 用户登录*/
?>
<?php i_frame(); ?>
<style>
body {
overflow: hidden;
}
.progress-wrap,
.active-progress {
display: none !important;
}
</style>
<body>
<?php
if(is_user_logged_in()) {
if(current_user_can('level_10')) {
wp_redirect(home_url() . '/wp-admin', 302);
} else {
wp_redirect(home_url(), 302);
}
} else ?>
<?php get_header(); ?>
<div class="login-page">
<div class="login-main">
<img class="login-img" src="<?php echo get_template_directory_uri(); ?>/inc/random-img.php" alt="login">
<div class="login-msg">
<div class="login-box">
<h2>欢迎回来!</h2>
<div class="des">Welcome back!</div>
<p style="color:#D43030">
<?php
$login = (isset($_GET['login']) ) ? $_GET['login'] : 0;
if ( $login === "failed" ) {
echo '用户名或密码错误!';
}
?>
</p>
<form name="loginform" method="POST" action="<?php home_url(); ?>/wp-login.php">
<div class="form-item form-username">
<span class="iconfont icon-atm"></span>
<input type="text" name="log" placeholder="用户名/邮箱" size="20" required="required" />
</div>
<div class="form-item form-password">
<span class="iconfont icon-password"></span>
<input type="password" name="pwd" placeholder="密码" size="20" required="required" />
</div>
<div class="form-item form-remember" style="display:none">
<input name="rememberme" type="checkbox" value="forever" checked="checked" />
</div>
<div class="form-other">
<?php if(get_option("i_register_turn") == 1) {echo '<span>还没有账户?<a href="' . home_url(). '/register">立即注册</a></span>';} ?>
<?php if(get_option("i_forget_turn") == 1) {echo '<span><a href="' . home_url() . '/forget">找回密码</a></span>';} ?>
</div>
<div class="form-item">
<button type="submit" name="wp-submit">登录</button>
</div>
</form>
<!-- <div class="login-third">
<div class="hr-line"><hr><p>第三方登录</p><hr></div>
<div class="qqlogin"><a href="#"><img src="<?php //echo i_static(); ?>/images/qq.png" alt=""></a></div>
</div> -->
</div>
</div>
</div>
</div>
<?php i_frame_js(); ?>
</body>
</html> | php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/template/register.php | template/register.php | <?php
/*Template Name: 用户注册*/
?>
<?php i_frame(); ?>
<style>
body {
overflow: hidden;
}
.progress-wrap,
.active-progress {
display: none !important;
}
</style>
<body>
<?php
if(get_option("i_register_turn") == 1) {
if(is_user_logged_in()) {
if(current_user_can('level_10')) {
wp_redirect(home_url() . '/wp-admin', 302);
} else {
wp_redirect(home_url(), 302);
}
} else ?>
<div class="login-page">
<div class="login-main">
<img class="login-img" src="<?php echo get_template_directory_uri(); ?>/inc/random-img.php" alt="login">
<div class="login-msg">
<div class="login-box">
<!-- 注册 -->
<div class="register-box">
<h2>欢迎注册!</h2>
<div class="des">Welcome to register!</div>
<?php
if( !empty($_POST['ludou_reg']) ) {
$error = '';
$sanitized_user_login = sanitize_user( $_POST['user_login'] );
$user_email = apply_filters( 'user_registration_email', $_POST['user_email'] );
// 检查用户名
if ( ! validate_username( $sanitized_user_login ) ) {
$error .= '<p style="color:#D43030">用户名包含无效字符,请输入有效的用户名!</p>';
$sanitized_user_login = '';
} elseif ( username_exists( $sanitized_user_login ) ) {
$error .= '<p style="color:#D43030">该用户名已被注册!</p>';
} elseif(strlen($sanitized_user_login) < 5) {
$error .= '<p style="color:#D43030">用户名长度至少5位!</p>';
}
// 检查邮箱
if ( ! is_email( $user_email ) ) {
$error .= '<p style="color:#D43030">电子邮件地址不正确!</p>';
$user_email = '';
} elseif ( email_exists( $user_email ) ) {
$error .= '<p style="color:#D43030">该电子邮件地址已经被注册!</p>';
}
// 检查密码
if(strlen($_POST['user_pass']) < 6)
$error .= '<p style="color:#D43030">密码长度至少6位!</p>';
elseif($_POST['user_pass'] != $_POST['user_pass2'])
$error .= '<p style="color:#D43030">两次输入的密码必须一致!</p>';if($error == '') {
$user_id = wp_create_user( $sanitized_user_login, $_POST['user_pass'], $user_email );
if ( ! $user_id ) {
$error .= sprintf( '<p style="color:#D43030">无法完成您的注册请求...请联系<a href="mailto:%s">管理员</a>!</p>', get_option( 'admin_email' ) );
}
else if (!is_user_logged_in()) {
$user = get_userdatabylogin($sanitized_user_login);
$user_id = $user->ID;
// 自动登录
wp_set_current_user($user_id, $user_login);
wp_set_auth_cookie($user_id);
do_action('wp_login', $user_login);
}
}
}
?>
<?php
if(!empty($error)) {
echo '<p class="ludou-error">'.$error.'</p>';
} elseif (is_user_logged_in()) {
echo '<p style="color:#32CD32"><span class="iconfont icon-yduizhengqueshixin" style="color:#18aa18;"></span> 注册成功!正在跳转中...</p>';
header("refresh:2");
}
?>
<form name="registerform" method="POST" action="<?php echo $_SERVER["REQUEST_URI"]; ?>">
<div class="form-item form-username">
<span class="iconfont icon-atm"></span>
<input type="text" name="user_login" placeholder="用户名" size="20" required="required" value="<?php if(!empty($sanitized_user_login)) echo $sanitized_user_login; ?>" />
</div>
<div class="form-item form-email">
<span class="iconfont icon-email"></span>
<input type="email" name="user_email" placeholder="邮箱" size="20" required="required" value="<?php if(!empty($user_email)) echo $user_email; ?>" />
</div>
<div class="form-item form-password">
<span class="iconfont icon-password"></span>
<input type="password" name="user_pass" placeholder="密码" size="20" required="required" />
</div>
<div class="form-item form-password">
<span class="iconfont icon-password"></span>
<input type="password" name="user_pass2" placeholder="确认密码" size="20" required="required" />
</div>
<div class="form-other">
<span>已有账户?<a href="<?php home_url(); ?>/login">立即登录</a></span>
<span><a href="<?php home_url(); ?>/forget">找回密码</a></span>
</div>
<div class="form-item">
<input type="hidden" name="ludou_reg" value="ok">
<button type="submit" name="wp-submit">注册</button>
</div>
<div class="form-other">
<span style="margin-bottom:0;"><span class="iconfont icon-tishi1"></span>注册即代表同意<a href="<?php home_url(); ?>/privacy-policy">《用户协议》</a></span>
</div>
</form>
</div>
</div>
</div>
</div>
<?php get_header(); ?>
<?php i_frame_js(); ?>
<?php
} else {
wp_redirect(home_url() . '/404', 302);
}
?>
</body>
</html> | php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
kannafay/iFalse | https://github.com/kannafay/iFalse/blob/e434c2131c7faf29aa79a2553cca35e55d56dcca/template/links.php | template/links.php | <?php
/*Template Name: 友情链接*/
?>
<?php i_frame(); ?>
<?php get_header(); ?>
<section>
<div class="container single-main page-main page-link">
<div class="left">
<div class="post-content">
<?php the_content(); ?>
<?php wp_list_bookmarks('orderby=id&show_description=1&show_name=1'); ?>
</div>
<?php
if(get_option("i_comments_page") == 1) { ?>
<?php comments_template('/comments.php');?>
<?php
}
?>
<script>
const web_master_links = [];
for(let i=0; i<comment_vcard.length; i++) {
web_master_links[i] = comment_vcard[i].querySelector('i').innerText + comment_vcard[i].querySelector('.fn > i').innerText;
if(web_master_links[i] == "博主作者") {
comment_vcard[i].querySelector('.fn > i').style.display = 'none';
};
};
</script>
</div>
</div>
<?php get_footer(); ?>
</section>
<?php i_frame_js(); ?> | php | MIT | e434c2131c7faf29aa79a2553cca35e55d56dcca | 2026-01-05T05:14:50.586486Z | false |
dzuelke/HadooPHP | https://github.com/dzuelke/HadooPHP/blob/09ba720998748c578ac2cc47b7b53fb011c8579e/bin/compile.php | bin/compile.php | #!/usr/bin/env php
<?php
$opts = getopt('i:t:h', array('help', 'debug'));
$optlength = 0;
// count all flags, and count string values twice (then it wasn't just a flag, so it'll use up two argv entries
// must do recursively since args can be repeated
array_walk_recursive($opts, function($value) use(&$optlength) { if(is_scalar($value)) $optlength++; if(is_string($value)) $optlength++; });
if(isset($opts['h']) || isset($opts['help']) || ($_SERVER['argc']-$optlength) < 3) {
echo "Usage: " . basename(__FILE__) . " [OPTION]... JOBDIR OUTPUTDIR\n";
echo "\n";
echo "Options:\n";
echo " --debug : Build debug version of package (with internal counters etc).\n";
echo " -h/--help : Display this help screen.\n";
echo " -i PATH : PATH of directory to package with phar (can be repeated).\n";
echo " -t TIMEZONE : Name of the TIMEZONE to force in generated scripts.\n";
echo " If not given, the timezone of this machine is used.\n";
echo "\n";
exit(1);
}
if(!Phar::canWrite()) {
echo "Phar write mode not allowed; disable phar.readonly in php.ini\n\n";
exit(2);
}
$jobdir = realpath($_SERVER['argv'][$_SERVER['argc']-2]);
if($jobdir === false || !is_dir($jobdir) || !is_readable($jobdir)) {
echo sprintf("Input directory '%s' not found or not readable.\n\n", $_SERVER['argv'][$_SERVER['argc']-2]);
exit(1);
}
$jobname = basename($jobdir);
$builddir = realpath($_SERVER['argv'][$_SERVER['argc']-1]);
if($builddir === false || !is_dir($builddir) || !is_writable($builddir)) {
echo sprintf("Output directory '%s' not found or not writable.\n\n", $_SERVER['argv'][$_SERVER['argc']-1]);
exit(1);
}
$jobphar = "$builddir/$jobname.phar";
$jobsh = "$builddir/$jobname.sh";
if(isset($opts['t'])) {
$tz = $opts['t'];
} else {
$tz = date_default_timezone_get();
}
try {
new DateTimeZone($tz);
} catch(Exception $e) {
echo sprintf("Invalid timezone '%s'.\n\n", $tz);
exit(1);
}
$phar = new Phar($jobphar, RecursiveDirectoryIterator::CURRENT_AS_FILEINFO | RecursiveDirectoryIterator::KEY_AS_FILENAME, 'my.phar');
$phar->startBuffering();
$stub = $phar->createDefaultStub('Hadoophp/_run.php', false);
// inject timezone and add shebang (substr cuts off the leading "<?php" bit)
$stub = "#!/usr/bin/env php\n<?php\ndate_default_timezone_set('$tz');\ndefine('HADOOPHP_DEBUG', " . var_export(isset($opts['debug']), true) . ");\n" . substr($stub, 5);
$phar->setStub($stub);
// for envs without phar, this will work and not create a checksum error, but invocation needs to be "php archive.phar then":
// $phar->setStub($phar->createDefaultStub('Hadoophp/_run.php', 'Hadoophp/_run.php'));
$buildDirectories = array(
realpath(__DIR__ . '/../lib/'),
realpath($jobdir),
);
if(isset($opts['i'])) {
$buildDirectories = array_merge($buildDirectories, (array)$opts['i']);
}
$filterRegex = '#^((/|^)(?!\\.)[^/]*)+$#';
foreach($buildDirectories as $path) {
$phar->buildFromDirectory(realpath($path), $filterRegex);
}
$phar->stopBuffering();
if(file_exists("$jobdir/ARGUMENTS")) {
$mapRedArgs = trim(file_get_contents("$jobdir/ARGUMENTS"));
} else {
$mapRedArgs = array();
// must do reducer first because of the fallback... -D args must be listed first or hadoop-streaming.jar complains
if(file_exists("$jobdir/Reducer.php")) {
$mapRedArgs[] = "-reducer 'php -d detect_unicode=off $jobname.phar reducer'";
} else {
$mapRedArgs[] = "-D mapred.reduce.tasks=0";
}
if(file_exists("$jobdir/Combiner.php")) {
$mapRedArgs[] = "-combiner 'php -d detect_unicode=off $jobname.phar combiner'";
}
$mapRedArgs[] = "-mapper 'php -d detect_unicode=off $jobname.phar mapper'";
$mapRedArgs = implode(" \\\n", $mapRedArgs) . " \\";
}
file_put_contents($jobsh, sprintf('#!/bin/sh
confswitch=""
streaming=""
while getopts ":c:s:" opt; do
case $opt in
c) confswitch="--config $OPTARG";;
s) streaming="$OPTARG";;
\?) echo "Invalid option: -$OPTARG"; exit 1;;
:) echo "Option -$OPTARG requires an argument."; exit 1;;
esac
done
shift $((OPTIND-1))
if [ $# -lt 2 ]
then
echo "Usage: $0 [OPTION...] HDFSINPUTPATH... HDFSOUTPUTPATH"
echo ""
echo "HDFSINPUTPATH can be repeated to use multiple paths as input for the job."
echo ""
echo "Options:"
echo " -c HADOOPCONFDIR Gets passed to hadoop via "--config" (see hadoop help)."
echo " -s STREAMINGJAR Path to hadoop-streaming-*.jar"
echo ""
exit 1
fi
input=""
output=""
index=0
for path in $*
do
index=`expr $index + 1`
if [ $index -ne $# ]
then
input=$input" -input $path"
else
output="-output $path"
fi
done
if [ $HADOOP_HOME ]
then
hadoop=$HADOOP_HOME/bin/hadoop
if [ -z $streaming ]
then
streaming=$HADOOP_HOME"/contrib/streaming/hadoop-streaming-*.jar"
fi
else
hadoop="hadoop"
if [ -z $streaming ]
then
streaming="/usr/lib/hadoop/contrib/streaming/hadoop-streaming-*.jar"
fi
fi
dir=`dirname $0`
$hadoop $confswitch jar $streaming \\
%s
$input \\
$output \\
-file $dir/%s.phar
', $mapRedArgs, $jobname));
echo "
Build done, generated files:
$jobsh
$jobphar
If you re-built the job, make sure to check the modifications in $jobname.sh
Do not forget to chmod
$jobname.sh
and
$jobname.phar
to be executable before checking in.
";
?> | php | MIT | 09ba720998748c578ac2cc47b7b53fb011c8579e | 2026-01-05T05:14:56.295411Z | false |
dzuelke/HadooPHP | https://github.com/dzuelke/HadooPHP/blob/09ba720998748c578ac2cc47b7b53fb011c8579e/examples/HitsByUri2/Mapper.php | examples/HitsByUri2/Mapper.php | <?php
require_once('Hadoophp/MapReduce/Mapper.php');
require_once('Hadoophp/MapReduce/Util.php');
class Mapper extends \Hadoophp\MapReduce\Mapper
{
protected function map($key, $value)
{
if($log = \Hadoophp\MapReduce\Util::parseApacheLogLine($value)) {
$this->emit("LongValueSum:" . $log['request_uri'], 1);
}
}
}
?> | php | MIT | 09ba720998748c578ac2cc47b7b53fb011c8579e | 2026-01-05T05:14:56.295411Z | false |
dzuelke/HadooPHP | https://github.com/dzuelke/HadooPHP/blob/09ba720998748c578ac2cc47b7b53fb011c8579e/examples/TrafficByUriAndDay/Mapper.php | examples/TrafficByUriAndDay/Mapper.php | <?php
require_once('Hadoophp/MapReduce/Mapper.php');
require_once('Hadoophp/MapReduce/Util.php');
class Mapper extends \Hadoophp\MapReduce\Mapper
{
protected function map($key, $value)
{
if($log = \Hadoophp\MapReduce\Util::parseApacheLogLine($value)) {
$this->emit(
// composite key
sprintf("LongValueSum:%s\t%s", $log['request_uri'], $log['datetime']->format('Y-m-d')),
$log['length']
);
}
}
}
?> | php | MIT | 09ba720998748c578ac2cc47b7b53fb011c8579e | 2026-01-05T05:14:56.295411Z | false |
dzuelke/HadooPHP | https://github.com/dzuelke/HadooPHP/blob/09ba720998748c578ac2cc47b7b53fb011c8579e/examples/TrafficByUri/Mapper.php | examples/TrafficByUri/Mapper.php | <?php
require_once('Hadoophp/MapReduce/Mapper.php');
require_once('Hadoophp/MapReduce/Util.php');
class Mapper extends \Hadoophp\MapReduce\Mapper
{
protected function map($key, $value)
{
if($log = \Hadoophp\MapReduce\Util::parseApacheLogLine($value)) {
$this->emit('LongValueSum:' . $log['request_uri'], $log['length']);
}
}
}
?> | php | MIT | 09ba720998748c578ac2cc47b7b53fb011c8579e | 2026-01-05T05:14:56.295411Z | false |
dzuelke/HadooPHP | https://github.com/dzuelke/HadooPHP/blob/09ba720998748c578ac2cc47b7b53fb011c8579e/examples/HitsByHour/Mapper.php | examples/HitsByHour/Mapper.php | <?php
require_once('Hadoophp/MapReduce/Mapper.php');
require_once('Hadoophp/MapReduce/Util.php');
class Mapper extends \Hadoophp\MapReduce\Mapper
{
protected function map($key, $value)
{
if($log = \Hadoophp\MapReduce\Util::parseApacheLogLine($value)) {
$this->emit("LongValueSum:" . $log['datetime']->format('H'), 1);
// diagnostics
if(HADOOPHP_DEBUG) {
$this->emitCounter('com.github.dzuelke.hadoophp.examples.HitsByHour', 'MAP_PARSED_RECORDS');
}
} else {
// diagnostics
if(HADOOPHP_DEBUG) {
$this->emitCounter('com.github.dzuelke.hadoophp.examples.HitsByHour', 'MAP_FAILED_RECORDS');
}
}
}
}
?> | php | MIT | 09ba720998748c578ac2cc47b7b53fb011c8579e | 2026-01-05T05:14:56.295411Z | false |
dzuelke/HadooPHP | https://github.com/dzuelke/HadooPHP/blob/09ba720998748c578ac2cc47b7b53fb011c8579e/examples/HitsByUri/Mapper.php | examples/HitsByUri/Mapper.php | <?php
require_once('Hadoophp/MapReduce/Mapper.php');
require_once('Hadoophp/MapReduce/Util.php');
class Mapper extends \Hadoophp\MapReduce\Mapper
{
protected function map($key, $value)
{
if($log = \Hadoophp\MapReduce\Util::parseApacheLogLine($value)) {
$this->emit($log['request_uri'], 1);
// diagnostics
if(HADOOPHP_DEBUG) {
$this->emitCounter('com.github.dzuelke.hadoophp.examples.HitsByUri', 'MAP_PARSED_RECORDS');
}
} else {
// diagnostics
if(HADOOPHP_DEBUG) {
$this->emitCounter('com.github.dzuelke.hadoophp.examples.HitsByUri', 'MAP_FAILED_RECORDS');
}
}
}
}
?> | php | MIT | 09ba720998748c578ac2cc47b7b53fb011c8579e | 2026-01-05T05:14:56.295411Z | false |
dzuelke/HadooPHP | https://github.com/dzuelke/HadooPHP/blob/09ba720998748c578ac2cc47b7b53fb011c8579e/examples/HitsByUri/Reducer.php | examples/HitsByUri/Reducer.php | <?php
require_once('Hadoophp/MapReduce/Reducer.php');
class Reducer extends \Hadoophp\MapReduce\Reducer
{
public function reduce($key, Traversable $values)
{
$sum = 0;
foreach($values as $value) {
$sum += $value;
}
$this->emit($key, $sum);
}
}
?> | php | MIT | 09ba720998748c578ac2cc47b7b53fb011c8579e | 2026-01-05T05:14:56.295411Z | false |
dzuelke/HadooPHP | https://github.com/dzuelke/HadooPHP/blob/09ba720998748c578ac2cc47b7b53fb011c8579e/lib/Hadoophp/_run.php | lib/Hadoophp/_run.php | <?php
if($_SERVER['argc'] != 2 || !in_array($mode = strtolower($_SERVER['argv'][1]), array('mapper', 'combiner', 'reducer'))) {
echo "Usage: " . $_SERVER['argv'][0] . " MODE\n\nMODE can be 'mapper', 'combiner' or 'reducer'\n\n";
exit(1);
}
if(!defined('HADOOPHP_DEBUG')) {
define('HADOOPHP_DEBUG', false);
}
$job = basename($argv[0], '.phar');
$mode = ucfirst(strtolower($mode));
require($mode . '.php');
$worker = new $mode();
$worker->handle();
?> | php | MIT | 09ba720998748c578ac2cc47b7b53fb011c8579e | 2026-01-05T05:14:56.295411Z | false |
dzuelke/HadooPHP | https://github.com/dzuelke/HadooPHP/blob/09ba720998748c578ac2cc47b7b53fb011c8579e/lib/Hadoophp/MapReduce/Util.php | lib/Hadoophp/MapReduce/Util.php | <?php
namespace Hadoophp\MapReduce;
class Util {
public static function parseApacheLogLine($line) {
if(preg_match('/^(?P<ip>[0-9a-fA-F.:]+)\s(?P<identd>\S+)\s(?P<authuser>\S+)\s\[(?P<timestamp>[^\]]+)\]\s"(?P<request>(?P<request_method>[A-Z]+)\s(?P<request_uri>\S+)\s(?P<request_protocol>\S+))"\s(?P<status>\d+)\s(?P<length>(\d+|-))/', $line, $matches)) {
$matches['datetime'] = new \DateTime($matches['timestamp']);
if($matches['length'] == '-') {
$matches['length'] = '0';
}
return $matches;
}
}
public static function findMinMax(\Traversable $it, callback $extractor = null) {
if($extractor === null) {
$extractor = function($value) {
return $value;
};
}
$min = $max = $extractor($it->current());
$it->next();
$value = $extractor($it->current());
while($it->valid()) {
if($value > $max) {
$max = $value;
} elseif($value < $min) {
$min = $value;
}
$it->next();
$value = $it->current();
}
return array($min, $max);
}
}
?> | php | MIT | 09ba720998748c578ac2cc47b7b53fb011c8579e | 2026-01-05T05:14:56.295411Z | false |
dzuelke/HadooPHP | https://github.com/dzuelke/HadooPHP/blob/09ba720998748c578ac2cc47b7b53fb011c8579e/lib/Hadoophp/MapReduce/Base.php | lib/Hadoophp/MapReduce/Base.php | <?php
namespace Hadoophp\MapReduce;
require_once('Hadoophp/MapReduce/Key.php');
/**
* Base class all mappers and reducers can extend from.
*/
abstract class Base
{
/**
* The input stream handle.
*/
private $handle;
protected $inputFieldSeparator = "\t";
protected $outputFieldSeparator = "\t";
protected $inputKeyFields = 1;
protected $outputKeyFields = 1;
/**
* Constructor.
*/
public function __construct()
{
$this->handle = fopen('php://stdin', 'r');
$this->reporterPrefix = isset($_SERVER['stream_stderr_reporter_prefix']) ? $_SERVER['stream_stderr_reporter_prefix'] : 'reporter:';
}
/**
* Read a complete record from the input.
* This default implementation reads a line and trims the trailing newline.
* Returns the result or false if EOF reached.
*
* @return mixed The input record as a string or false if EOF was reached.
*/
protected function read() {
$retval = fgets($this->handle);
if($retval !== false) {
return rtrim($retval, "\n");
}
return false;
}
/**
* Base splitter function.
* Splits a record into key and value; this default implementation assumes a tab character as the separator.
*
* @param string The line read by the read() method.
*
* @return array An array with key and value, or null for malformed input.
*/
protected function split($line)
{
if($this->inputKeyFields == 0) {
// no key
$key = array(null);
$value = $line;
} else {
$parts = array_chunk(explode($this->inputFieldSeparator, $line, $this->inputKeyFields + 1), $this->inputKeyFields); // max keyfields + 1 elements, then chunk to key size
// $parts should now have two entries, one with the key parts, one with just one value part
if(count($parts) != 2) {
// invalid record
return null;
}
$key = $parts[0];
$value = $parts[1][0]; // this is guaranteed to be there or offset [1] wouldn't even exist
}
return array(new Key($key, $this->inputFieldSeparator, $this->inputKeyFields), $value); // return
}
/**
* Emit output.
* Uses the given key and value and emits using these two arguments.
*
* @param mixed The key (string, array or Key object) to emit with.
* @param string The value to emit with.
*/
protected function emit($key, $value)
{
if($key instanceof Key) {
$key = $key->getParts();
} elseif(!is_array($key)) {
$key = array($key);
}
echo implode($this->outputFieldSeparator, $key) . $this->outputFieldSeparator . $value . "\n";
}
/**
* Emit a counter value for statistical purposes.
* Expects a group name, a counter name, and the amount to increment by.
*
* @param string The name of the group the counter belongs to.
* @param string The name of the counter to operate on.
* @param int An optional amount to increment the counter by (default: 1).
*/
protected function emitCounter($group, $counter, $amount = 1)
{
file_put_contents('php://stderr', sprintf("%scounter:%s,%s,%d\n", $this->reporterPrefix, $group, $counter, $amount));
}
/**
* Emit a status message for logging purposes.
*
* @param string The message to produce.
*/
protected function emitStatus($message)
{
file_put_contents('php://stderr', sprintf("%sstatus:%s\n", $this->reporterPrefix, $message));
}
/**
* Main handler function invoked by the runner.
* To be implemented by more specific Mapper and Reducer implementations.
*/
abstract public function handle();
}
?> | php | MIT | 09ba720998748c578ac2cc47b7b53fb011c8579e | 2026-01-05T05:14:56.295411Z | false |
dzuelke/HadooPHP | https://github.com/dzuelke/HadooPHP/blob/09ba720998748c578ac2cc47b7b53fb011c8579e/lib/Hadoophp/MapReduce/Mapper.php | lib/Hadoophp/MapReduce/Mapper.php | <?php
namespace Hadoophp\MapReduce;
require_once('Hadoophp/MapReduce/Base.php');
/**
* Base Mapper class.
*/
abstract class Mapper extends Base
{
public function __construct()
{
parent::__construct();
$this->inputFieldSeparator = isset($_SERVER['stream_map_input_field_separator']) ? $_SERVER['stream_map_input_field_separator'] : "\t";
$this->outputFieldSeparator = isset($_SERVER['stream_map_output_field_separator']) ? $_SERVER['stream_map_output_field_separator'] : "\t";
// hard-code
$this->inputKeyFields = 1;
// input fields can't be configured, but "ignoreKey" (only for TextInputWriter, which we assume is the default) plays a role
if(!isset($_SERVER['stream_map_input_writer_class']) || $_SERVER['stream_map_input_writer_class'] == 'org.apache.hadoop.streaming.io.TextInputWriter') {
// if ignoreKey is true or if it's not set and we're using TextInputFormat (it's a default for that one), set to 0
$isTIF = !isset($_SERVER['mapred_input_format_class']) || $_SERVER['mapred_input_format_class'] == 'org.apache.hadoop.mapred.TextInputFormat';
if(
(!isset($_SERVER['stream_map_input_ignoreKey']) && $isTIF) ||
(isset($_SERVER['stream_map_input_ignoreKey']) && $_SERVER['stream_map_input_ignoreKey'] == 'true')
) {
$this->inputKeyFields = 0;
}
}
$this->outputKeyFields = isset($_SERVER['stream_num_map_output_key_fields']) ? (int)$_SERVER['stream_num_map_output_key_fields'] : 1;
}
/**
* Main handler function, invoked by the runner.
* Will hand each input fragment to the map() method.
*/
public function handle()
{
while(($line = $this->read()) !== false) {
$kv = $this->split($line);
if(!$kv || count($kv) != 2) {
continue;
}
list($key, $value) = $kv;
$this->map($key, $value);
if(HADOOPHP_DEBUG) {
$this->emitCounter('SkippingTaskCounters', 'MapProcessedRecords'); // defined in org.apache.hadoop.mapred.SkipBadRecords
}
}
}
/**
* The mapper implementation.
*
* @param mixed The key for the given record.
* @param mixed The value of the given record.
*/
abstract protected function map($key, $value);
}
?> | php | MIT | 09ba720998748c578ac2cc47b7b53fb011c8579e | 2026-01-05T05:14:56.295411Z | false |
dzuelke/HadooPHP | https://github.com/dzuelke/HadooPHP/blob/09ba720998748c578ac2cc47b7b53fb011c8579e/lib/Hadoophp/MapReduce/Reducer.php | lib/Hadoophp/MapReduce/Reducer.php | <?php
namespace Hadoophp\MapReduce;
require_once('Hadoophp/MapReduce/Base.php');
abstract class Reducer extends Base implements \Iterator
{
protected $previousKey = null;
protected $currentKey = null;
protected $currentValue = null;
/**
* Constructor.
*/
public function __construct()
{
parent::__construct();
$this->inputFieldSeparator = isset($_SERVER['stream_reduce_input_field_separator']) ? $_SERVER['stream_reduce_input_field_separator'] : "\t";
$this->outputFieldSeparator = isset($_SERVER['stream_reduce_output_field_separator']) ? $_SERVER['stream_reduce_output_field_separator'] : "\t";
// yes, this is correct
$this->inputKeyFields = isset($_SERVER['stream_num_map_output_key_fields']) ? (int)$_SERVER['stream_num_map_output_key_fields'] : 1;
$this->outputKeyFields = isset($_SERVER['stream_num_reduce_output_key_fields']) ? (int)$_SERVER['stream_num_reduce_output_key_fields'] : 1;
// init
$this->readAhead();
$this->previousKey = $this->currentKey;
}
/**
* Main handler function, invoked by the runner.
* Will hand each key and an iterator for all corresponding values to the reduce() method.
*/
public function handle()
{
while(($key = $this->getCurrentKey()) !== null) {
$this->reduce($key, $this);
// make sure that if a reducer exits prematurely, we scan ahead to the next key
while($this->valid()) {
$this->next();
}
if(HADOOPHP_DEBUG) {
$this->emitCounter('SkippingTaskCounters', 'ReduceProcessedGroups'); // defined in org.apache.hadoop.mapred.SkipBadRecords
}
$this->reset();
}
}
/**
* Read the next input chunk.
*/
private function readAhead()
{
// reset
$this->currentKey = $this->currentValue = null;
// scan forward to the next valid entry
while(true) {
// ingest
$chunk = $this->read();
if($chunk === false) {
// EOF
return;
} else {
$kv = $this->split($chunk);
if($kv && count($kv) == 2) {
list($this->currentKey, $this->currentValue) = $kv;
return;
}
}
}
}
/**
* Reset the iterator so another foreach() run for the next key will be possible.
*/
public function reset()
{
// allow iteration again
$this->previousKey = $this->currentKey;
}
/**
* @see Iterator::current()
*/
public function current()
{
return $this->currentValue;
}
/**
* Casts the key to string so it always works in foreach()es
* @see Iterator::key()
*/
public function key()
{
// must not cast null to a string, otherwise handle() will reduce forever
return $this->currentKey === null ? $this->currentKey : (string)$this->currentKey;
}
/**
* Return the current key object (unlike key(), this doesn't cast to string).
*/
public function getCurrentKey()
{
return $this->currentKey;
}
/**
* @see Iterator::next()
*/
public function next()
{
$this->readAhead();
}
/**
* @see Iterator::rewind()
*/
public function rewind()
{
// nop
}
/**
* @see Iterator::valid()
*/
public function valid()
{
// make sure iteration ends once the key changes; reset() will take care of changing previousKey so that iteration works once again
if(isset($_SERVER['mapred_partitioner_class']) && $_SERVER['mapred_partitioner_class'] == 'org.apache.hadoop.mapred.lib.KeyFieldBasedPartitioner' && (isset($_SERVER['mapred_text_key_partitioner_options']) || isset($_SERVER['mapreduce_partition_keypartitioner_options']))) {
$options = isset($_SERVER['mapred_text_key_partitioner_options']) ? $_SERVER['mapred_text_key_partitioner_options'] /* 0.20 */ : $_SERVER['mapreduce_partition_keypartitioner_options'] /* 0.23/YARN */;
$ranges = explode(' ', $options);
$ck = $this->currentKey ? $this->currentKey->getParts() : array();
$pk = $this->previousKey ? $this->previousKey->getParts() : array();
foreach($ranges as $range) {
if(preg_match('#^-k(?P<from>\d+(,(?P<to>\d+))?)$#', $range, $matches)) {
$from = $matches['from'] - 1;
$to = (isset($matches['to']) ? $matches['to'] : $matches['from']) - 1;
if(array_slice($ck, $from, $to-$from+1) != array_slice($pk, $from, $to-$from+1)) {
return false;
}
}
}
return true;
} else {
return $this->currentKey == $this->previousKey;
}
}
/**
* The reducer implementation.
*
* @param mixed The key for the given records.
* @param Traversable An iterator delivering the values for the current key.
*/
abstract protected function reduce($key, \Traversable $values);
}
?> | php | MIT | 09ba720998748c578ac2cc47b7b53fb011c8579e | 2026-01-05T05:14:56.295411Z | false |
dzuelke/HadooPHP | https://github.com/dzuelke/HadooPHP/blob/09ba720998748c578ac2cc47b7b53fb011c8579e/lib/Hadoophp/MapReduce/Key.php | lib/Hadoophp/MapReduce/Key.php | <?php
namespace Hadoophp\MapReduce;
/**
* Key class.
*/
class Key implements \ArrayAccess
{
protected $parts = array();
protected $separator = "\t";
protected $count = 1;
public function __construct(array $parts, $separator = "\t", $count = 1)
{
$this->parts = $parts;
$this->separator = $separator;
$this->count = $count;
}
public function __toString()
{
return implode($this->separator, $this->parts);
}
public function offsetExists($offset)
{
return isset($this->parts[$offset]);
}
public function offsetGet($offset)
{
return isset($this->parts[$offset]) ? $this->parts[$offset] : null;
}
public function offsetSet($offset, $value)
{
// nop
}
public function offsetUnset($offset)
{
// nop
}
public function getParts()
{
return $this->parts;
}
public function valid()
{
return $this->count == count($this->parts);
}
} | php | MIT | 09ba720998748c578ac2cc47b7b53fb011c8579e | 2026-01-05T05:14:56.295411Z | false |
pagekit/razr | https://github.com/pagekit/razr/blob/5ba5e580bd71e0907e3966a122ac7b6869bac7f8/src/Token.php | src/Token.php | <?php
namespace Razr;
class Token
{
protected $type;
protected $value;
protected $line;
/**
* Constructor.
*
* @param int $type
* @param string $value
* @param int $line
*/
public function __construct($type, $value, $line)
{
$this->type = $type;
$this->value = $value;
$this->line = $line;
}
/**
* Gets the line.
*
* @return int
*/
public function getLine()
{
return $this->line;
}
/**
* Gets the token type.
*
* @return int
*/
public function getType()
{
return $this->type;
}
/**
* Gets the token value.
*
* @return string
*/
public function getValue()
{
return $this->value;
}
/**
* Tests the token for a type and/or a value.
*
* @param array|integer $type
* @param array|string|null $value
* @return bool
*/
public function test($type, $value = null)
{
if ($value === null && !is_int($type)) {
$value = $type;
$type = $this->type;
}
return ($this->type === $type) && ($value === null || (is_array($value) && in_array($this->value, $value)) || $this->value == $value);
}
/**
* Returns a string with the token details.
*
* @return string
*/
public function __toString()
{
return sprintf('%s (%s)', self::getName($this->type), $this->value);
}
/**
* Returns the token name.
*
* @param int $type
* @return string
*/
public static function getName($type)
{
if ($type == T_PUNCTUATION) {
$type = 'T_PUNCTUATION';
} else {
$type = token_name($type);
}
return $type;
}
}
| php | MIT | 5ba5e580bd71e0907e3966a122ac7b6869bac7f8 | 2026-01-05T05:15:04.388201Z | false |
pagekit/razr | https://github.com/pagekit/razr/blob/5ba5e580bd71e0907e3966a122ac7b6869bac7f8/src/TokenStream.php | src/TokenStream.php | <?php
namespace Razr;
use Razr\Exception\SyntaxErrorException;
class TokenStream
{
protected $tokens;
protected $current = 0;
protected $peek = 0;
/**
* Constructor.
*
* @param array $tokens
*/
public function __construct(array $tokens)
{
$line = 0;
if (!defined('T_PUNCTUATION')) {
define('T_PUNCTUATION', -1);
}
foreach ($tokens as $token) {
if (is_array($token)) {
$this->tokens[] = new Token($token[0], $token[1], $line = $token[2]);
} elseif (is_string($token)) {
$this->tokens[] = new Token(T_PUNCTUATION, $token, $line);
}
}
}
/**
* Get a token.
*
* @param int $number
* @return Token|null
*/
public function get($number = 0)
{
if (isset($this->tokens[$this->current + $number])) {
return $this->tokens[$this->current + $number];
}
}
/**
* Next token.
*
* @return Token|null
*/
public function next()
{
$this->peek = 0;
if (isset($this->tokens[$this->current])) {
return $this->tokens[$this->current++];
}
}
/**
* Previous token.
*
* @return Token|null
*/
public function prev()
{
return $this->tokens[$this->current--];
}
/**
* Gets next token if condition is true.
*
* @param array|integer $type
* @param array|string|null $value
* @return Token|null
*/
public function nextIf($type, $value = null)
{
if ($this->test($type, $value)) {
return $this->next();
}
}
/**
* Tests the current token for a condition.
*
* @param array|integer $type
* @param array|string|null $value
* @return Token|null
*/
public function test($type, $value = null)
{
return $this->tokens[$this->current]->test($type, $value);
}
/**
* Tests the current token for a condition or throws an exception otherwise.
*
* @param array|integer $type
* @param array|string|null $value
* @param string|null $message
* @throws SyntaxErrorException
* @return Token|null
*/
public function expect($type, $value = null, $message = null)
{
$token = $this->tokens[$this->current];
if (!$token->test($type, $value)) {
throw new SyntaxErrorException(sprintf('%sUnexpected token "%s" of value "%s" ("%s" expected%s) on line %s', $message ? "$message. " : "", $token, $token->getValue(), Token::getName($type), $value ? sprintf(' with value "%s"', $value) : '', $token->getLine()));
}
return $token;
}
/**
* Resets the peek pointer to 0.
*/
public function resetPeek()
{
$this->peek = 0;
}
/**
* Moves the peek token forward.
*
* @return Token|null
*/
public function peek()
{
if (isset($this->tokens[$this->current + ++$this->peek])) {
return $this->tokens[$this->current + $this->peek];
} else {
return null;
}
}
/**
* Peeks until a token with the given type is found.
*
* @param array|integer $type
* @param array|string|null $value
* @return Token|null
*/
public function peekUntil($type, $value = null)
{
while($token = $this->peek() and !$token->test($type, $value)) {
$token = null;
}
return $token;
}
/**
* Peeks at the next token, returns it and immediately resets the peek.
*
* @return Token|null
*/
public function glimpse()
{
$peek = $this->peek();
$this->peek = 0;
return $peek;
}
/**
* Returns a string with the token stream details.
*
* @return string
*/
public function __toString()
{
return implode("\n", $this->tokens);
}
}
| php | MIT | 5ba5e580bd71e0907e3966a122ac7b6869bac7f8 | 2026-01-05T05:15:04.388201Z | false |
pagekit/razr | https://github.com/pagekit/razr/blob/5ba5e580bd71e0907e3966a122ac7b6869bac7f8/src/Lexer.php | src/Lexer.php | <?php
namespace Razr;
use Razr\Exception\RuntimeException;
use Razr\Exception\SyntaxErrorException;
class Lexer
{
const STATE_DATA = 0;
const STATE_OUTPUT = 1;
const STATE_DIRECTIVE = 2;
const REGEX_CHAR = '/@{2}|@(?=\(|[a-zA-Z_])/s';
const REGEX_START = '/\(|([a-zA-Z_][a-zA-Z0-9_]*)(\s*\()?/A';
const REGEX_STRING = '/"([^#"\\\\]*(?:\\\\.[^#"\\\\]*)*)"|\'([^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\'/As';
protected $engine;
protected $tokens;
protected $code;
protected $source;
protected $cursor;
protected $lineno;
protected $end;
protected $state;
protected $states;
protected $brackets;
protected $filename;
protected $position;
protected $positions;
/**
* Constructor.
*
* @param Engine $engine
*/
public function __construct(Engine $engine)
{
$this->engine = $engine;
}
/**
* Gets the token stream from a template.
*
* @param string $code
* @param string $filename
* @throws SyntaxErrorException
* @return TokenStream
*/
public function tokenize($code, $filename = null)
{
if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2) {
$encoding = mb_internal_encoding();
mb_internal_encoding('ASCII');
}
$this->code = str_replace(array("\r\n", "\r"), "\n", $code);
$this->source = '';
$this->filename = $filename;
$this->cursor = 0;
$this->lineno = 1;
$this->end = strlen($this->code);
$this->tokens = array();
$this->state = self::STATE_DATA;
$this->states = array();
$this->brackets = array();
$this->position = -1;
preg_match_all(self::REGEX_CHAR, $this->code, $this->positions, PREG_OFFSET_CAPTURE);
while ($this->cursor < $this->end) {
switch ($this->state) {
case self::STATE_DATA:
$this->lexData();
break;
case self::STATE_OUTPUT:
$this->lexOutput();
break;
case self::STATE_DIRECTIVE:
$this->lexDirective();
break;
}
}
if ($this->state != self::STATE_DATA) {
$this->addCode(' ?>');
$this->popState();
}
if (!empty($this->brackets)) {
list($expect, $lineno) = array_pop($this->brackets);
throw new SyntaxErrorException(sprintf('Unclosed "%s" at line %d in file %s', $expect, $lineno, $this->filename));
}
if (isset($encoding)) {
mb_internal_encoding($encoding);
}
return new TokenStream(token_get_all($this->source));
}
/**
* Lex data.
*/
protected function lexData()
{
if ($this->position == count($this->positions[0]) - 1) {
$this->addCode(substr($this->code, $this->cursor));
$this->cursor = $this->end;
return;
}
$position = $this->positions[0][++$this->position];
while ($position[1] < $this->cursor) {
if ($this->position == count($this->positions[0]) - 1) {
return;
}
$position = $this->positions[0][++$this->position];
}
$this->addCode($text = substr($this->code, $this->cursor, $position[1] - $this->cursor));
$this->moveCursor($text);
$this->cursor++;
if (preg_match(self::REGEX_START, $this->code, $match, null, $this->cursor)) {
if (isset($match[1])) {
$this->addCode('<?php /* DIRECTIVE */');
$this->pushState(self::STATE_DIRECTIVE);
$this->addCode($match[1]);
$this->moveCursor($match[1]);
if (isset($match[2])) {
$this->moveCursor(rtrim($match[2], '('));
$this->lexExpression();
}
} else {
$this->addCode('<?php /* OUTPUT */');
$this->pushState(self::STATE_OUTPUT);
$this->lexExpression();
}
}
}
/**
* Lex output.
*/
protected function lexOutput()
{
if (empty($this->brackets)) {
$this->addCode(' ?>');
$this->popState();
} else {
$this->lexExpression();
}
}
/**
* Lex directive.
*/
protected function lexDirective()
{
if (empty($this->brackets)) {
$this->addCode(' ?>');
$this->popState();
} else {
$this->lexExpression();
}
}
/**
* Lex expression.
*/
protected function lexExpression()
{
if (preg_match(self::REGEX_STRING, $this->code, $match, null, $this->cursor)) {
$this->addCode($match[0]);
$this->moveCursor($match[0]);
}
if (strpos('([{', $this->code[$this->cursor]) !== false) {
$this->brackets[] = array($this->code[$this->cursor], $this->lineno);
} elseif (strpos(')]}', $this->code[$this->cursor]) !== false) {
if (empty($this->brackets)) {
throw new SyntaxErrorException(sprintf('Unexpected "%s" at line %d in file %s', $this->code[$this->cursor], $this->lineno, $this->filename));
}
list($expect, $lineno) = array_pop($this->brackets);
if ($this->code[$this->cursor] != strtr($expect, '([{', ')]}')) {
throw new SyntaxErrorException(sprintf('Unclosed "%s" at line %d in file %s', $expect, $lineno, $this->filename));
}
}
$this->addCode($this->code[$this->cursor++]);
}
/**
* Adds a piece of code to the source.
*
* @param string $code
*/
protected function addCode($code)
{
$this->source .= $code;
}
/**
* Moves the cursor of the length of the given text.
*
* @param string $text
*/
protected function moveCursor($text)
{
$this->cursor += strlen($text);
$this->lineno += substr_count($text, "\n");
}
/**
* Pushes a state onto the state stack.
*
* @param int $state
*/
protected function pushState($state)
{
$this->states[] = $this->state;
$this->state = $state;
}
/**
* Pops the last state off the state stack.
*/
protected function popState()
{
if (count($this->states) === 0) {
throw new RuntimeException('Cannot pop state without a previous state');
}
$this->state = array_pop($this->states);
}
}
| php | MIT | 5ba5e580bd71e0907e3966a122ac7b6869bac7f8 | 2026-01-05T05:15:04.388201Z | false |
pagekit/razr | https://github.com/pagekit/razr/blob/5ba5e580bd71e0907e3966a122ac7b6869bac7f8/src/Parser.php | src/Parser.php | <?php
namespace Razr;
class Parser
{
protected $engine;
protected $stream;
protected $filename;
protected $variables;
/**
* Constructor.
*
* @param Engine $engine
*/
public function __construct(Engine $engine)
{
$this->engine = $engine;
}
/**
* Parsing method.
*
* @param TokenStream $stream
* @param string $filename
* @return string
*/
public function parse($stream, $filename = null)
{
$this->stream = $stream;
$this->filename = $filename;
$this->variables = array();
return $this->parseMain();
}
/**
* Parse main.
*
* @return string
*/
public function parseMain()
{
$out = '';
while ($token = $this->stream->next()) {
if ($token->test(T_COMMENT, '/* OUTPUT */')) {
$out .= $this->parseOutput();
} elseif ($token->test(T_COMMENT, '/* DIRECTIVE */')) {
$out .= $this->parseDirective();
} else {
$out .= $token->getValue();
}
}
if ($this->variables) {
$info = sprintf('<?php /* %s */ extract(%s, EXTR_SKIP) ?>', $this->filename, str_replace("\n", '', var_export($this->variables, true)));
} else {
$info = sprintf('<?php /* %s */ ?>', $this->filename);
}
return $info.$out;
}
/**
* Parse output.
*
* @return string
*/
public function parseOutput()
{
$out = "echo \$this->escape(";
while (!$this->stream->test(T_CLOSE_TAG)) {
$out .= $this->parseExpression();
}
return "$out) ";
}
/**
* Parse directive.
*
* @return string
*/
public function parseDirective()
{
$out = '';
foreach ($this->engine->getDirectives() as $directive) {
if ($out = $directive->parse($this->stream, $this->stream->get())) {
break;
}
}
return $out;
}
/**
* Parse expression.
*
* @return string
*/
public function parseExpression()
{
$out = '';
$brackets = array();
do {
if ($token = $this->stream->nextIf(T_STRING)) {
$name = $token->getValue();
if ($this->stream->test('(') && $this->engine->getFunction($name)) {
$out .= sprintf("\$this->callFunction('%s', array%s)", $name, $this->parseExpression());
} else {
$out .= $name;
}
} elseif ($token = $this->stream->nextIf(T_VARIABLE)) {
$out .= $this->parseSubscript($var = $token->getValue());
$this->variables[ltrim($var, '$')] = null;
} else {
$token = $this->stream->next();
if ($token->test(array('(', '['))) {
array_push($brackets, $token);
} elseif ($token->test(array(')', ']'))) {
array_pop($brackets);
}
$out .= $token->getValue();
}
} while (!empty($brackets));
return $out;
}
/**
* Parse subscript.
*
* @param string $out
* @return string
*/
public function parseSubscript($out)
{
while (true) {
if ($this->stream->nextIf('.')) {
if (!$this->stream->test(T_STRING)) {
$this->stream->prev();
break;
}
$val = $this->stream->next()->getValue();
$out = sprintf("\$this->getAttribute(%s, '%s'", $out, $val);
if ($this->stream->test('(')) {
$out .= sprintf(", array%s, 'method')", $this->parseExpression());
} else {
$out .= ")";
}
} elseif ($this->stream->nextIf('[')) {
$exp = '';
while (!$this->stream->test(']')) {
$exp .= $this->parseExpression();
}
$this->stream->expect(']');
$this->stream->next();
$out = sprintf("\$this->getAttribute(%s, %s, array(), 'array')", $out, $exp);
} else {
break;
}
}
return $out;
}
}
| php | MIT | 5ba5e580bd71e0907e3966a122ac7b6869bac7f8 | 2026-01-05T05:15:04.388201Z | false |
pagekit/razr | https://github.com/pagekit/razr/blob/5ba5e580bd71e0907e3966a122ac7b6869bac7f8/src/Engine.php | src/Engine.php | <?php
namespace Razr;
use Razr\Directive\Directive;
use Razr\Directive\DirectiveInterface;
use Razr\Exception\RuntimeException;
use Razr\Extension\CoreExtension;
use Razr\Extension\ExtensionInterface;
use Razr\Loader\LoaderInterface;
use Razr\Storage\FileStorage;
use Razr\Storage\Storage;
use Razr\Storage\StringStorage;
class Engine
{
const VERSION = '0.10.0';
const ANY_CALL = 'any';
const ARRAY_CALL = 'array';
const METHOD_CALL = 'method';
protected $lexer;
protected $parser;
protected $current;
protected $charset = 'UTF-8';
protected $parents = array();
protected $globals = array();
protected $directives = array();
protected $functions = array();
protected $extensions = array();
protected $cache = array();
protected $cachePath;
protected $loader;
private $initialized;
private $template;
private $parameters;
private static $classes = array();
/**
* Constructor.
*
* @param LoaderInterface $loader
* @param string $cachePath
*/
public function __construct(LoaderInterface $loader, $cachePath = null)
{
$this->loader = $loader;
$this->lexer = new Lexer($this);
$this->parser = new Parser($this);
$this->cachePath = $cachePath;
$this->addExtension(new CoreExtension);
}
/**
* Gets the lexer.
*
* @return Lexer
*/
public function getLexer()
{
return $this->lexer;
}
/**
* Gets the parser.
*
* @return Parser
*/
public function getParser()
{
return $this->parser;
}
/**
* Gets the charset.
*
* @return string
*/
public function getCharset()
{
return $this->charset;
}
/**
* Sets the charset.
*
* @param string $charset
*/
public function setCharset($charset)
{
$this->charset = $charset;
}
/**
* Gets all global parameters.
*
* @return array
*/
public function getGlobals()
{
return $this->globals;
}
/**
* Adds a global parameter.
*
* @param string $name
* @param mixed $value
*/
public function addGlobal($name, $value)
{
$this->globals[$name] = $value;
}
/**
* Gets a directives.
*
* @param string $name
* @return Directive
*/
public function getDirective($name)
{
if (!$this->initialized) {
$this->initialize();
}
return isset($this->directives[$name]) ? $this->directives[$name] : null;
}
/**
* Gets the directives.
*
* @return array
*/
public function getDirectives()
{
if (!$this->initialized) {
$this->initialize();
}
return $this->directives;
}
/**
* Adds a directive.
*
* @param DirectiveInterface $directive
* @throws Exception\RuntimeException
*/
public function addDirective(DirectiveInterface $directive)
{
if ($this->initialized) {
throw new RuntimeException(sprintf('Unable to add directive "%s" as they have already been initialized.', $directive->getName()));
}
$directive->setEngine($this);
$this->directives[$directive->getName()] = $directive;
}
/**
* Gets a function.
*
* @param string $name
* @return callable
*/
public function getFunction($name)
{
if (!$this->initialized) {
$this->initialize();
}
return isset($this->functions[$name]) ? $this->functions[$name] : null;
}
/**
* Gets the functions.
*
* @return array
*/
public function getFunctions()
{
if (!$this->initialized) {
$this->initialize();
}
return $this->functions;
}
/**
* Adds a function.
*
* @param string $name
* @param callable $function
* @throws RuntimeException
*/
public function addFunction($name, $function)
{
if ($this->initialized) {
throw new RuntimeException(sprintf('Unable to add function "%s" as they have already been initialized.', $name));
}
$this->functions[$name] = $function;
}
/**
* Gets an extension.
*
* @param string $name
* @return ExtensionInterface
*/
public function getExtension($name)
{
return isset($this->extensions[$name]) ? $this->extensions[$name] : null;
}
/**
* Gets the extensions.
*
* @return array
*/
public function getExtensions()
{
return $this->extensions;
}
/**
* Adds an extension.
*
* @param ExtensionInterface $extension
* @throws Exception\RuntimeException
*/
public function addExtension(ExtensionInterface $extension)
{
if ($this->initialized) {
throw new RuntimeException(sprintf('Unable to add extension "%s" as they have already been initialized.', $extension->getName()));
}
$this->extensions[$extension->getName()] = $extension;
}
/**
* Gets an attribute value from an array or object.
*
* @param mixed $object
* @param mixed $name
* @param array $args
* @param string $type
* @throws \BadMethodCallException
* @throws \Exception
* @return mixed
*/
public function getAttribute($object, $name, array $args = array(), $type = self::ANY_CALL)
{
// array
if ($type == self::ANY_CALL || $type == self::ARRAY_CALL) {
$key = is_bool($name) || is_float($name) ? (int) $name : $name;
if ((is_array($object) && array_key_exists($key, $object)) || ($object instanceof \ArrayAccess && isset($object[$key]))) {
return $object[$key];
}
if ($type == self::ARRAY_CALL) {
return null;
}
}
// object
if (!is_object($object)) {
return null;
}
// property
if ($type == self::ANY_CALL && isset($object->$name)) {
return $object->$name;
}
// method
$call = false;
$name = (string) $name;
$item = strtolower($name);
$class = get_class($object);
if (!isset(self::$classes[$class])) {
self::$classes[$class] = array_change_key_case(array_flip(get_class_methods($object)));
}
if (!isset(self::$classes[$class][$item])) {
if (isset(self::$classes[$class]["get$item"])) {
$name = "get$name";
} elseif (isset(self::$classes[$class]["is$item"])) {
$name = "is$name";
} elseif (isset(self::$classes[$class]["__call"])) {
$call = true;
} else {
return null;
}
}
try {
return call_user_func_array(array($object, $name), $args);
} catch (\BadMethodCallException $e) {
if (!$call) throw $e;
}
}
/**
* Calls a function with an array of arguments.
*
* @param string $name
* @param array $args
* @return mixed
*/
public function callFunction($name, array $args = array())
{
return call_user_func_array($this->getFunction($name), $args);
}
/**
* Decorates a template with another template.
*
* @param string $template
*/
public function extend($template)
{
$this->parents[$this->current] = $template;
}
/**
* Escapes a html entities in a string.
*
* @param mixed $value
* @return string
*/
public function escape($value)
{
if (is_numeric($value)) {
return $value;
}
return is_string($value) ? htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, $this->charset, false) : $value;
}
/**
* Renders a template.
*
* @param string $name
* @param array $parameters
* @throws RuntimeException
* @return string
*/
public function render($name, array $parameters = array())
{
$storage = $this->load($name);
$parameters = array_replace($this->getGlobals(), $parameters);
$this->current = $key = sha1(serialize($storage));
$this->parents[$key] = null;
if (false === $content = $this->evaluate($storage, $parameters)) {
throw new RuntimeException('The template cannot be rendered.');
}
if ($this->parents[$key]) {
$content = $this->render($this->parents[$key], $parameters);
}
return $content;
}
/**
* Evaluates a template.
*
* @param Storage $template
* @param array $parameters
* @return string|false
*/
protected function evaluate(Storage $template, array $parameters = array())
{
$this->template = $template;
$this->parameters = $parameters;
unset($template, $parameters);
extract($this->parameters, EXTR_SKIP);
$this->parameters = null;
if ($this->template instanceof FileStorage) {
ob_start();
require $this->template;
$this->template = null;
return ob_get_clean();
} elseif ($this->template instanceof StringStorage) {
ob_start();
eval('; ?>'.$this->template.'<?php ;');
$this->template = null;
return ob_get_clean();
}
return false;
}
/**
* Compiles a template.
*
* @param string $source
* @param string $filename
* @return string
*/
protected function compile($source, $filename = null)
{
$tokens = $this->lexer->tokenize($source, $filename);
$source = $this->parser->parse($tokens, $filename);
return $source;
}
/**
* Loads a template.
*
* @param string $name
* @throws Exception\RuntimeException
* @throws Exception\InvalidArgumentException
* @return Storage
*/
protected function load($name)
{
if (!$this->initialized) {
$this->initialize();
}
if (isset($this->cache[$name])) {
return $this->cache[$name];
}
$cache = $this->cachePath ? sprintf('%s/%s.cache', $this->cachePath, sha1($name)) : false;
if (!$cache) {
$storage = new StringStorage($this->compile($this->loader->getSource($name), $name));
} else {
if (!is_file($cache) || !$this->isTemplateFresh($name, filemtime($cache))) {
$this->writeCacheFile($cache, $this->compile($this->loader->getSource($name), $name));
}
$storage = new FileStorage($cache);
}
return $this->cache[$name] = $storage;
}
/**
* Initializes the extensions.
*
* @return void
*/
protected function initialize()
{
foreach ($this->extensions as $extension) {
$extension->initialize($this);
}
$this->initialized = true;
}
/**
* Writes cache file
*
* @param string $file
* @param string $content
* @throws RuntimeException
*/
protected function writeCacheFile($file, $content)
{
$dir = dirname($file);
if (!is_dir($dir)) {
if (false === @mkdir($dir, 0777, true) && !is_dir($dir)) {
throw new RuntimeException("Unable to create the cache directory ($dir).");
}
} elseif (!is_writable($dir)) {
throw new RuntimeException("Unable to write in the cache directory ($dir).");
}
if (!file_put_contents($file, $content)) {
throw new RuntimeException("Failed to write cache file ($file).");
}
}
/**
* Checks if template is fresh
*
* @param string $name
* @param int $time
* @return bool
*/
protected function isTemplateFresh($name, $time)
{
foreach ($this->extensions as $extension) {
$r = new \ReflectionObject($extension);
if (filemtime($r->getFileName()) > $time) {
return false;
}
}
return $this->loader->isFresh($name, $time);
}
}
| php | MIT | 5ba5e580bd71e0907e3966a122ac7b6869bac7f8 | 2026-01-05T05:15:04.388201Z | false |
pagekit/razr | https://github.com/pagekit/razr/blob/5ba5e580bd71e0907e3966a122ac7b6869bac7f8/src/Extension/CoreExtension.php | src/Extension/CoreExtension.php | <?php
namespace Razr\Extension;
use Razr\Directive\BlockDirective;
use Razr\Directive\ControlDirective;
use Razr\Directive\ExtendDirective;
use Razr\Directive\IncludeDirective;
use Razr\Directive\RawDirective;
use Razr\Directive\SetDirective;
use Razr\Engine;
use Razr\Exception\InvalidArgumentException;
use Razr\Exception\RuntimeException;
class CoreExtension implements ExtensionInterface
{
protected $blocks = array();
protected $openBlocks = array();
/**
* {@inheritdoc}
*/
public function getName()
{
return 'core';
}
/**
* {@inheritdoc}
*/
public function initialize(Engine $engine)
{
// directives
$engine->addDirective(new BlockDirective);
$engine->addDirective(new ControlDirective);
$engine->addDirective(new ExtendDirective);
$engine->addDirective(new IncludeDirective);
$engine->addDirective(new RawDirective);
$engine->addDirective(new SetDirective);
// functions
$engine->addFunction('e', array($engine, 'escape'));
$engine->addFunction('escape', array($engine, 'escape'));
$engine->addFunction('block', array($this, 'block'));
$engine->addFunction('constant', array($this, 'getConstant'));
$engine->addFunction('json', 'json_encode');
$engine->addFunction('upper', 'strtoupper');
$engine->addFunction('lower', 'strtolower');
$engine->addFunction('format', 'sprintf');
$engine->addFunction('replace', 'strtr');
}
/**
* Gets or sets a block.
*
* @param string $name
* @param mixed $value
* @return string
*/
public function block($name, $value = null)
{
if ($value === null) {
return isset($this->blocks[$name]) ? $this->blocks[$name] : null;
}
$this->blocks[$name] = $value;
}
/**
* Starts a block.
*
* @param string $name
* @throws InvalidArgumentException
*/
public function startBlock($name)
{
if (in_array($name, $this->openBlocks)) {
throw new InvalidArgumentException(sprintf('A block "%s" is already started.', $name));
}
$this->openBlocks[] = $name;
if (!isset($this->blocks[$name])) {
$this->blocks[$name] = null;
}
ob_start();
ob_implicit_flush(0);
}
/**
* Stops a block.
*
* @throws RuntimeException
* @return string
*/
public function endBlock()
{
if (!$this->openBlocks) {
throw new RuntimeException('No block started.');
}
$name = array_pop($this->openBlocks);
$value = ob_get_clean();
if ($this->blocks[$name] === null) {
$this->blocks[$name] = $value;
}
return $this->blocks[$name];
}
/**
* Reset all blocks.
*
* @return void
*/
public function resetBlocks()
{
$this->blocks = array();
$this->openBlocks = array();
}
/**
* Gets a constant from an object.
*
* @param string $name
* @param object $object
* @return mixed
*/
public function getConstant($name, $object = null)
{
if ($object !== null) {
$name = sprintf('%s::%s', get_class($object), $name);
}
return constant($name);
}
}
| php | MIT | 5ba5e580bd71e0907e3966a122ac7b6869bac7f8 | 2026-01-05T05:15:04.388201Z | false |
pagekit/razr | https://github.com/pagekit/razr/blob/5ba5e580bd71e0907e3966a122ac7b6869bac7f8/src/Extension/ExtensionInterface.php | src/Extension/ExtensionInterface.php | <?php
namespace Razr\Extension;
use Razr\Engine;
interface ExtensionInterface
{
/**
* Gets the name.
*
* @return string
*/
public function getName();
/**
* Initializes the extension.
*/
public function initialize(Engine $engine);
}
| php | MIT | 5ba5e580bd71e0907e3966a122ac7b6869bac7f8 | 2026-01-05T05:15:04.388201Z | false |
pagekit/razr | https://github.com/pagekit/razr/blob/5ba5e580bd71e0907e3966a122ac7b6869bac7f8/src/Storage/Storage.php | src/Storage/Storage.php | <?php
namespace Razr\Storage;
abstract class Storage
{
protected $template;
/**
* Constructor.
*
* @param string $template
*/
public function __construct($template)
{
$this->template = $template;
}
/**
* Gets the object string representation.
*
* @return string
*/
public function __toString()
{
return (string) $this->template;
}
/**
* Gets the template content.
*
* @return string
*/
abstract public function getContent();
}
| php | MIT | 5ba5e580bd71e0907e3966a122ac7b6869bac7f8 | 2026-01-05T05:15:04.388201Z | false |
pagekit/razr | https://github.com/pagekit/razr/blob/5ba5e580bd71e0907e3966a122ac7b6869bac7f8/src/Storage/FileStorage.php | src/Storage/FileStorage.php | <?php
namespace Razr\Storage;
class FileStorage extends Storage
{
/**
* @{inheritdoc}
*/
public function getContent()
{
return file_get_contents($this->template);
}
}
| php | MIT | 5ba5e580bd71e0907e3966a122ac7b6869bac7f8 | 2026-01-05T05:15:04.388201Z | false |
pagekit/razr | https://github.com/pagekit/razr/blob/5ba5e580bd71e0907e3966a122ac7b6869bac7f8/src/Storage/StringStorage.php | src/Storage/StringStorage.php | <?php
namespace Razr\Storage;
class StringStorage extends Storage
{
/**
* @{inheritdoc}
*/
public function getContent()
{
return $this->template;
}
}
| php | MIT | 5ba5e580bd71e0907e3966a122ac7b6869bac7f8 | 2026-01-05T05:15:04.388201Z | false |
pagekit/razr | https://github.com/pagekit/razr/blob/5ba5e580bd71e0907e3966a122ac7b6869bac7f8/src/Exception/ExceptionInterface.php | src/Exception/ExceptionInterface.php | <?php
namespace Razr\Exception;
interface ExceptionInterface
{
}
| php | MIT | 5ba5e580bd71e0907e3966a122ac7b6869bac7f8 | 2026-01-05T05:15:04.388201Z | false |
pagekit/razr | https://github.com/pagekit/razr/blob/5ba5e580bd71e0907e3966a122ac7b6869bac7f8/src/Exception/SyntaxErrorException.php | src/Exception/SyntaxErrorException.php | <?php
namespace Razr\Exception;
class SyntaxErrorException extends RuntimeException
{
}
| php | MIT | 5ba5e580bd71e0907e3966a122ac7b6869bac7f8 | 2026-01-05T05:15:04.388201Z | false |
pagekit/razr | https://github.com/pagekit/razr/blob/5ba5e580bd71e0907e3966a122ac7b6869bac7f8/src/Exception/RuntimeException.php | src/Exception/RuntimeException.php | <?php
namespace Razr\Exception;
class RuntimeException extends \RuntimeException implements ExceptionInterface
{
}
| php | MIT | 5ba5e580bd71e0907e3966a122ac7b6869bac7f8 | 2026-01-05T05:15:04.388201Z | false |
pagekit/razr | https://github.com/pagekit/razr/blob/5ba5e580bd71e0907e3966a122ac7b6869bac7f8/src/Exception/InvalidArgumentException.php | src/Exception/InvalidArgumentException.php | <?php
namespace Razr\Exception;
class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface
{
}
| php | MIT | 5ba5e580bd71e0907e3966a122ac7b6869bac7f8 | 2026-01-05T05:15:04.388201Z | false |
pagekit/razr | https://github.com/pagekit/razr/blob/5ba5e580bd71e0907e3966a122ac7b6869bac7f8/src/Directive/RawDirective.php | src/Directive/RawDirective.php | <?php
namespace Razr\Directive;
use Razr\Token;
use Razr\TokenStream;
class RawDirective extends Directive
{
/**
* Constructor.
*/
public function __construct()
{
$this->name = 'raw';
}
/**
* @{inheritdoc}
*/
public function parse(TokenStream $stream, Token $token)
{
if ($stream->nextIf('raw') && $stream->expect('(')) {
$out = 'echo';
while (!$stream->test(T_CLOSE_TAG)) {
$out .= $this->parser->parseExpression();
}
return $out;
}
}
}
| php | MIT | 5ba5e580bd71e0907e3966a122ac7b6869bac7f8 | 2026-01-05T05:15:04.388201Z | false |
pagekit/razr | https://github.com/pagekit/razr/blob/5ba5e580bd71e0907e3966a122ac7b6869bac7f8/src/Directive/Directive.php | src/Directive/Directive.php | <?php
namespace Razr\Directive;
use Razr\Engine;
abstract class Directive implements DirectiveInterface
{
protected $name;
protected $engine;
protected $parser;
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->name;
}
/**
* @{inheritdoc}
*/
public function setEngine(Engine $engine)
{
$this->engine = $engine;
$this->parser = $engine->getParser();
}
}
| php | MIT | 5ba5e580bd71e0907e3966a122ac7b6869bac7f8 | 2026-01-05T05:15:04.388201Z | false |
pagekit/razr | https://github.com/pagekit/razr/blob/5ba5e580bd71e0907e3966a122ac7b6869bac7f8/src/Directive/IncludeDirective.php | src/Directive/IncludeDirective.php | <?php
namespace Razr\Directive;
use Razr\Token;
use Razr\TokenStream;
class IncludeDirective extends Directive
{
/**
* Constructor.
*/
public function __construct()
{
$this->name = 'include';
}
/**
* @{inheritdoc}
*/
public function parse(TokenStream $stream, Token $token)
{
if ($stream->nextIf('include') && $stream->expect('(')) {
return sprintf("\$_defined = array%s; echo(\$this->render(\$_defined[0], array_merge(get_defined_vars(), isset(\$_defined[1]) ? \$_defined[1] : [])))", $this->parser->parseExpression());
}
}
}
| php | MIT | 5ba5e580bd71e0907e3966a122ac7b6869bac7f8 | 2026-01-05T05:15:04.388201Z | false |
pagekit/razr | https://github.com/pagekit/razr/blob/5ba5e580bd71e0907e3966a122ac7b6869bac7f8/src/Directive/ExtendDirective.php | src/Directive/ExtendDirective.php | <?php
namespace Razr\Directive;
use Razr\Token;
use Razr\TokenStream;
class ExtendDirective extends Directive
{
/**
* Constructor.
*/
public function __construct()
{
$this->name = 'extend';
}
/**
* @{inheritdoc}
*/
public function parse(TokenStream $stream, Token $token)
{
if ($stream->nextIf('extend') && $stream->expect('(')) {
return sprintf("\$this->extend%s", $this->parser->parseExpression());
}
}
}
| php | MIT | 5ba5e580bd71e0907e3966a122ac7b6869bac7f8 | 2026-01-05T05:15:04.388201Z | false |
pagekit/razr | https://github.com/pagekit/razr/blob/5ba5e580bd71e0907e3966a122ac7b6869bac7f8/src/Directive/BlockDirective.php | src/Directive/BlockDirective.php | <?php
namespace Razr\Directive;
use Razr\Token;
use Razr\TokenStream;
class BlockDirective extends Directive
{
/**
* Constructor.
*/
public function __construct()
{
$this->name = 'block';
}
/**
* @{inheritdoc}
*/
public function parse(TokenStream $stream, Token $token)
{
if ($stream->nextIf('block') && $stream->expect('(')) {
return sprintf("\$this->getExtension('core')->startBlock%s", $this->parser->parseExpression());
}
if ($stream->nextIf('endblock')) {
return "echo(\$this->getExtension('core')->endBlock())";
}
}
}
| php | MIT | 5ba5e580bd71e0907e3966a122ac7b6869bac7f8 | 2026-01-05T05:15:04.388201Z | false |
pagekit/razr | https://github.com/pagekit/razr/blob/5ba5e580bd71e0907e3966a122ac7b6869bac7f8/src/Directive/FunctionDirective.php | src/Directive/FunctionDirective.php | <?php
namespace Razr\Directive;
use Razr\Token;
use Razr\TokenStream;
class FunctionDirective extends Directive
{
protected $function;
protected $escape;
/**
* Constructor.
*
* @param string $name
* @param callable $function
* @param bool $escape
*/
public function __construct($name, $function, $escape = false)
{
$this->name = $name;
$this->function = $function;
$this->escape = $escape;
}
/**
* Calls the function with an array of arguments.
*
* @param array $args
* @return mixed
*/
public function call(array $args = array())
{
return call_user_func_array($this->function, $args);
}
/**
* @{inheritdoc}
*/
public function parse(TokenStream $stream, Token $token)
{
if ($stream->nextIf($this->name)) {
$out = sprintf("\$this->getDirective('%s')->call(%s)", $this->name, $stream->test('(') ? 'array' . $this->parser->parseExpression() : '');
if ($this->escape) {
$out = sprintf("\$this->escape(%s)", $out);
}
return sprintf("echo(%s)", $out);
}
}
}
| php | MIT | 5ba5e580bd71e0907e3966a122ac7b6869bac7f8 | 2026-01-05T05:15:04.388201Z | false |
pagekit/razr | https://github.com/pagekit/razr/blob/5ba5e580bd71e0907e3966a122ac7b6869bac7f8/src/Directive/ControlDirective.php | src/Directive/ControlDirective.php | <?php
namespace Razr\Directive;
use Razr\Token;
use Razr\TokenStream;
class ControlDirective extends Directive
{
protected $control;
protected $controlEnd;
/**
* Constructor.
*/
public function __construct()
{
$this->name = 'control';
$this->control = array(T_FOR, T_FOREACH, T_IF, T_ELSEIF, T_ELSE, T_WHILE);
$this->controlEnd = array(T_ENDFOR, T_ENDFOREACH, T_ENDIF, T_ENDWHILE);
}
/**
* @{inheritdoc}
*/
public function parse(TokenStream $stream, Token $token)
{
$control = in_array($token->getType(), $this->control);
if ($control || in_array($token->getType(), $this->controlEnd)) {
$out = '';
while (!$stream->test(T_CLOSE_TAG)) {
$out .= $this->parser->parseExpression();
}
if ($control) {
$out .= ':';
}
return $out;
}
}
}
| php | MIT | 5ba5e580bd71e0907e3966a122ac7b6869bac7f8 | 2026-01-05T05:15:04.388201Z | false |
pagekit/razr | https://github.com/pagekit/razr/blob/5ba5e580bd71e0907e3966a122ac7b6869bac7f8/src/Directive/SetDirective.php | src/Directive/SetDirective.php | <?php
namespace Razr\Directive;
use Razr\Token;
use Razr\TokenStream;
class SetDirective extends Directive
{
/**
* Constructor.
*/
public function __construct()
{
$this->name = 'set';
}
/**
* @{inheritdoc}
*/
public function parse(TokenStream $stream, Token $token)
{
if ($stream->nextIf('set') && $stream->expect('(')) {
$out = '';
while (!$stream->test(T_CLOSE_TAG)) {
$out .= $this->parser->parseExpression();
}
return $out;
}
}
}
| php | MIT | 5ba5e580bd71e0907e3966a122ac7b6869bac7f8 | 2026-01-05T05:15:04.388201Z | false |
pagekit/razr | https://github.com/pagekit/razr/blob/5ba5e580bd71e0907e3966a122ac7b6869bac7f8/src/Directive/DirectiveInterface.php | src/Directive/DirectiveInterface.php | <?php
namespace Razr\Directive;
use Razr\Engine;
use Razr\Token;
use Razr\TokenStream;
interface DirectiveInterface
{
/**
* Gets the name.
*
* @return string
*/
public function getName();
/**
* Sets the engine.
*
* @param $engine
*/
public function setEngine(Engine $engine);
/**
* Parses a directive.
*
* @param TokenStream $stream
* @param Token $token
* @return string
*/
public function parse(TokenStream $stream, Token $token);
}
| php | MIT | 5ba5e580bd71e0907e3966a122ac7b6869bac7f8 | 2026-01-05T05:15:04.388201Z | false |
pagekit/razr | https://github.com/pagekit/razr/blob/5ba5e580bd71e0907e3966a122ac7b6869bac7f8/src/Loader/FilesystemLoader.php | src/Loader/FilesystemLoader.php | <?php
namespace Razr\Loader;
use Razr\Exception\RuntimeException;
class FilesystemLoader implements LoaderInterface
{
protected $paths;
/**
* Constructor.
*
* @param array $paths
*/
public function __construct($paths = array())
{
$this->paths = (array) $paths;
}
/**
* {@inheritdoc}
*/
public function getSource($name)
{
return file_get_contents($this->findTemplate($name));
}
/**
* {@inheritdoc}
*/
public function getCacheKey($name)
{
return $this->findTemplate($name);
}
/**
* {@inheritdoc}
*/
public function isFresh($name, $time)
{
return filemtime($this->findTemplate($name)) <= $time;
}
/**
* Finds a template by a given name.
*/
protected function findTemplate($name)
{
$name = (string) $name;
if (self::isAbsolutePath($name) && is_file($name)) {
return $name;
}
$name = ltrim(strtr($name, '\\', '/'), '/');
foreach ($this->paths as $path) {
if (is_file($file = $path.'/'.$name)) {
return $file;
}
}
throw new RuntimeException(sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths)));
}
/**
* Returns true if the file is an existing absolute path.
*
* @param string $file
* @return boolean
*/
protected static function isAbsolutePath($file)
{
if ($file[0] == '/' || $file[0] == '\\' || (strlen($file) > 3 && ctype_alpha($file[0]) && $file[1] == ':' && ($file[2] == '\\' || $file[2] == '/')) || null !== parse_url($file, PHP_URL_SCHEME)) {
return true;
}
return false;
}
}
| php | MIT | 5ba5e580bd71e0907e3966a122ac7b6869bac7f8 | 2026-01-05T05:15:04.388201Z | false |
pagekit/razr | https://github.com/pagekit/razr/blob/5ba5e580bd71e0907e3966a122ac7b6869bac7f8/src/Loader/LoaderInterface.php | src/Loader/LoaderInterface.php | <?php
namespace Razr\Loader;
interface LoaderInterface
{
/**
* Gets the source code of a template, given its name.
*
* @param string $name
* @return string
*/
public function getSource($name);
/**
* Gets the cache key to use for the cache for a given template name.
*
* @param string $name
* @return string
*/
public function getCacheKey($name);
/**
* Returns true if the template is still fresh.
*
* @param string $name
* @param int $time
* @return bool
*/
public function isFresh($name, $time);
}
| php | MIT | 5ba5e580bd71e0907e3966a122ac7b6869bac7f8 | 2026-01-05T05:15:04.388201Z | false |
pagekit/razr | https://github.com/pagekit/razr/blob/5ba5e580bd71e0907e3966a122ac7b6869bac7f8/src/Loader/StringLoader.php | src/Loader/StringLoader.php | <?php
namespace Razr\Loader;
class StringLoader implements LoaderInterface
{
/**
* {@inheritdoc}
*/
public function getSource($name)
{
return $name;
}
/**
* {@inheritdoc}
*/
public function getCacheKey($name)
{
return $name;
}
/**
* {@inheritdoc}
*/
public function isFresh($name, $time)
{
return true;
}
}
| php | MIT | 5ba5e580bd71e0907e3966a122ac7b6869bac7f8 | 2026-01-05T05:15:04.388201Z | false |
pagekit/razr | https://github.com/pagekit/razr/blob/5ba5e580bd71e0907e3966a122ac7b6869bac7f8/src/Loader/ChainLoader.php | src/Loader/ChainLoader.php | <?php
namespace Razr\Loader;
use Razr\Exception\RuntimeException;
class ChainLoader implements LoaderInterface
{
protected $loaders = array();
/**
* Constructor.
*
* @param LoaderInterface[] $loaders
*/
public function __construct(array $loaders = array())
{
foreach ($loaders as $loader) {
$this->addLoader($loader);
}
}
/**
* Adds a loader instance.
*
* @param LoaderInterface $loader
*/
public function addLoader(LoaderInterface $loader)
{
$this->loaders[] = $loader;
}
/**
* {@inheritdoc}
*/
public function getSource($name)
{
foreach ($this->loaders as $loader) {
try {
return $loader->getSource($name);
} catch (RuntimeException $e) {}
}
throw new RuntimeException(sprintf('Template "%s" is not defined (%s).', $name));
}
/**
* {@inheritdoc}
*/
public function getCacheKey($name)
{
foreach ($this->loaders as $loader) {
try {
return $loader->getCacheKey($name);
} catch (RuntimeException $e) {}
}
throw new RuntimeException(sprintf('Template "%s" is not defined (%s).', $name));
}
/**
* {@inheritdoc}
*/
public function isFresh($name, $time)
{
foreach ($this->loaders as $loader) {
try {
return $loader->isFresh($name, $time);
} catch (RuntimeException $e) {}
}
return false;
}
}
| php | MIT | 5ba5e580bd71e0907e3966a122ac7b6869bac7f8 | 2026-01-05T05:15:04.388201Z | false |
pagekit/razr | https://github.com/pagekit/razr/blob/5ba5e580bd71e0907e3966a122ac7b6869bac7f8/example/autoload.php | example/autoload.php | <?php
spl_autoload_register(function($class) {
$class = str_replace('Razr\\', '', $class);
$path = realpath(__DIR__.'/../src/').'/';
if (($namespace = strrpos($class = ltrim($class, '\\'), '\\')) !== false) {
$path .= strtr(substr($class, 0, ++$namespace), '\\', '/');
}
require($path.strtr(substr($class, $namespace), '_', '/').'.php');
});
class Article
{
const NAME = 'Constant Name';
protected $title;
protected $content;
protected $author;
protected $date;
public function __construct($title, $content, $author, $date = null)
{
$this->title = $title;
$this->content = $content;
$this->author = $author;
$this->date = $date ?: new \DateTime;
}
public function getTitle()
{
return $this->title;
}
public function getContent()
{
return $this->content;
}
public function getAuthor()
{
return $this->author;
}
public function getDate()
{
return $this->date;
}
}
| php | MIT | 5ba5e580bd71e0907e3966a122ac7b6869bac7f8 | 2026-01-05T05:15:04.388201Z | false |
pagekit/razr | https://github.com/pagekit/razr/blob/5ba5e580bd71e0907e3966a122ac7b6869bac7f8/example/index.php | example/index.php | <?php
require __DIR__.'/autoload.php';
use Razr\Engine;
use Razr\Loader\FilesystemLoader;
// simple array
$array = array();
$array['title'] = 'I am the walrus';
$array['artist'] = array('name' => 'The Beatles', 'homepage' => 'http://www.thebeatles.com');
// simple object
$object = new stdClass;
$object->title = 'I am the walrus';
$object->artist = array('name' => 'The Beatles', 'homepage' => 'http://www.thebeatles.com');
// article object
$article = new Article('My article', 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.', 'Me');
// render template
$razr = new Engine(new FilesystemLoader(__DIR__));
function hello($str) { echo "Hello ".$str; };
// $razr->addFunction('hello', 'hello');
echo $razr->render('template.razr', array(
'name' => 'World',
'pi' => 3.14159265359,
'number' => -5,
'now' => new DateTime,
'array' => $array,
'object' => $object,
'article' => $article
));
| php | MIT | 5ba5e580bd71e0907e3966a122ac7b6869bac7f8 | 2026-01-05T05:15:04.388201Z | false |
JacobBennett/SendyPHP | https://github.com/JacobBennett/SendyPHP/blob/590ae66557d625cf4732b9a3d302a14df2234bb7/bootstrap/autoload.php | bootstrap/autoload.php | <?php
/**
* An example of a project-specific implementation.
*
* After registering this autoload function with SPL, the following line
* would cause the function to attempt to load the \Foo\Bar\Baz\Qux class
* from /path/to/project/src/Baz/Qux.php:
*
* new \Foo\Bar\Baz\Qux;
*
* @param string $class The fully-qualified class name.
* @return void
*/
spl_autoload_register(function ($class) {
// project-specific namespace prefix
$prefix = 'SandyPHP\\';
// base directory for the namespace prefix
$base_dir = __DIR__ . '/src/';
// does the class use the namespace prefix?
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
// no, move to the next registered autoloader
return;
}
// get the relative class name
$relative_class = substr($class, $len);
// replace the namespace prefix with the base directory, replace namespace
// separators with directory separators in the relative class name, append
// with .php
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
// if the file exists, require it
if (file_exists($file)) {
require $file;
}
}); | php | MIT | 590ae66557d625cf4732b9a3d302a14df2234bb7 | 2026-01-05T05:15:17.755469Z | false |
JacobBennett/SendyPHP | https://github.com/JacobBennett/SendyPHP/blob/590ae66557d625cf4732b9a3d302a14df2234bb7/src/SendyPHP.php | src/SendyPHP.php | <?php
namespace SendyPHP;
/**
* Sendy Class
*/
class SendyPHP
{
protected $installation_url;
protected $api_key;
protected $list_id;
public function __construct(array $config)
{
//error checking
$list_id = @$config['list_id'];
$installation_url = @$config['installation_url'];
$api_key = @$config['api_key'];
if (empty($list_id)) {
throw new \Exception("Required config parameter [list_id] is not set or empty", 1);
}
if (empty($installation_url)) {
throw new \Exception("Required config parameter [installation_url] is not set or empty", 1);
}
if (empty($api_key)) {
throw new \Exception("Required config parameter [api_key] is not set or empty", 1);
}
$this->list_id = $list_id;
$this->installation_url = $installation_url;
$this->api_key = $api_key;
}
public function setListId($list_id)
{
if (empty($list_id)) {
throw new \Exception("Required config parameter [list_id] is not set", 1);
}
$this->list_id = $list_id;
}
public function getListId()
{
return $this->list_id;
}
public function subscribe(array $values)
{
$type = 'subscribe';
//Send the subscribe
$values = array_merge($values, array('api_key' => $this->api_key)); //add api key
$result = strval($this->buildAndSend($type, $values));
//Handle results
switch ($result) {
case '1':
return array(
'status' => true,
'message' => 'Subscribed'
);
break;
case 'Already subscribed.':
return array(
'status' => true,
'message' => 'Already subscribed.'
);
break;
default:
return array(
'status' => false,
'message' => $result
);
break;
}
}
public function unsubscribe($email)
{
$type = 'unsubscribe';
//Send the unsubscribe
$result = strval($this->buildAndSend($type, array('email' => $email, 'api_key' => $this->api_key))); //add api key
//Handle results
switch ($result) {
case '1':
return array(
'status' => true,
'message' => 'Unsubscribed'
);
break;
default:
return array(
'status' => false,
'message' => $result
);
break;
}
}
public function substatus($email)
{
$type = 'api/subscribers/subscription-status.php';
//Send the request for status
$result = $this->buildAndSend($type, array(
'email' => $email,
'api_key' => $this->api_key,
'list_id' => $this->list_id
));
//Handle the results
switch ($result) {
case 'Subscribed':
case 'Unsubscribed':
case 'Unconfirmed':
case 'Bounced':
case 'Soft bounced':
case 'Complained':
return array(
'status' => true,
'message' => $result
);
break;
default:
return array(
'status' => false,
'message' => $result
);
break;
}
}
public function subcount($list = "")
{
$type = 'api/subscribers/active-subscriber-count.php';
//if a list is passed in use it, otherwise use $this->list_id
if (empty($list)) {
$list = $this->list_id;
}
//handle exceptions
if (empty($list)) {
throw new \Exception("method [subcount] requires parameter [list] or [$this->list_id] to be set.", 1);
}
//Send request for subcount
$result = $this->buildAndSend($type, array(
'api_key' => $this->api_key,
'list_id' => $list
));
//Handle the results
if (is_numeric($result)) {
return array(
'status' => true,
'message' => $result
);
}
//Error
return array(
'status' => false,
'message' => $result
);
}
public function createCampaign(array $values)
{
$type = 'api/campaigns/create.php';
//Global options
$global_options = array(
'api_key' => $this->api_key
);
//Merge the passed in values with the global options
$values = array_merge($global_options, $values);
//Send request for campaign
$result = $this->buildAndSend($type, $values);
//Handle the results
switch ($result) {
case 'Campaign created':
case 'Campaign created and now sending':
return array(
'status' => true,
'message' => $result
);
break;
default:
return array(
'status' => false,
'message' => $result
);
break;
}
}
private function buildAndSend($type, array $values)
{
//error checking
if (empty($type)) {
throw new \Exception("Required config parameter [type] is not set or empty", 1);
}
if (empty($values)) {
throw new \Exception("Required config parameter [values] is not set or empty", 1);
}
//Global options for return
$return_options = array(
'list' => $this->list_id,
'boolean' => 'true'
);
//Merge the passed in values with the options for return
$content = array_merge($values, $return_options);
//build a query using the $content
$postdata = http_build_query($content);
$ch = curl_init($this->installation_url .'/'. $type);
// Settings to disable SSL verification for testing (leave commented for production use)
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}
| php | MIT | 590ae66557d625cf4732b9a3d302a14df2234bb7 | 2026-01-05T05:15:17.755469Z | false |
JacobBennett/SendyPHP | https://github.com/JacobBennett/SendyPHP/blob/590ae66557d625cf4732b9a3d302a14df2234bb7/src/test/SendyPHPTest.php | src/test/SendyPHPTest.php | <?php
use SendyPHP\SendyPHP;
/**
* @author : Gayan Hewa
*/
class SendyPHPTest extends PHPUnit_Framework_TestCase
{
protected $sendy;
public function setUp()
{
$config = [
'api_key' => 'xxx', //your API key is available in Settings
'installation_url' => 'http://aaa.aaa.com', //Your Sendy installation
'list_id' => 'xxx'//Users - vEpmBm892Lq3bp1f8Ebzg0NQ' //Users list
];
$sendy = new SendyPHP($config);
$this->sendy = $sendy;
}
/**
* Test Subscribe status
* @return void
*/
public function test_failed_substatus()
{
//test@test.com - does not exist
$result = $this->sendy->substatus('test@test.com');
//var_dump($result);
$this->assertEquals($result['message'], 'Email does not exist in list');
$this->assertEquals($result['status'], false);
}
/**
* Subscribe new user test
* @return void
*/
public function test_subscribe()
{
$user = array(
'name'=>'Gayan',
'email' => 'gayanhewa@gmail.com'
);
$result = $this->sendy->subscribe($user);
//var_dump($result);
$this->assertEquals($result['message'], 'Subscribed');
$this->assertEquals($result['status'], true);
}
/**
* Unsubscribe test
* @return void
*/
public function test_unsubscribe()
{
$result = $this->sendy->unsubscribe('gayanhewa@gmail.com');
//var_dump($result);
$this->assertEquals($result['message'], 'Unsubscribed');
$this->assertEquals($result['status'], true);
}
public function test_subcount()
{
$result = $this->sendy->subcount();
//var_dump($result);
//Number of subscribesin the list
$this->assertEquals($result['message'], '2');
}
}
?> | php | MIT | 590ae66557d625cf4732b9a3d302a14df2234bb7 | 2026-01-05T05:15:17.755469Z | false |
icetalker/filament-table-repeater | https://github.com/icetalker/filament-table-repeater/blob/f633ecb3f54f3adc93f1e6a90a40dfe8bc03483a/.php-cs-fixer.dist.php | .php-cs-fixer.dist.php | <?php
$finder = Symfony\Component\Finder\Finder::create()
->in([
__DIR__ . '/src',
__DIR__ . '/tests',
])
->name('*.php')
->notName('*.blade.php')
->ignoreDotFiles(true)
->ignoreVCS(true);
return (new PhpCsFixer\Config())
->setRules([
'@PSR12' => true,
'array_syntax' => ['syntax' => 'short'],
'ordered_imports' => ['sort_algorithm' => 'alpha'],
'no_unused_imports' => true,
'not_operator_with_successor_space' => true,
'trailing_comma_in_multiline' => true,
'phpdoc_scalar' => true,
'unary_operator_spaces' => true,
'binary_operator_spaces' => true,
'blank_line_before_statement' => [
'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'],
],
'phpdoc_single_line_var_spacing' => true,
'phpdoc_var_without_name' => true,
'class_attributes_separation' => [
'elements' => [
'method' => 'one',
],
],
'method_argument_space' => [
'on_multiline' => 'ensure_fully_multiline',
'keep_multiple_spaces_after_comma' => true,
],
'single_trait_insert_per_statement' => true,
])
->setFinder($finder);
| php | MIT | f633ecb3f54f3adc93f1e6a90a40dfe8bc03483a | 2026-01-05T05:15:40.280096Z | false |
icetalker/filament-table-repeater | https://github.com/icetalker/filament-table-repeater/blob/f633ecb3f54f3adc93f1e6a90a40dfe8bc03483a/lang/en/components.php | lang/en/components.php | <?php
return [
'table-repeater' =>[
'collapsed' => 'Content collapsed.'
]
]; | php | MIT | f633ecb3f54f3adc93f1e6a90a40dfe8bc03483a | 2026-01-05T05:15:40.280096Z | false |
icetalker/filament-table-repeater | https://github.com/icetalker/filament-table-repeater/blob/f633ecb3f54f3adc93f1e6a90a40dfe8bc03483a/lang/ckb/components.php | lang/ckb/components.php | <?php
return [
'table-repeater' =>[
'collapsed' => 'ناوەڕۆکەکە داخرا.'
]
]; | php | MIT | f633ecb3f54f3adc93f1e6a90a40dfe8bc03483a | 2026-01-05T05:15:40.280096Z | false |
icetalker/filament-table-repeater | https://github.com/icetalker/filament-table-repeater/blob/f633ecb3f54f3adc93f1e6a90a40dfe8bc03483a/lang/ar/components.php | lang/ar/components.php | <?php
return [
'table-repeater' =>[
'collapsed' => 'انهار المحتوى.'
]
]; | php | MIT | f633ecb3f54f3adc93f1e6a90a40dfe8bc03483a | 2026-01-05T05:15:40.280096Z | false |
icetalker/filament-table-repeater | https://github.com/icetalker/filament-table-repeater/blob/f633ecb3f54f3adc93f1e6a90a40dfe8bc03483a/src/FilamentTableRepeaterServiceProvider.php | src/FilamentTableRepeaterServiceProvider.php | <?php
namespace Icetalker\FilamentTableRepeater;
use Filament\Support\Assets\Css;
use Filament\Support\Facades\FilamentAsset;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
class FilamentTableRepeaterServiceProvider extends PackageServiceProvider
{
public function boot()
{
$this->registerAssets();
$this->bootLoaders();
$this->bootPublishing();
}
public function configurePackage(Package $package): void
{
$package
->name('filament-table-repeater')
->hasConfigFile()
->hasViews();
}
protected function bootLoaders()
{
$this->loadViewsFrom(__DIR__ . '/../resources/views', 'filament-table-repeater');
$this->loadTranslationsFrom(__DIR__ . '/../lang', 'filament-table-repeater');
}
protected function bootPublishing()
{
$this->publishes([
__DIR__ . '/../resources/views' => resource_path('views/vendor/filament-table-repeater'),
], 'filament-table-repeater');
}
protected function registerAssets()
{
FilamentAsset::register([
Css::make('table-repeater', __DIR__ .'/../dist/css/table-repeater.css'),
], 'filament-table-repeater');
}
}
| php | MIT | f633ecb3f54f3adc93f1e6a90a40dfe8bc03483a | 2026-01-05T05:15:40.280096Z | false |
icetalker/filament-table-repeater | https://github.com/icetalker/filament-table-repeater/blob/f633ecb3f54f3adc93f1e6a90a40dfe8bc03483a/src/Facades/FilamentTableRepeater.php | src/Facades/FilamentTableRepeater.php | <?php
namespace Icetalker\FilamentTableRepeater\Facades;
use Illuminate\Support\Facades\Facade;
/**
* @see \Icetalker\FilamentTableRepeater\FilamentTableRepeater
*/
class FilamentTableRepeater extends Facade
{
protected static function getFacadeAccessor()
{
return 'filament-table-repeater';
}
}
| php | MIT | f633ecb3f54f3adc93f1e6a90a40dfe8bc03483a | 2026-01-05T05:15:40.280096Z | false |
icetalker/filament-table-repeater | https://github.com/icetalker/filament-table-repeater/blob/f633ecb3f54f3adc93f1e6a90a40dfe8bc03483a/src/Forms/Components/TableRepeater.php | src/Forms/Components/TableRepeater.php | <?php
namespace Icetalker\FilamentTableRepeater\Forms\Components;
use Closure;
use Filament\Actions\Action;
use Filament\Actions\ActionGroup;
use Filament\Forms\Components\Repeater;
use Filament\Schemas\Components\Component;
use Filament\Schemas\Schema;
use Illuminate\Contracts\Support\Htmlable;
class TableRepeater extends Repeater
{
protected string $view = 'filament-table-repeater::table-repeater';
//columns for table header
protected array|null $columnLabels = [];
protected array| Closure | null $colStyles = null;
protected function setUp(): void
{
$this->columnSpanFull();
parent::setUp();
}
public function getColumnLabels(): array|null
{
return $this->columnLabels;
}
public function childComponents(array | Schema | Component | Action | ActionGroup | string | Htmlable | Closure | null $components, string $key = 'default'): static
{
foreach ($components as $component) {
$component->hiddenLabel(); //Disable Label, only show Inputs inside table
$this->childComponents[$key][] = $component;
//Set Columen Labels
$this->columnLabels[] = [
'component' => $component->getName(),
'name' => $component->getLabel(),
'display' => ($component->isHidden() || ($component instanceof \Filament\Forms\Components\Hidden)) ? false : true,
];
}
return $this;
}
public function colStyles(array | Closure $colstyles): static
{
$this->colStyles = $colstyles;
return $this;
}
public function getColStyles(): array| Closure | null
{
return $this->evaluate($this->colStyles);
}
//So that `OrderColumn()` could be used before `relationship()`
public function relationship(string | Closure | null $name = null, ?Closure $modifyQueryUsing = null, ?Closure $modifyRecordsUsing = null): static
{
parent::relationship($name, $modifyQueryUsing, $modifyRecordsUsing);
if ($this->orderColumn) {
$this->reorderable($this->evaluate($this->orderColumn));
}
return $this;
}
}
| php | MIT | f633ecb3f54f3adc93f1e6a90a40dfe8bc03483a | 2026-01-05T05:15:40.280096Z | false |
icetalker/filament-table-repeater | https://github.com/icetalker/filament-table-repeater/blob/f633ecb3f54f3adc93f1e6a90a40dfe8bc03483a/tests/Pest.php | tests/Pest.php | <?php
use Icetalker\FilamentTableRepeater\Tests\TestCase;
uses(TestCase::class)->in(__DIR__);
| php | MIT | f633ecb3f54f3adc93f1e6a90a40dfe8bc03483a | 2026-01-05T05:15:40.280096Z | false |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.