Filter uncanny-automator

uap_option_all_lp_lessons

Filters the all lesson options for LearnPress to modify or access lesson data before it is displayed or processed.

add_filter( 'uap_option_all_lp_lessons', $callback, 10, 1 );

Description

Filters the available options for LearnPress lessons within Uncanny Automator. Developers can use this hook to add custom lesson-related tokens or modify existing ones for use in automations. This hook fires when Uncanny Automator is preparing its list of available tokens for LearnPress triggers and actions.


Usage

add_filter( 'uap_option_all_lp_lessons', 'your_function_name', 10, 1 );

Parameters

$option (mixed)
This parameter represents the lesson option that can be filtered.

Return Value

The filtered value.


Examples

<?php
/**
 * Example of filtering the 'uap_option_all_lp_lessons' hook.
 * This example demonstrates how to add extra information to the lesson options
 * available for Uncanny Automator triggers and actions.
 *
 * We'll add a new token for the lesson's instructor.
 */
add_filter(
	'uap_option_all_lp_lessons',
	function( $option ) {
		// Check if the $option array is structured as expected.
		// The hook expects an array where each key represents an option code.
		// We are looking to append to existing lesson-related options.
		if ( ! is_array( $option ) ) {
			return $option; // Return original if not an array
		}

		// Assuming the structure of $option includes keys like 'lesson_title', 'lesson_ID', 'lesson_URL'
		// and we want to add 'lesson_instructor' alongside them.
		// We'll iterate through the existing options and add our new token if it's lesson-related.
		foreach ( $option as $key => &$value ) {
			// We're looking for the 'relevant_tokens' sub-array within each lesson option.
			if ( isset( $value['relevant_tokens'] ) && is_array( $value['relevant_tokens'] ) ) {
				// Let's assume the base option code for lessons is 'lesson' or similar.
				// We'll dynamically check for the presence of 'Lesson title' to identify lesson options.
				// A more robust approach might involve checking a known pattern or structure.
				$is_lesson_option = false;
				foreach ( $value['relevant_tokens'] as $token_key => $token_label ) {
					if ( str_contains( $token_label, 'Lesson title' ) ) {
						$is_lesson_option = true;
						// Extract the base option code if possible, for dynamic token generation.
						// For example, if the key is 'lp_lesson', we want to create 'lp_lesson_instructor'.
						$base_option_code = str_replace(
							array( '_ID', '_URL' ),
							'',
							$token_key
						);
						break;
					}
				}

				if ( $is_lesson_option && ! empty( $base_option_code ) ) {
					// Add the new token for the lesson instructor.
					// We need to fetch the actual lesson object to get the instructor.
					// This requires a bit more context. For this example, we'll simulate
					// getting the instructor, assuming the lesson ID is available somehow.
					// In a real scenario, you might need to retrieve the lesson ID from
					// the context of where this option is being generated.
					// For simplicity here, we'll just add the token placeholder.
					// A more complete solution would involve fetching the lesson object.

					// Let's assume the token code would be derived from the base option code.
					$instructor_token_code = $base_option_code . '_instructor';
					$value['relevant_tokens'][ $instructor_token_code ] = esc_html__( 'Lesson instructor', 'uncanny-automator' );
				}
			}
		}

		return $option;
	},
	10,
	1
);

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/integrations/learnpress/helpers/learnpress-helpers.php:153

public function all_lp_lessons( $label = null, $option_code = 'LPLESSON', $any_option = true ) {

		if ( ! $label ) {
			$label = esc_html_x( 'Lesson', 'Learnpress', 'uncanny-automator' );
		}

		$args = array(
			'post_type'      => 'lp_lesson',
			'posts_per_page' => 9999, // phpcs:ignore WordPress.WP.PostsPerPage.posts_per_page_posts_per_page
			'orderby'        => 'title',
			'order'          => 'ASC',
			'post_status'    => 'publish',
		);

		$options = Automator()->helpers->recipe->options->wp_query( $args, $any_option, esc_html_x( 'Any lesson', 'Learnpress', 'uncanny-automator' ) );

		$option = array(
			'option_code'     => $option_code,
			'label'           => $label,
			'input_type'      => 'select',
			'required'        => true,
			// to setup example, lets define the value the child will be based on
			'current_value'   => false,
			'validation_type' => 'text',
			'options'         => $options,
			'relevant_tokens' => array(
				$option_code          => esc_html_x( 'Lesson title', 'Learnpress', 'uncanny-automator' ),
				$option_code . '_ID'  => esc_html_x( 'Lesson ID', 'Learnpress', 'uncanny-automator' ),
				$option_code . '_URL' => esc_html_x( 'Lesson URL', 'Learnpress', 'uncanny-automator' ),
			),
		);

		return apply_filters( 'uap_option_all_lp_lessons', $option );
	}

Scroll to Top