<?php

//plugins loaded
add_action( 'plugins_loaded', 'du_wl_init' );

function du_wl_init() {

	if ( defined( 'DU_API_KEY' ) ) {
		if ( ! get_option( 'du_api_key' ) ) {
			update_option( 'du_api_key', DU_API_KEY );
			update_option( 'du_api_key_active', 1 );
		}
	}

	du_get_constants();

	add_filter( 'all_plugins', 'du_all_plugins', 10, 4 );
}


function du_all_plugins( $plugins ) {
	//print_r($plugins['divi_cloud/divi_cloud.php']);

	$plugins['divi_cloud/divi_cloud.php']['Name']      = DU_NAME;
	$plugins['divi_cloud/divi_cloud.php']['Title']     = DU_NAME;
	$plugins['divi_cloud/divi_cloud.php']['Author']    = DU_AUTHOR;
	$plugins['divi_cloud/divi_cloud.php']['AuthorURI'] = DU_AUTHOR_URL;
	$plugins['divi_cloud/divi_cloud.php']['PluginURI'] = DU_URL;

	if ( DU_DESC ) {
		$plugins['divi_cloud/divi_cloud.php']['Description'] = DU_DESC;
	}

	return $plugins;
}

function du_get_constants() {
	$default_view      = 'layouts';
	$plugin_name       = 'Divi Cloud';
	$plugin_url        = 'https://layoutscloud.com';
	$plugin_author     = 'Page Builder Cloud';
	$plugin_author_url = 'https://www.pagebuildercloud.com';
	$plugin_desc       = ''; //intentionally blank

	if ( $white_label = get_option( 'du_white_labelling', array() ) ) {
		if ( isset( $white_label['plugin_name'] ) && $white_label['plugin_name'] ) {
			$plugin_name = $white_label['plugin_name'];
		}
		if ( isset( $white_label['plugin_url'] ) && $white_label['plugin_url'] ) {
			$plugin_url = $white_label['plugin_url'];
		}
		if ( isset( $white_label['plugin_author'] ) && $white_label['plugin_author'] ) {
			$plugin_author = $white_label['plugin_author'];
		}
		if ( isset( $white_label['plugin_author_url'] ) && $white_label['plugin_author_url'] ) {
			$plugin_author_url = $white_label['plugin_author_url'];
		}
		if ( isset( $white_label['plugin_desc'] ) && $white_label['plugin_desc'] ) {
			$plugin_desc = $white_label['plugin_desc'];
		}
	}

	if ( $du_settings = get_option( 'du_settings', array() ) ) {
		if ( isset( $du_settings['default_view'] ) && $du_settings['default_view'] ) {
			$default_view = $du_settings['default_view'];
		}
	}

	if ( ! du_has_permission( 'du_layouts' ) ) {
		$default_view = 'my-cloud';
	}

	if ( ! defined( 'DU_NAME' ) ) {
		define( 'DU_NAME', $plugin_name );
	}
	if ( ! defined( 'DU_URL' ) ) {
		define( 'DU_URL', $plugin_url );
	}
	if ( ! defined( 'DU_DESC' ) ) {
		define( 'DU_DESC', $plugin_desc );
	}
	if ( ! defined( 'DU_AUTHOR' ) ) {
		define( 'DU_AUTHOR', $plugin_author );
	}
	if ( ! defined( 'DU_AUTHOR_URL' ) ) {
		define( 'DU_AUTHOR_URL', $plugin_author_url );
	}
	if ( ! defined( 'DU_DEFAULT_VIEW' ) ) {
		define( 'DU_DEFAULT_VIEW', $default_view );
	}

	//text used around the site. Maybe override using a setting later
	if ( ! defined( 'DU_UPLOAD_FROM_LIBRARY_TEXT' ) ) {
		define( 'DU_UPLOAD_FROM_LIBRARY_TEXT', apply_filters( 'du_upload_from_library_text', 'Upload to <span class="dashicons dashicons-cloud"></span>' ) );
	}
}

function du_update_permissions( $return ) {
	if ( $return['permissions'] ) {
		update_option( 'du_permissions', $return['permissions'] );
	}
}

function du_has_permission( $what_for ) {
	$return = - 1;

	if ( ! $perms = get_option( 'du_permissions' ) ) {
		if ( $key = get_option( 'du_api_key' ) ) {

			if ( $acct = du_account_info( $key ) ) {
				du_update_permissions( $acct );

				$perms = $acct['permissions'];
			}
		}
	}

	if ( $perms ) {
		if ( isset( $perms[ $what_for ] ) ) {
			$return = $perms[ $what_for ];
		}
	}

	return $return;
}

function du_get_permissions() {

	$std_permissions = array(
		'edit'         => true,
		'upload'       => true,
		'account_info' => 'full', //vs partial.. maybe add none?
		'white_label'  => true,
		'my_cloud'     => true,
		'snippets'     => true
	); //normal permissions

	return get_option( 'du_permissions', $std_permissions );
}


?>
