WP_CREATEPOST::define_action()

Define and register the action by pushing it into the Automator object


Source Source

File: src/integrations/wp/actions/wp-createpost.php

	public function define_action() {


		$custom_post_types = Automator()->helpers->recipe->wp->options->all_post_types( esc_attr__( 'Type', 'uncanny-automator' ), $this->action_code, [
			'token'   => false,
			'is_ajax' => false,
		] );
		// now get regular post types.
		$args = [
			'public'   => true,
			'_builtin' => true,
		];

		$output     = 'object';
		$operator   = 'and';
		$options    = array();
		$post_types = get_post_types( $args, $output, $operator );
		if ( ! empty( $post_types ) ) {
			foreach ( $post_types as $post_type ) {
				$options[ $post_type->name ] = esc_html( $post_type->labels->singular_name );
			}
		}
		$options                      = array_merge( $options, $custom_post_types['options'] );
		$custom_post_types['options'] = $options;

		// get all the post stati objects
		$post_statuses = get_post_stati( array(), 'objects' );

		// initialise options
		$status_options = array();

		// make sure there are post stati
		if ( ! empty( $post_statuses ) ) {

			// loop through each post status object
			foreach ( $post_statuses as $key => $post_status ) {

				// initialise label indicating the source of the post status
				$source_label = '';

				// guess the source using the text-domain. See: https://developer.wordpress.org/reference/functions/register_post_status/
				if ( is_array( $post_status->label_count ) ) {
					// Also see: https://developer.wordpress.org/reference/functions/_nx_noop/
					$source_label = isset( $post_status->label_count['domain'] ) ? "({$post_status->label_count['domain']})" : '';
				}

				// set the post status and its source as the option's label
				$status_options[ $key ] = $post_status->label . $source_label;
			}
		}

		// Remove status "Scheduled"
		if ( isset( $status_options['future'] ) ) {
			unset( $status_options['future'] );
		}

		// Remove status "auto-draft"
		if ( isset( $status_options['auto-draft'] ) ) {
			unset( $status_options['auto-draft'] );
		}

		// Remove status "inherit"
		if ( isset( $status_options['inherit'] ) ) {
			unset( $status_options['inherit'] );
		}

		$post_status_field = Automator()->helpers->recipe->field->select_field( 'WPCPOSTSTATUS', esc_attr__( 'Status', 'uncanny-automator' ), $status_options );

		$action = [
			'author'             => Automator()->get_author_name( $this->action_code ),
			'support_link'       => Automator()->get_author_support_link( $this->action_code, 'integration/wordpress-core/' ),
			'integration'        => self::$integration,
			'code'               => $this->action_code,
			/* translators: Action - WordPress Core */
			'sentence'           => sprintf( esc_attr__( 'Create {{a post:%1$s}}', 'uncanny-automator' ), $this->action_code ),
			/* translators: Action - WordPress Core */
			'select_option_name' => esc_attr__( 'Create {{a post}}', 'uncanny-automator' ),
			'priority'           => 11,
			'accepted_args'      => 3,
			'execution_function' => [ $this, 'create_post' ],
			'options_group'      => [
				$this->action_code => [
					$custom_post_types,
					$post_status_field,
					Automator()->helpers->recipe->field->text_field( 'WPCPOSTAUTHOR', esc_attr__( 'Author', 'uncanny-automator' ), true, 'text', '{{admin_email}}', true, esc_attr__( 'Accepts user ID, email or username', 'uncanny-automator' ) ),
					Automator()->helpers->recipe->field->text_field( 'WPCPOSTTITLE', esc_attr__( 'Title', 'uncanny-automator' ), true, 'text', '', true ),
					Automator()->helpers->recipe->field->text_field( 'WPCPOSTSLUG', esc_attr__( 'Slug', 'uncanny-automator' ), true, 'text', '', false ),
					Automator()->helpers->recipe->field->text_field( 'WPCPOSTCONTENT', esc_attr__( 'Content', 'uncanny-automator' ), true, 'textarea', '', false ),
					[
						'input_type'        => 'repeater',
						'option_code'       => 'CPMETA_PAIRS',
						'label'             => esc_attr__( 'Meta', 'uncanny-automator' ),
						'required'          => false,
						'fields'            => [
							[
								'input_type'      => 'text',
								'option_code'     => 'KEY',
								'label'           => esc_attr__( 'Key', 'uncanny-automator' ),
								'supports_tokens' => true,
								'required'        => true,
							],
							[
								'input_type'      => 'text',
								'option_code'     => 'VALUE',
								'label'           => esc_attr__( 'Value', 'uncanny-automator' ),
								'supports_tokens' => true,
								'required'        => true,
							],
						],
						'add_row_button'    => esc_attr__( 'Add pair', 'uncanny-automator' ),
						'remove_row_button' => esc_attr__( 'Remove pair', 'uncanny-automator' ),
					],
				],
			],
		];

		Automator()->register->action( $action );
	}