<?php
/*
  Plugin Name: WP Pixabay
  Plugin URI: https://wpclever.net
  Description: This plugin help you search millions of free photos, vectors and art illustrations from https://pixabay.com then insert into content or set as featured image very quickly.
  Version: 2.5
  Author: WPclever
  Author URI: https://wpclever.net/contact
 */
register_activation_hook( __FILE__, 'vpxb_activate' );
add_action( 'admin_init', 'vpxb_redirect' );
function vpxb_activate() {
	add_option( 'vpxb_do_activation_redirect', true );
}

function vpxb_redirect() {
	if ( get_option( 'vpxb_do_activation_redirect', false ) ) {
		delete_option( 'vpxb_do_activation_redirect' );
		wp_redirect( 'admin.php?page=vpxb' );
	}
}

add_action( 'admin_menu', 'vpxb_menu' );
function vpxb_menu() {
	add_menu_page( 'Pixabay', 'Pixabay', 'manage_options', 'vpxb', 'vpxb_menu_pages', 'dashicons-camera' );
}

function vpxb_menu_pages() {
	$vpxb_active_tab = isset( $_GET['tab'] ) ? $_GET['tab'] : 'settings';
	?>
	<div class="wrap vpxb_welcome">
		<h1>Welcome to WP Pixabay</h1>

		<div class="about-text">
			This plugin help you search millions of free photos, vectors and art illustrations from <a
				href="https://pixabay.com" target="_blank">https://pixabay.com</a> then insert into content
			or set
			as featured image very quickly.
		</div>

		<h2 class="nav-tab-wrapper">
			<a href="?page=vpxb&amp;tab=settings"
			   class="nav-tab <?php echo $vpxb_active_tab == 'settings' ? 'nav-tab-active' : ''; ?>"><?php _e( 'Settings' ); ?></a>
			<a href="?page=vpxb&amp;tab=api"
			   class="nav-tab <?php echo $vpxb_active_tab == 'api' ? 'nav-tab-active' : ''; ?>"><?php _e( 'How to get Pixabay Username & Key?' ); ?></a>
			<a href="?page=vpxb&amp;tab=about"
			   class="nav-tab <?php echo $vpxb_active_tab == 'about' ? 'nav-tab-active' : ''; ?>"><?php _e( 'How to use?' ); ?></a>
			<a href="?page=vpxb&amp;tab=support"
			   class="nav-tab <?php echo $vpxb_active_tab == 'support' ? 'nav-tab-active' : ''; ?>"><?php _e( 'Support' ); ?></a>
			<a href="?page=vpxb&amp;tab=buy"
			   class="nav-tab <?php echo $vpxb_active_tab == 'buy' ? 'nav-tab-active' : ''; ?>"><?php _e( 'Buy PRO version' ); ?></a>
		</h2>
		<br />
		<?php if ( $vpxb_active_tab == 'api' ) { ?>
			Step 1: Go to
			<a href="https://pixabay.com" target="_blank">https://pixabay.com</a> and create new account (verify via email required).
			<br /><br />
			Step 2: Login with your account and go to
			<a href="https://pixabay.com/api/docs/#api_key" target="_blank">https://pixabay.com/api/docs/#api_key</a>
			then you can see the key.
			<br /><br />
			<img src="<?php echo plugin_dir_url( __FILE__ ); ?>/images/pixabay_key.jpg" />

		<?php } elseif ( $vpxb_active_tab == 'about' ) { ?>
			<iframe width="560" height="315" src="https://www.youtube.com/embed/nk40Ce06D4E" frameborder="0"
			        allowfullscreen></iframe>
		<?php } elseif ( $vpxb_active_tab == 'settings' ) { ?>
			<form method="post" action="options.php" novalidate="novalidate">
				<?php wp_nonce_field( 'update-options' ) ?>
				<table class="form-table">
					<tr>
						<th scope="row"><label for="vpxb_username">Pixabay Username</label></th>
						<td>
							<input name="vpxb_username" type="text" id="vpxb_username"
							       value="<?php echo get_option( 'vpxb_username' ); ?>" />

							<p class="description"><a href="<?php echo admin_url( "admin.php?page=vpxb&tab=api" ); ?>">How
									to get Pixabay Username & Key?</a></p>
						</td>
					</tr>
					<tr>
						<th scope="row"><label for="vpxb_key">Pixabay Key</label></th>
						<td>
							<input name="vpxb_key" type="text" id="vpxb_key"
							       value="<?php echo get_option( 'vpxb_key' ); ?>" />

							<p class="description"><a href="<?php echo admin_url( "admin.php?page=vpxb&tab=api" ); ?>">How
									to get Pixabay Username & Key?</a></p>
						</td>
					</tr>
					<tr>
						<th scope="row"><label for="vpxb_frontend">Front-end editor</label></th>
						<td>
							<input name="vpxb_frontend" type="checkbox" id="vpxb_frontend"
							       value="1" <?php checked( '1', get_option( 'vpxb_frontend' ) ); ?>/>

							<p class="description">Check this option if you want use Pixabay for front-end
								editor.</p>
						</td>
					</tr>
				</table>
				<p class="submit">
					<input type="hidden" name="action" value="update" />
					<input type="hidden" name="page_options"
					       value="vpxb_username,vpxb_key,vpxb_frontend" />
					<input type="submit" name="submit" id="submit" class="button button-primary"
					       value="Save Changes" />
				</p>
			</form>
		<?php } elseif ( $vpxb_active_tab == 'support' ) { ?>
			Thank you for choosing WP Pixabay,
			<br /><strong>WPclever</strong>
			<br />Email: cleverwp@gmail.com
			<br />Website: <a href="https://wpclever.net" target="_blank">https://wpclever.net</a>
		<?php } elseif ( $vpxb_active_tab == 'buy' ) { ?>
			Buy PRO version with only $15 (Paypal) at
			<a href="https://wpclever.net/downloads/wordpress-pixabay-search-and-insert">https://wpclever.net/downloads/wordpress-pixabay-search-and-insert</a>
		<?php } ?>
	</div>
	<?php
}

