php WP-CLI-子-n8f-toptal.php

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php WP-CLI-子-n8f-toptal.php相关的知识,希望对你有一定的参考价值。

<?php
/**
 * Plugin Name: TOPTAL WP-CLI Commands
 * Version: 0.1
 * Plugin URI: https://n8finch.com/
 * Description: Some rando wp-cli commands to make life easier...
 * Author: Nate Finch
 * Author URI: https://n8finch.com/
 * Text Domain: toptal-wpcli
 * Domain Path: /languages/
 * License: GPL v3
 */

/**
 * NOTE: THIS PLUGIN FILE WILL NOT WORK IN PRODUCTION AS IS AND IS ONLY FOR DEMONSTRATION PURPOSES!
 * You can of course take the code and repurpose it:-).
 */

if ( !defined( 'WP_CLI' ) && WP_CLI ) {
    //Then we don't want to load the plugin
    return;
}


/**
* Implements TOPTAL WP-CLI commands.
*/

class TOPTAL_WP_CLI_COMMANDS extends WP_CLI_Command {

	/**
	 * Remove a membership level from a user
	 *
	 * ## OPTIONS
	 * --level=<number>
	 * : Membership level to check for and remove
	 *
	 * --email=<email>
	 * : Email of user to check against
	 *
	 * [--dry-run]
	 * : Run the entire search/replace operation and show report, but don't save changes to the database.
	 *
	 * ## EXAMPLES
	 *
	 * wp toptal remove_user --level=5 --email=me@test.com,another@email.com, and@another.com --dry-run
	 *
	 * @synopsis --level=<number> --email=<email> [--dry-run]
	 *
	 * @when after_wp_load
	 */
	public function remove_user ( $args, $assoc_args ) {

		// Keep a tally of warnings and loops
		$total_warnings = 0;
		$total_users_removed = 0;
        
        // If it's a dry run, add this to the end of the success message
		$dry_suffix = '';
        
        // Keep a list of emails for users we may want to double check
		$emails_not_existing = array();
		$emails_without_level = array();

		//Get the args
		$dry_run = $assoc_args['dry-run'];
		$level = $assoc_args['level'];
		$emails = explode( ',', $assoc_args['email'] );

		// Loop through emails
		foreach ( $emails as $email ) {

			//Get User ID
			$user_id = email_exists($email);

			if( !$user_id ) {

				WP_CLI::warning( "The user {$email} does not seem to exist." );

				array_push( $emails_not_existing, $email );

				$total_warnings++;

				continue;
			}

			// Check membership level. This is a made up function, but you could write one or your membership plugin probably has one.
			$has_level = function_to_check_membership_level( $level, $user_id );
			if ( $has_level ) {

				if ( !$dry_run ) {
                    			// Deactivate membership level. This is a made up function, but you could write one or your membership plugin probably has one.
					function_to_deactivate_membership_level( $level, $user_id, 'inactive' );
				}

				WP_CLI::success( "Membership canceled for {$email}, Level {$level} removed" . PHP_EOL );

				$total_users_removed++;

			} else {

				WP_CLI::warning( "The user {$email} does not have Level = {$level} membership." );

				array_push( $emails_without_level, $email );

				$total_warnings++;
			}

			//We could echo something here to show that things are processing...

		} //end foreach

		if ( $dry_run ) {

			$dry_suffix = 'BUT, nothing really changed because this was a dry run:-).';

		}

		WP_CLI::success( "{$total_users_removed} User/s been removed, with {$total_warnings} warnings. {$dry_suffix}" );


		if ( $total_warnings ) {

			$emails_not_existing = implode(',', $emails_not_existing);
			$emails_without_level = implode(',', $emails_without_level);

			WP_CLI::warning(

				"These are the emails to double check and make sure things are on the up and up:" . PHP_EOL .
				"Non-existent emails: " . $emails_not_existing . PHP_EOL .
				"Emails without the associated level: " . $emails_without_level . PHP_EOL
			);

		}
	}
}

WP_CLI::add_command( 'toptal', 'TOPTAL_WP_CLI_COMMANDS' );

以上是关于php WP-CLI-子-n8f-toptal.php的主要内容,如果未能解决你的问题,请参考以下文章

php 创建WP-CLI命令,传递1个参数

php WP-CLI脚本,用于将多站点实例从一个域移动到另一个域

php [CoursePress] - 提供一组可以创建课程和单元的功能。可以使用eval从wp-cli调用这些函数

apache_conf 我的`〜/ .wp-cli / config.yml`文件用于将WP-CLI和WP-CLI Valet命令弯曲到我的意愿

text wp-cli #memo

sh WP-CLI