PHP code example of dimadin / commonwp

1. Go to this page and download the library: Download dimadin/commonwp library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dimadin / commonwp example snippets


add_action( 'wp_enqueue_scripts', function() {
	// Use the .min version if SCRIPT_DEBUG is turned off.
	$min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';

	wp_enqueue_script( 'popper.js', get_template_directory_uri() . "/assets/js/popper{$min}.js", [], '1.14.0', true );
	wp_enqueue_script( 'bootstrap', get_template_directory_uri() . "/assets/js/bootstrap{$min}.js", [ 'jquery', 'popper.js' ], '4.1.0', true );

	wp_enqueue_style( 'bootstrap', get_template_directory_uri() . "/assets/css/bootstrap{$min}.css", [], '4.1.0' );
} );

add_filter( 'npm_packages_scripts', function( $scripts ) {
	// For file: https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js
	$scripts['bootstrap'] = [ // This is the handle used when script is registered in WordPress.
		'package'  => 'bootstrap',  // Slug of npm package.
		'file'     => 'dist/js/bootstrap', // Path to file, excluding extension (it is always either .js or .css).
		'minified' => '.min', // If file has both minified and unminified version, and if simple suffix can be added to the base.
	];

	// For file: https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js
	$scripts['popper.js'] = [
		'package'  => 'popper.js',
		'file'     => 'dist/umd/popper',
		'minified' => '.min',
	]

	return $scripts;
} );

add_filter( 'npm_packages_styles', function( $styles ) {
	// For file: https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css
	$styles['bootstrap'] = [
		'package'  => 'bootstrap',
		'file'     => 'dist/css/bootstrap',
		'minified' => '.min',
	];

	return $styles;
} );

add_filter( 'commonwp_store_callback', function( $function, $base ) {
	if ( 'update' === $base ) {
		$base = 'set';
	}

	return $base . '_site_transient';
}, 10, 2 );

add_filter( 'commonwp_should_rewrite', function( $rewrite, $relative_path, $src, $handle, $type ) {
	if ( false !== strpos( $src, '/wp-content/plugins/my-custom-plugin' ) ) {
		return false;
	}

	return $rewrite;
}, 10, 5 );

add_filter( 'commonwp_get_src', function( $pre, $relative_path, $src, $handle, $type ) {
	return \Some\Custom\Function( $relative_path );
}, 10, 5 );

add_filter( 'commonwp_process_core_with_major_update', '__return_false' );
add_filter( 'commonwp_process_core_with_minor_update', '__return_false' );
add_filter( 'commonwp_process_plugin_with_update', '__return_false' );
add_filter( 'commonwp_process_theme_with_update', '__return_false' );

add_filter( 'commonwp_npm_compare_with_local', function( $compare, $process ) {
	if ( ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) && 'script' == $process->type ) {
		switch ( $process->handle ) {
			case 'jquery-core' :
			case 'underscore':
			case 'backbone':
				$compare = false;

				break;
		}
	}

	return $compare;
}, 10, 2 );

add_action( 'plugins_loaded', function() {
	remove_filter( 'script_loader_tag', [ 'dimadin\WP\Plugin\commonWP\SRI', 'script' ], 567, 3 );
	remove_filter( 'style_loader_tag',  [ 'dimadin\WP\Plugin\commonWP\SRI', 'style'  ], 567, 3 );
}, 15 );

add_filter( 'commonwp_add_subresource_integrity', function( $enable, $process ) {
	if ( 'jquery-core' == $process->handle ) {
		return false;
	}

	return $enable;
}, 10, 2 );