<?php

/*
Plugin Name:  Builder Tiles
Plugin URI:   https://themify.me/addons/tiles
Version:      3.5.2 
Author:       Themify
Author URI:   https://themify.me
Description:  A Builder addon to make flippable Tiles like Windows 8 Metro layouts. It requires to use with the latest version of any Themify theme or the Themify Builder plugin.
Text Domain:  builder-tiles
Domain Path:  /languages
Requires PHP: 7.2
Compatibility: 7.0.0
*/

defined('ABSPATH') or die('-1');

if (!class_exists('Builder_Tiles',false)) :
	
class Builder_Tiles {

	public static $url;

    /**
     * Init Builder Tiles
     */
    public static function init() {
		add_action( 'init', array(__CLASS__, 'i18n') );
		add_action( 'themify_builder_setup_modules', array(__CLASS__, 'register_module') );
		if(did_action('plugins_loaded')){
		    self::constants();
		}
		else{
		    add_action( 'plugins_loaded', array( __CLASS__, 'constants' ) );
		}
		if( is_admin() ) {
			add_filter( 'plugin_row_meta', array( __CLASS__, 'themify_plugin_meta'), 10, 2 );
			add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), array( __CLASS__, 'action_links') );
		} 
	}

	public static function get_version():string{
		return '3.5.2';
	}

	public static function constants() {
	    self::$url = defined('BUILDER_TILES_URL') ? BUILDER_TILES_URL : trailingslashit(plugin_dir_url(__FILE__));
	}

	public static function themify_plugin_meta(array $links, $file ):array {
		if ( plugin_basename( __FILE__ ) === $file ) {
			$row_meta = array(
			  'changelogs'    => '<a href="' . esc_url( 'https://themify.org/changelogs/' ) . basename( dirname( $file ) ) .'.txt" target="_blank" aria-label="' . esc_attr__( 'Plugin Changelogs', 'themify' ) . '">' . esc_html__( 'View Changelogs', 'themify' ) . '</a>'
			);

			return array_merge( $links, $row_meta );
		}
		return (array) $links;
	}
	public static function action_links(array $links ):array {
		if ( is_plugin_active( 'themify-updater/themify-updater.php' ) ) {
			$tlinks = array(
			 '<a href="' . admin_url( 'index.php?page=themify-license' ) . '">'.__('Themify License', 'themify') .'</a>',
			 );
		} else {
			$tlinks = array(
			 '<a href="' . esc_url('https://themify.me/docs/themify-updater-documentation') . '">'. __('Themify Updater', 'themify') .'</a>',
			 );
		}
		return array_merge( $links, $tlinks );
	}
	public static function i18n() {
		load_plugin_textdomain( 'builder-tiles', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
	}

	public static function register_module() {
		$dir = defined('BUILDER_TILES_DIR') ? BUILDER_TILES_DIR : trailingslashit(plugin_dir_path(__FILE__));
		if ( is_admin() ) {
                    include( $dir . 'includes/admin.php' );
		} else{
			add_action( 'wp_head', array( __CLASS__, 'tiles_gap' ) );
		}

		if ( method_exists('Themify_Builder_Model', 'add_module')){
			Themify_Builder_Model::add_module($dir . 'modules/module-tile.php' );
		} else {
			Themify_Builder_Model::register_directory('templates', $dir . 'templates');
			Themify_Builder_Model::register_directory('modules', $dir . 'modules');
		}
	}

	public static function get_tile_sizes():array {
		$url=self::$url.'assets/sizes/';
		return apply_filters('builder_tiles_sizes', array(
			'square-large' => array( 'width' => 480, 'height' => 480, 'mobile_width' => 280, 'mobile_height' => 280, 'image' => $url . 'size-sl.svg'),
			'square-small' => array('width' => 240, 'height' => 240, 'mobile_width' => 140, 'mobile_height' => 140, 'image' => $url. 'size-ss.svg'),
			'landscape' => array('width' => 480, 'height' => 240, 'mobile_width' => 280, 'mobile_height' => 140, 'image' => $url . 'size-l.svg'),
			'portrait' => array('width' => 240, 'height' => 480, 'mobile_width' => 140, 'mobile_height' => 280, 'image' => $url . '/size-p.svg'),
		));
	}

	public static function get_option(string $name ) {
		$options = get_option( 'builder_tiles', array()) + array('fluid_tiles' => 1,'gutter' => 0);
		$value = isset( $options[ $name ] ) ? $options[ $name ] : '';
		if ( is_page() ) {
			$id = get_the_ID();
			if ( empty( $id ) ) {
				$id = Themify_Builder::$builder_active_id;
			}
			$single_value = get_post_meta( $id, "builder_tiles_{$name}", true );

			if ( $single_value !== '' ) {
				$value = $single_value;
			}
		}

		return $value;
	}

	public static function tiles_gap():void {
		?>
		<style>:root{--builder-tiles-gutter:<?php echo (int) self::get_option( 'gutter' ); ?>px}</style>
		<?php
	}

}
	Builder_Tiles::init();
endif;
