Automattic\WooCommerce\Admin\Notes

DataStore::save_actions()privateWC 1.0

Save actions to the database. This function clears old actions, then re-inserts new if any changes are found.

Method of the class: DataStore{}

No Hooks.

Return

true|false|null.

Usage

// private - for code of main (parent) class only
$result = $this->save_actions( $note );
$note(Note) (required) (passed by reference — &)
Note object.

DataStore::save_actions() code WC 8.6.1

private function save_actions( &$note ) {
	global $wpdb;

	$changed_props = array_keys( $note->get_changes() );

	if ( ! in_array( 'actions', $changed_props, true ) ) {
		return false;
	}

	// Process action removal. Actions are removed from
	// the note if they aren't part of the changeset.
	// See Note::add_action().
	$changed_actions = $note->get_actions( 'edit' );
	$actions_to_keep = array();

	foreach ( $changed_actions as $action ) {
		if ( ! empty( $action->id ) ) {
			$actions_to_keep[] = (int) $action->id;
		}
	}

	$clear_actions_query = $wpdb->prepare(
		"DELETE FROM {$wpdb->prefix}wc_admin_note_actions WHERE note_id = %d",
		$note->get_id()
	);

	if ( $actions_to_keep ) {
		$clear_actions_query .= sprintf( ' AND action_id NOT IN (%s)', implode( ',', $actions_to_keep ) );
	}

	$wpdb->query( $clear_actions_query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared

	// Update/insert the actions in this changeset.
	foreach ( $changed_actions as $action ) {
		$action_data = array(
			'note_id'       => $note->get_id(),
			'name'          => $action->name,
			'label'         => $action->label,
			'query'         => $action->query,
			'status'        => $action->status,
			'actioned_text' => $action->actioned_text,
			'nonce_action'  => $action->nonce_action,
			'nonce_name'    => $action->nonce_name,
		);

		$data_format = array(
			'%d',
			'%s',
			'%s',
			'%s',
			'%s',
			'%s',
			'%s',
			'%s',
		);

		if ( ! empty( $action->id ) ) {
			$action_data['action_id'] = $action->id;
			$data_format[]            = '%d';
		}

		$wpdb->replace(
			$wpdb->prefix . 'wc_admin_note_actions',
			$action_data,
			$data_format
		);
	}

	// Update actions from DB (to grab new IDs).
	$this->read_actions( $note );
}