<?php
/**
 * Plugin Name:     Tasty Roundups
 * Plugin URI:      https://www.wptasty.com/tasty-roundups
 * Description:     Publish search- and user-friendly lists of posts.
 * Author:          WP Tasty
 * Author URI:      https://www.wptasty.com
 * Text Domain:     tasty-roundups
 * Domain Path:     /languages
 * Version: 2.1.1
 *
 * @package         Tasty_Roundups
 */

define( 'TASTY_ROUNDUPS_PLUGIN_VERSION', '2.1.1' );
define( 'TASTY_ROUNDUPS_PLUGIN_FILE', __FILE__ );
define( 'TASTY_ROUNDUPS_SHAREASALE_PRODUCT_ID', '2000830' );
define( 'TASTY_ROUNDUPS_PATH', plugin_dir_path( __FILE__ ) );


// Admin.
add_action( 'admin_init', array( 'Tasty_Roundups\Admin', 'action_admin_init' ) );
add_filter( 'http_request_args', array( 'Tasty_Roundups\Admin', 'filter_http_request_args' ), 10, 2 );
add_action( 'admin_menu', array( 'Tasty_Roundups\Admin', 'action_admin_menu' ) );
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( 'Tasty_Roundups\Admin', 'filter_plugin_action_links' ) );
add_action( 'update_option_tasty_roundup_license_key', array( 'Tasty_Roundups\Admin', 'action_update_option_clear_transient' ) );
add_filter( 'rest_post_search_query', array( 'Tasty_Roundups\Block_Editor', 'prepare_search_args' ), 10, 2 );
add_filter( 'tasty_framework_admin_menu_items', array( 'Tasty_Roundups\Admin', 'add_menu_item' ) );
add_filter( 'post_search_columns', array( 'Tasty_Roundups\Block_Editor', 'search_only_in_titles' ) ); // Works with WP v6.2.0+.
add_filter( 'tasty_framework_admin_header_title', array( 'Tasty_Roundups\Admin', 'update_admin_dashboard_title' ) );

add_action(
	'admin_enqueue_scripts',
	array(
		'Tasty_Roundups\Admin',
		'action_admin_enqueue_scripts',
	)
);

add_action(
	'init',
	array(
		'Tasty_Roundups\Block_Editor',
		'action_init_register',
	)
);

add_action(
	'rest_api_init',
	array(
		'Tasty_Roundups\Block_Editor',
		'action_rest_api_init',
	)
);

add_action(
	'enqueue_block_assets',
	array(
		'Tasty_Roundups\Block_Editor',
		'action_enqueue_block_assets',
	)
);


add_action(
	'enqueue_block_editor_assets',
	array(
		'Tasty_Roundups\Block_Editor',
		'action_enqueue_block_editor_assets',
	)
);

add_action(
	'wp_head',
	array(
		'Tasty_Roundups\Distribution_Metadata',
		'action_wp_head_google_schema',
	)
);

add_filter(
	'wpseo_schema_graph_pieces',
	array(
		'Tasty_Roundups\Distribution_Metadata',
		'filter_wpseo_schema_graph_pieces',
	),
	10,
	2
);

/**
 * Get a fully-qualified path to a template.
 *
 * @param string $template Template name.
 * @return string
 */
function tasty_roundups_get_template_path( $template ) {
	$full_path = __DIR__ . '/templates/' . $template . '.php';
	return apply_filters( 'tasty_roundups_template_path', $full_path, $template );
}

/**
 * Get a rendered template.
 *
 * @param string $template Fully-qualified template path.
 * @param array  $vars     Variables to pass into the template.
 * @return string
 */
function tasty_roundups_get_template_part( $template, $vars = array() ) {
	$full_path = tasty_roundups_get_template_path( $template );
	// Provided template may already be a full path.
	if ( ! file_exists( $full_path ) ) {
		$full_path = $template;
	}
	if ( ! file_exists( $full_path ) ) {
		return '';
	}

	ob_start();
	if ( ! empty( $vars ) ) {
		// phpcs:ignore WordPress.PHP.DontExtract.extract_extract
		extract( $vars );
	}
	include $full_path;
	return ob_get_clean();
}

// Load Tasty framework.
require_once implode(
	DIRECTORY_SEPARATOR,
	array(
		dirname( TASTY_ROUNDUPS_PLUGIN_FILE ),
		'inc',
		'libs',
		'tasty-framework',
		'tasty-framework.php',
	)
);

/**
 * Register the class autoloader
 */
spl_autoload_register(
	function ( $class_name ) {
			$class_name = ltrim( $class_name, '\\' );
		if ( 0 !== stripos( $class_name, 'Tasty_Roundups\\' ) ) {
			return;
		}

			$parts = explode( '\\', $class_name );
			array_shift( $parts ); // Don't need "Tasty_Roundups".
			$last    = array_pop( $parts ); // File should be 'class-[...].php'.
			$last    = 'class-' . $last . '.php';
			$parts[] = $last;
			$file    = __DIR__ . '/inc/' . str_replace( '_', '-', strtolower( implode( '/', $parts ) ) );
		if ( file_exists( $file ) ) {
			require $file;
		}
	}
);