function vpxb_load_scripts() {
	if ( ( get_option( 'vpxb_username' ) != '' ) && ( get_option( 'vpxb_key' ) != '' ) ) {
		$vpxb_username = get_option( 'vpxb_username' );
		$vpxb_key      = get_option( 'vpxb_key' );
	} else {
		$vpxb_username = 'dunghv';
		$vpxb_key      = '1498928-f190b376157b831824bdfb89b';
	}
	wp_enqueue_script( 'colorbox', plugin_dir_url( __FILE__ ) . 'js/jquery.colorbox.js', array( 'jquery' ) );
	wp_enqueue_style( 'colorbox', plugin_dir_url( __FILE__ ) . 'css/colorbox.css' );
	wp_enqueue_style( 'vpxb_css', plugin_dir_url( __FILE__ ) . 'css/vpxb.css' );
	wp_enqueue_script( 'vpxb_js', plugin_dir_url( __FILE__ ) . 'js/vpxb.js', array( 'jquery' ), '1.0', true );
	wp_localize_script( 'vpxb_js', 'vpxb_vars', array(
		'vpxb_username' => $vpxb_username,
		'vpxb_key'      => $vpxb_key,
		'vpxb_ajax_url' => admin_url( 'admin-ajax.php' ),
		'vpxb_nonce'    => wp_create_nonce( 'vpxb_nonce' )
	) );
}

add_action( 'admin_enqueue_scripts', 'vpxb_load_scripts' );
if ( get_option( 'vpxb_frontend', 0 ) == 1 ) {
	add_action( 'wp_enqueue_scripts', 'vpxb_load_scripts' );
}
function vpxb_add_button( $editor_id ) {
	echo ' <a href="#vpxb_popup" id="vpxb_btn" data-editor="' . $editor_id . '" class="vpxb_btn button add_media" title="Pixabay"><span class="dashicons dashicons-camera vpxb_dashicons"></span> Pixabay</a><input type="hidden" id="vpxb_featured_url" name="vpxb_featured_url" value="" /> ';
}

add_action( 'media_buttons', 'vpxb_add_button' );
function vpxb_save_postdata( $post_id, $post ) {
	if ( isset( $post->post_status ) && 'auto-draft' == $post->post_status ) {
		return;
	}
	if ( wp_is_post_revision( $post_id ) ) {
		return;
	}
	if ( ! empty( $_POST['vpxb_featured_url'] ) ) {
		if ( strstr( $_SERVER['REQUEST_URI'], 'wp-admin/post-new.php' ) || strstr( $_SERVER['REQUEST_URI'], 'wp-admin/post.php' ) ) {
			if ( 'page' == $_POST['post_type'] ) {
				if ( ! current_user_can( 'edit_page', $post_id ) ) {
					return;
				}
			} else {
				if ( ! current_user_can( 'edit_post', $post_id ) ) {
					return;
				}
			}
			$vpxb_furl = sanitize_text_field( $_POST['vpxb_featured_url'] );
			vpxb_save_featured( $vpxb_furl );
		}
	}
}

