| 
 | 1 | +<?php  | 
 | 2 | +/**  | 
 | 3 | + * Initializes ACF updater logic and logic specific to ACF direct downloads.  | 
 | 4 | + *  | 
 | 5 | + * @package ACF  | 
 | 6 | + */  | 
 | 7 | + | 
 | 8 | +if ( ! defined( 'ABSPATH' ) ) {  | 
 | 9 | +	exit;  | 
 | 10 | +}  | 
 | 11 | + | 
 | 12 | +// Include updater.  | 
 | 13 | +acf_include( 'includes/Updater/Updater.php' );  | 
 | 14 | + | 
 | 15 | +if ( ! class_exists( 'ACF_Updates' ) ) {  | 
 | 16 | +	/**  | 
 | 17 | +	 * The main function responsible for returning the acf_updates singleton.  | 
 | 18 | +	 * Use this function like you would a global variable, except without needing to declare the global.  | 
 | 19 | +	 *  | 
 | 20 | +	 * Example: <?php $acf_updates = acf_updates(); ?>  | 
 | 21 | +	 *  | 
 | 22 | +	 * @since   5.5.12  | 
 | 23 | +	 *  | 
 | 24 | +	 * @return ACF\Updater The singleton instance of Updater.  | 
 | 25 | +	 */  | 
 | 26 | +	function acf_updates() {  | 
 | 27 | +		global $acf_updates;  | 
 | 28 | +		if ( ! isset( $acf_updates ) ) {  | 
 | 29 | +			$acf_updates = new ACF\Updater();  | 
 | 30 | +		}  | 
 | 31 | +		return $acf_updates;  | 
 | 32 | +	}  | 
 | 33 | + | 
 | 34 | +	/**  | 
 | 35 | +	 * Alias of acf_updates()->add_plugin().  | 
 | 36 | +	 *  | 
 | 37 | +	 * @since   5.5.10  | 
 | 38 | +	 *  | 
 | 39 | +	 * @param   array $plugin Plugin data array.  | 
 | 40 | +	 */  | 
 | 41 | +	function acf_register_plugin_update( $plugin ) {  | 
 | 42 | +		acf_updates()->add_plugin( $plugin );  | 
 | 43 | +	}  | 
 | 44 | + | 
 | 45 | +	/**  | 
 | 46 | +	 * Register a dummy ACF_Updates class for back compat.  | 
 | 47 | +	 */  | 
 | 48 | +	class ACF_Updates {} //phpcs:ignore -- Back compat.  | 
 | 49 | +}  | 
 | 50 | + | 
 | 51 | +/**  | 
 | 52 | + * Registers updates for the free version of ACF hosted on connect.  | 
 | 53 | + *  | 
 | 54 | + * @return void  | 
 | 55 | + */  | 
 | 56 | +function acf_register_free_updates() {  | 
 | 57 | +	// If we're ACF free, register the updater.  | 
 | 58 | +	if ( function_exists( 'acf_is_pro' ) && ! acf_is_pro() ) {  | 
 | 59 | +		acf_register_plugin_update(  | 
 | 60 | +			array(  | 
 | 61 | +				'id'       => 'acf',  | 
 | 62 | +				'slug'     => acf_get_setting( 'slug' ),  | 
 | 63 | +				'basename' => acf_get_setting( 'basename' ),  | 
 | 64 | +				'version'  => acf_get_setting( 'version' ),  | 
 | 65 | +			)  | 
 | 66 | +		);  | 
 | 67 | +	}  | 
 | 68 | +}  | 
 | 69 | +add_action( 'acf/init', 'acf_register_free_updates' );  | 
 | 70 | + | 
 | 71 | +/**  | 
 | 72 | + * Filters the "Update Source" param in the ACF site health.  | 
 | 73 | + *  | 
 | 74 | + * @since 6.3.11.1  | 
 | 75 | + *  | 
 | 76 | + * @param string $update_source The original update source.  | 
 | 77 | + * @return string  | 
 | 78 | + */  | 
 | 79 | +function acf_direct_update_source( $update_source ) {  | 
 | 80 | +	return __( 'ACF Direct', 'acf' );  | 
 | 81 | +}  | 
 | 82 | +add_filter( 'acf/site_health/update_source', 'acf_direct_update_source' );  | 
 | 83 | + | 
 | 84 | +/**  | 
 | 85 | + * Unsets ACF from reporting back to the WP.org API.  | 
 | 86 | + *  | 
 | 87 | + * @param array  $args An array of HTTP request arguments.  | 
 | 88 | + * @param string $url  The request URL.  | 
 | 89 | + * @return array|mixed  | 
 | 90 | + */  | 
 | 91 | +function acf_unset_plugin_from_org_reporting( $args, $url ) {  | 
 | 92 | +	// Bail if not a plugins request.  | 
 | 93 | +	if ( empty( $args['body']['plugins'] ) ) {  | 
 | 94 | +		return $args;  | 
 | 95 | +	}  | 
 | 96 | + | 
 | 97 | +	// Bail if not a request to the wp.org API.  | 
 | 98 | +	$parsed_url = wp_parse_url( $url );  | 
 | 99 | +	if ( empty( $parsed_url['host'] ) || 'api.wordpress.org' !== $parsed_url['host'] ) {  | 
 | 100 | +		return $args;  | 
 | 101 | +	}  | 
 | 102 | + | 
 | 103 | +	$plugins = json_decode( $args['body']['plugins'], true );  | 
 | 104 | +	if ( empty( $plugins ) ) {  | 
 | 105 | +		return $args;  | 
 | 106 | +	}  | 
 | 107 | + | 
 | 108 | +	// Remove ACF from reporting.  | 
 | 109 | +	if ( ! empty( $plugins['plugins'][ ACF_BASENAME ] ) ) {  | 
 | 110 | +		unset( $plugins['plugins'][ ACF_BASENAME ] );  | 
 | 111 | +	}  | 
 | 112 | + | 
 | 113 | +	if ( ! empty( $plugins['active'] ) && is_array( $plugins['active'] ) ) {  | 
 | 114 | +		$is_active = array_search( ACF_BASENAME, $plugins['active'], true );  | 
 | 115 | +		if ( $is_active !== false ) {  | 
 | 116 | +			unset( $plugins['active'][ $is_active ] );  | 
 | 117 | +			$plugins['active'] = array_values( $plugins['active'] );  | 
 | 118 | +		}  | 
 | 119 | +	}  | 
 | 120 | + | 
 | 121 | +	// Add the plugins list (minus ACF) back to $args.  | 
 | 122 | +	$args['body']['plugins'] = wp_json_encode( $plugins );  | 
 | 123 | + | 
 | 124 | +	return $args;  | 
 | 125 | +}  | 
 | 126 | +add_filter( 'http_request_args', 'acf_unset_plugin_from_org_reporting', 10, 2 );  | 
0 commit comments