<?php
/**
 * Sample file, to change branding of the plugin
 *
 * @author Webcraftic <wordpress.webraftic@gmail.com>
 * @copyright (c) 18.07.2021, Webcraftic
 * @version 1.0
 */

if( class_exists('WCL_Plugin') ) {


	add_filter('wbcr/clearfy/adminbar_menu_items', function ($menu_items) {
		if( WCL_Plugin::app()->getPopulateOption('enable_whitelabel') ) {
			unset($menu_items['clearfy-support']);
			unset($menu_items['clearfy-rating']);
			unset($menu_items['clearfy-docs']);
		}

		return $menu_items;
	});

	/**
	 * Replace plugin name in admin menu
	 */
	add_filter('wbcr/factory/pages/impressive/menu_title', function ($title, $plugin_name, $page_id) {
		if( WCL_Plugin::app()->getPluginName() == $plugin_name && $page_id == 'quick_start' ) {
			if( WCL_Plugin::app()->getPopulateOption('enable_whitelabel') ) {
				$company_plugin_name = WCL_Plugin::app()->getPopulateOption('whitelabel_company_plugin_name', 'Plugin name');

				return $company_plugin_name . ' menu';
			}
		}

		return $title;
	}, 10, 3);

	add_filter('wbcr/factory/plugin_title', function ($title, $plugin_name) {
		if( WCL_Plugin::app()->getPluginName() == $plugin_name ) {
			if( WCL_Plugin::app()->getPopulateOption('enable_whitelabel') ) {
				return WCL_Plugin::app()->getPopulateOption('whitelabel_company_plugin_name', 'Plugin name');
			}
		}

		return $title;
	}, 10, 2);

	/**
	 * Replace plugin name in settins button
	 */
	add_filter('wbcr/clearfy/settings_button_title', function ($title) {
		if( WCL_Plugin::app()->getPopulateOption('enable_whitelabel') ) {
			$company_plugin_name = WCL_Plugin::app()->getPopulateOption('whitelabel_company_plugin_name', 'Plugin name');

			return $company_plugin_name . ' settings';
		}

		return $title;
	});

	/**
	 * Replace plugin name in adminbar menu
	 */
	add_filter('wbcr/clearfy/adminbar_menu_title', function ($title) {
		if( WCL_Plugin::app()->getPopulateOption('enable_whitelabel') ) {
			return WCL_Plugin::app()->getPopulateOption('whitelabel_company_plugin_name', 'Plugin name');
		}

		return $title;
	});

	/**
	 * Replace plugin name in header
	 */
	add_filter('wbcr/factory/pages/impressive/plugin_title', function ($title, $plugin_name) {
		if( WCL_Plugin::app()->getPluginName() == $plugin_name ) {
			if( WCL_Plugin::app()->getPopulateOption('enable_whitelabel') ) {
				$company_plugin_name = WCL_Plugin::app()->getPopulateOption('whitelabel_company_plugin_name', 'Plugin name');

				return $company_plugin_name . ' <span class="wbcr-clr-logo-label wbcr-clr-premium-label-logo">Business</span> ver';
			}
		}

		return $title;
	}, 10, 2);

	/**
	 * Remove logos using styles
	 */
	add_filter('admin_head', function () {
		if( WCL_Plugin::app()->getPopulateOption('enable_whitelabel') ) {
			$result = array_filter(WCL_Plugin::app()->getPluginInfoAttr('load_factory_modules'), function ($array) {
				return ($array[0] == "libs/factory/templates");
			});
			reset($result);
			$template_module_version = str_replace("_", "-", $result[key($result)][1]);
			?>
			<style>
                #wp-admin-bar-clearfy-menu .wbcr-clearfy-admin-bar-menu-icon {
                    display: none !important;
                }

                #wp-admin-bar-clearfy-menu .wbcr-clearfy-admin-bar-menu-title {
                    padding-left: 10px;
                }

                #WBCR .wbcr-clr-logo-label:before {
                    display: none !important;
                }

                #WBCR .wbcr-<?php echo esc_attr($template_module_version); ?>-impressive-page-template .wbcr-factory-page-header,
                #WBCR .wbcr-<?php echo esc_attr($template_module_version); ?>-impressive-page-template .wbcr-factory-page-header .wbcr-factory-header-logo {
                    background-color: <?php echo WCL_Plugin::app()->getPopulateOption('whitelabel_header_bgcolor', '#32373c') ?>;
                    color: <?php echo WCL_Plugin::app()->getPopulateOption('whitelabel_header_text_color', '#ccc') ?> !important;
                }

                #WBCR .wbcr-<?php echo esc_attr($template_module_version); ?>-impressive-page-template .wbcr-factory-page-header .wbcr-factory-header-title h2 {
                    color: <?php echo WCL_Plugin::app()->getPopulateOption('whitelabel_header_text_color', '#ccc') ?> !important;
                }

                #WBCR .wbcr-<?php echo esc_attr($template_module_version); ?>-impressive-page-template .wbcr-factory-page-header .wbcr-factory-button.wbcr-factory-type-save,
                #WBCR .wbcr-<?php echo esc_attr($template_module_version); ?>-impressive-page-template .wbcr-factory-page-header .wbcr-factory-button.wbcr-factory-type-save:hover {
                    background-color: <?php echo WCL_Plugin::app()->getPopulateOption('whitelabel_button1_bgcolor', '#8bc34a') ?>;
                    color: <?php echo WCL_Plugin::app()->getPopulateOption('whitelabel_button1_text_color', '#fff') ?> !important;
                }

                #WBCR .wbcr-<?php echo esc_attr($template_module_version); ?>-impressive-page-template .wbcr-factory-page-header .wbcr-factory-button.wbcr-factory-type-settings,
                #WBCR .wbcr-<?php echo esc_attr($template_module_version); ?>-impressive-page-template .wbcr-factory-page-header .wbcr-factory-button.wbcr-factory-type-settings:hover {
                    background-color: <?php echo WCL_Plugin::app()->getPopulateOption('whitelabel_button2_bgcolor', '#9e9e9e') ?>;
                    color: <?php echo WCL_Plugin::app()->getPopulateOption('whitelabel_button2_text_color', '#fff') ?> !important;
                }
			</style>
			<?php
		}
	});

	/**
	 * This hook removes our widgets to get premium.
	 */
	add_filter('wbcr/factory/pages/impressive/widgets', function ($widgets, $position) {
		if( WCL_Plugin::app()->getPopulateOption('enable_whitelabel') ) {
			return [];
		}

		return $widgets;
	}, 10, 3);

	/**
	 * You can create an arbitrary company widget, for technical support and any other data, so that users can see it on the pages of the plugin.
	 *
	 * @param array $widgets
	 * @param string $position
	 * @param Wbcr_Factory000_Plugin $plugin
	 */
	add_filter('wbcr/factory/pages/impressive/widgets', function ($widgets, $position, $plugin) {
		if( $plugin->getPluginName() == WCL_Plugin::app()->getPluginName() ) {
			if( WCL_Plugin::app()->getPopulateOption('enable_whitelabel') ) {
				if( $position == 'right' ) {
					for($i = 1; $i <= 3; $i++) {
						if( WCL_Plugin::app()->getPopulateOption('whitelabel_enable_right_sidebar_widget_' . $i) ) {
							ob_start();
							?>
							<div id="wbcr-clr-company-widget" class="wbcr-factory-sidebar-widget">
								<p>
									<strong><?php echo esc_html(WCL_Plugin::app()->getPopulateOption('whitelabel_right_sidebar_widget_' . $i . '_title')); ?></strong>
								</p>
								
								<div class="wbcr-clr-company-widget-body">
									<p><?php echo wp_kses_post((WCL_Plugin::app()->getPopulateOption('whitelabel_right_sidebar_widget_' . $i . '_description'))); ?></p>
								</div>
							</div>
							<?php
							$widgets['company_right_widget_' . $i] = ob_get_contents();

							ob_end_clean();
						}
					}
				}

				if( $position == 'bottom' ) {
					for($i = 1; $i <= 3; $i++) {
						if( WCL_Plugin::app()->getPopulateOption('whitelabel_enable_bottom_sidebar_widget_' . $i) ) {
							ob_start();
							?>
							<div id="wbcr-clr-company-widget" class="wbcr-factory-sidebar-widget">
								<p>
									<strong><?php echo esc_html(WCL_Plugin::app()->getPopulateOption('whitelabel_bottom_sidebar_widget_' . $i . '_title')); ?></strong>
								</p>
								
								<div class="wbcr-clr-company-widget-body">
									<p><?php echo wp_kses_post((WCL_Plugin::app()->getPopulateOption('whitelabel_bottom_sidebar_widget_' . $i . '_description'))); ?></p>
								</div>
							</div>
							<?php

							$widgets['company_bottom_widget_' . $i] = ob_get_contents();

							ob_end_clean();
						}
					}
				}
			}
		}

		return $widgets;
	}, 10, 3);

	/**
	 * Add a new component, for example, Yoast SEO
	 */
	/*add_filter('wbcr/clearfy/components/items_list', function ($components) {
		$components[] = array(
			'name' => 'wordpress-seo',
			'title' => __('Yoast SEO', 'clearfy'),
			'url' => 'https://wordpress.org/plugins/wordpress-seo/',
			'type' => 'wordpress',
			'base_path' => 'wordpress-seo/wp-seo.php',
			'icon' => 'https://ps.w.org/wordpress-seo/assets/icon.svg?rev=1946641',
			'description' => __('Improve your WordPress SEO: Write better content and have a fully optimized WordPress site using the Yoast SEO plugin.', 'clearfy')
		);

		return $components;
	});*/

	/**
	 * Replace plugin info on plugin page. I did not find the hooks I needed, so I think you can use the replacement trick.
	 * However, the best solution is to simply hide the plugin from the client.
	 */
	add_action('wp_loaded', function () {
		if( !is_admin() || !WCL_Helper::strContains($_SERVER['REQUEST_URI'], 'plugins.php') ) {
			return;
		}

		ob_start(function ($content) {
			$old_content = $content;
			$company_plugin_name = WCL_Plugin::app()->getPopulateOption('whitelabel_company_plugin_name', 'Plugin name');
			$company_name = WCL_Plugin::app()->getPopulateOption('whitelabel_company_name', 'Company name');
			$company_site_url = WCL_Plugin::app()->getPopulateOption('whitelabel_company_site_url', 'http://yoursite.com');

			$content = preg_replace('/(\|\s)?<a\shref=\"[^"]+plugin=clearfy[^"]+"\sclass=\"thickbox\sopen-plugin-details-modal\"[^<]+<\/a>/mi', '', $content);
			$content = preg_replace('/<a\shref=\"[^"]+\">\s?(Webcraftic|Creative Motion)\s?<\/a>/', '<a href="' . esc_url($company_site_url) . '">' . esc_html($company_name) . '</a>', $content);
			$content = str_replace('https://wordpress.org/plugins/clearfy/', 'http://yoursite.com', $content);
			$content = str_replace('Webcraftic Clearfy', esc_html($company_plugin_name), $content);
			$content = str_replace('Clearfy', esc_html($company_plugin_name), $content);

			if( empty($content) ) {
				$content = $old_content;
			}

			return $content;
		});
	});
}