add_action( 'save_post', 'vpxb_save_postdata', 10, 3 );
function vpxb_save_to_media( $vurl, $vtitle, $vfilename, $vpid ) {
	$thumbid  = 0;
	$filename = pathinfo( $vurl, PATHINFO_FILENAME );
	if ( ( $vfilename == '1' ) && ( $vtitle != '' ) ) {
		$filename = sanitize_title( $vtitle );
	}
	require_once( ABSPATH . 'wp-admin/includes/file.php' );
	require_once( ABSPATH . 'wp-admin/includes/media.php' );
	@set_time_limit( 300 );
	if ( ! empty( $vurl ) ) {
		$tmp                    = download_url( $vurl );
		$ext                    = pathinfo( $vurl, PATHINFO_EXTENSION );
		$file_array['name']     = $filename . '.' . $ext;
		$file_array['tmp_name'] = $tmp;
		if ( is_wp_error( $tmp ) ) {
			@unlink( $file_array['tmp_name'] );
			$file_array['tmp_name'] = '';
		}
		$thumbid = media_handle_sideload( $file_array, $vpid, $desc = null );
		if ( is_wp_error( $thumbid ) ) {
			@unlink( $file_array['tmp_name'] );

			return $thumbid;
		}
	}
	echo wp_get_attachment_url( $thumbid );
}

function vpxb_save_featured( $vurl ) {
	global $post;
	$filename = pathinfo( $vurl, PATHINFO_FILENAME );
	require_once( ABSPATH . 'wp-admin/includes/file.php' );
	require_once( ABSPATH . 'wp-admin/includes/media.php' );
	@set_time_limit( 300 );
	if ( ! empty( $vurl ) ) {
		$tmp                    = download_url( $vurl );
		$ext                    = pathinfo( $vurl, PATHINFO_EXTENSION );
		$file_array['name']     = $filename . '.' . $ext;
		$file_array['tmp_name'] = $tmp;
		if ( is_wp_error( $tmp ) ) {
			@unlink( $file_array['tmp_name'] );
			$file_array['tmp_name'] = '';
		}
		$thumbid = media_handle_sideload( $file_array, $post->ID, $desc = null );
		if ( is_wp_error( $thumbid ) ) {
			@unlink( $file_array['tmp_name'] );

			return $thumbid;
		}
	}
	set_post_thumbnail( $post, $thumbid );
}

function vpxb_popup_content() {
	?>
	<div style='display:none'>
		<div id="vpxb_popup" style="width: 920px; height: 440px; position: relative; overflow: hidden">
			<table style="width: 100%; height: 100%; padding: 0; margin: 0; border-spacing: 0; vertical-align: top">
				<tr>
					<td style="width: 620px; vertical-align: top; padding: 10px">
						<select name="vpxb_language" id="vpxb_language" class="vpxb_select">
							<option value="en">Language</option>
							<option value="en">en</option>
						</select>
						<select name="vpxb_type" id="vpxb_type" class="vpxb_select">
							<option value="all">Type</option>
							<option value="all">All</option>
							<option value="photo">Photo</option>
							<option value="illustration">Illustration</option>
							<option value="vector">Vector</option>
						</select>
						<select name="vpxb_orientation" id="vpxb_orientation" class="vpxb_select">
							<option value="all">Orientation</option>
							<option value="horizontal">Horizontal</option>
							<option value="vertical">Vertical</option>
						</select>
						<select name="vpxb_editorchoice" id="vpxb_editorchoice" class="vpxb_select">
							<option value="false">EditorsChoice</option>
							<option value="true">True</option>
							<option value="false">False</option>
						</select>
						<select name="vpxb_order" id="vpxb_order" class="vpxb_select">
							<option value="popular">Order</option>
							<option value="popular">Popular</option>
							<option value="latest">Latest</option>
						</select>

						<div style="width:100%; display: inline-block; height:28px; line-height: 28px;">
							<span style="float:left;">
								<select name="vpxb_safesearch" id="vpxb_safesearch" class="vpxb_select">
									<option value="false">SafeSearch</option>
									<option value="true">True</option>
									<option value="false">False</option>
								</select>
							</span>
							<input type="text" id="vpxb_input" name="vpxb_input" value=""
							       class="vpxb_input vpxb_input-normal" placeholder="keyword" />
							<input type="button" id="vpxb_search" class="vpxb_button" value="Search" />

							<span id="vpxb_spinner" style="display:none" class="vpxb_loading">&nbsp;</span>
						</div>
						<div id="vpxb_container" class="vpxb_container">
						</div>
						<div id="vpxb_page" class="vpxb_page"></div>
					</td>
					<td style="border-left: 1px solid #ddd; background: #fcfcfc; vertical-align: top; padding: 10px">
						<div id="vpxb_use-image" class="vpxb_use-image">
							<div class="vpxb_right" style="height: 370px; overflow-y: auto; overflow-x: hidden">
								<table class="vpxb_table">
									<tr class="vpxb_tr">
										<td colspan="2" class="vpxb_td">
											<div class="vpxb_item-single" id="vpxb_view"
											     style="margin-right: 20px;"></div>
										</td>
									</tr>
									<tr class="vpxb_tr">
										<td class="vpxb_td">Title</td>
										<td class="vpxb_td"><input type="text" id="vpxb_title" value=""
										                           class="vpxb_input"
										                           placeholder="title" />
										</td>
									</tr>
									<tr>
										<td class="vpxb_td">Caption</td>
										<td class="vpxb_td"><textarea id="vpxb_caption" name="vpxb_caption"
										                              class="vpxb_textarea"></textarea>
										</td>
									</tr>
									<tr>
										<td class="vpxb_td">File name</td>
										<td class="vpxb_td">
											<select name="vpxb_filename" id="vpxb_filename" class="vpxb_select">
												<option value="0">Keep original file name</option>
												<option value="1">Generate from title</option>
											</select>
										</td>
									</tr>
									<tr>
										<td class="vpxb_td">Size</td>
										<td class="vpxb_td"><input type="number" id="vpxb_width" value="0"
										                           class="vpxb_input vpxb_input-small"
										                           placeholder="width" />
											<input
												type="number" id="vpxb_height" value="0"
												class="vpxb_input vpxb_input-small"
												placeholder="height" />
										</td>
									</tr>
									<tr>
										<td class="vpxb_td">Alignment</td>
										<td class="vpxb_td">
											<select name="vpxb_align" id="vpxb_align" class="vpxb_select">
												<option value="alignnone">None</option>
												<option value="alignleft">Left</option>
												<option value="alignright">Right</option>
												<option value="aligncenter">Center</option>
											</select>
										</td>
									</tr>
									<tr>
										<td class="vpxb_td">Link to</td>
										<td class="vpxb_td">
											<select name="vpxb_link" id="vpxb_link" class="vpxb_select">
												<option value="0">None</option>
												<option value="1">Original site</option>
												<option value="2">Original image</option>
											</select>
										</td>
									</tr>
									<tr>
										<td class="vpxb_td">&nbsp;</td>
										<td class="vpxb_td"><input name="vpxb_blank" id="vpxb_blank" type="checkbox"
										                           class="vpxb_checkbox" /> Open
											new
											windows
										</td>
									</tr>
									<tr>
										<td class="vpxb_td">&nbsp;</td>
										<td class="vpxb_td"><input name="vpxb_nofollow" id="vpxb_nofollow"
										                           type="checkbox"
										                           class="vpxb_checkbox" />
											Rel
											nofollow
										</td>
									</tr>
								</table>
							</div>
							<p class="vpxb_p" style="margin-top: 10px; width: 100%; display: inline-block">
								<input type="hidden" id="vpxb_site" value="" />
								<input type="hidden" id="vpxb_url" value="" />
								<input type="hidden" id="vpxb_eid" value="" />
								<input type="button" id="vpxb_insert" class="vpxb_button" value="Insert" />
								<a href="http://wpclever.net/downloads/wordpress-pixabay-search-and-insert"
								   target="_blank"
								   onclick="return confirm('This feature only available in PRO version!\nBuy it now?')">
									<input type="button" id="vpxb_save" class="vpxb_button_disable" value="Save&Insert" />
								</a>
								<input type="button" id="vpxb_featured" class="vpxb_button" value="Featured" />
							</p>
							<div style="display:inline-block">
						<span class="vpxb_loading-text" id="vpxb_note"
						      style="display:none">Saving image to Media Library...</span>
								<span id="vpxb_error"></span>
							</div>
						</div>
					</td>
				</tr>
			</table>
		</div>
	</div>
	<?php
}

add_action( 'admin_footer', 'vpxb_popup_content' );
if ( get_option( 'vpxb_frontend', 0 ) == 1 ) {
	add_action( 'wp_footer', 'vpxb_popup_content', 100 );
}
?>