$aphg_hot_key,
'gifBot' => $aphg_gif_bot,
'gifGpu' => $aphg_gif_gpu,
'client_secret' => $aphg_client_secret,
'client_id' => $aphg_client_id,
);
$aphg_localize_array = array(
'url' => admin_url('admin-ajax.php'),
'nonce' => $nonce,
'botText' => __('GIF bot must be latin lowercase word with length of minimum two and maximum five characters', 'aparg-handygif'),
'hotKeyText' => __('Hot key must be single latin lowercase letter', 'aparg-handygif'),
'search' => __('Search GIFs', 'aparg-handygif'),
'playOn' => __('Play On', 'aparg-handygif'),
'hover' => __('Hover', 'aparg-handygif'),
'click' => __('Click', 'aparg-handygif'),
'view' => __('View', 'aparg-handygif'),
'chooseImg' => __('Choose GIF', 'aparg-handygif'),
'wentWrong' => __('Something went wrong', 'aparg-handygif'),
'alert' => __("Sorry can't insert GIF ", 'aparg-handygif'),
'noGifsFound' => __("No Gifs found", 'aparg-handygif'),
'settings' => $settings,
'icon_url' => plugins_url('/images/logo.png', __FILE__)
);
wp_localize_script('aparg_handygif_style', 'handyGIF', $aphg_localize_array);
if ("toplevel_page_aparghandygif" != $hook) {
return;
}
wp_register_style('aphg_custom_styles', plugins_url('css/aphg-admin-settings.css', __FILE__), false, false, 'all');
wp_enqueue_style('aphg_custom_styles');
wp_register_script('aphg_custom_script', plugins_url('js/aphg-admin-settings.js', __FILE__), array('jquery'));
wp_enqueue_script('aphg_custom_script');
}
add_action('admin_enqueue_scripts', 'aphg_my_plugin_scripts');
/*
* Including scripts and styles to front page
*
*/
function aphg_load_custom_files() {
if (!is_admin()) {
wp_enqueue_script('jquery');
wp_register_style('aphg-front', plugins_url('css/aphg-front.css', __FILE__));
wp_enqueue_style('aphg-front');
wp_register_script('aparg_handyGIF_scripts', plugins_url('js/aphg-front-script.js', __FILE__), array('jquery'));
wp_enqueue_script('aparg_handyGIF_scripts');
wp_register_script('aparg_gifplay_scripts', plugins_url('js/aphg-gifplayer.js', __FILE__), array('jquery'));
wp_enqueue_script('aparg_gifplay_scripts');
}
}
add_action('wp_enqueue_scripts', 'aphg_load_custom_files');
/*
* add menue icon in tinymce editor
*/
add_action('init', 'aphg_tinymce_button');
function aphg_tinymce_button() {
add_filter("mce_external_plugins", "aphg_add_button");
add_filter('mce_buttons', 'aphg_register_button');
}
function aphg_add_button($plugin_array) {
$plugin_array['aphg'] = plugins_url('/js/aphg-tinymce-button.js', __FILE__);
return $plugin_array;
}
function aphg_register_button($buttons) {
array_push($buttons, 'handyGIF');
return $buttons;
}
/*
* ajax handler function for creating still GIF when uploading from media library
*/
add_action('wp_ajax_aphg_create_still_gif', 'aphg_create_still_gif');
function aphg_create_still_gif() {
$nonce = sanitize_text_field($_POST['nonce']);
if (!wp_verify_nonce($nonce, 'aparg-handyGIF') && !current_user_can('edit_posts'))
die();
$id = intval(sanitize_text_field($_POST['id']));
$fullsize_path = get_attached_file($id);
$still_gif_path = plugin_dir_path(__FILE__) . 'images/media-upload/' . $id . '.gif';
if (file_exists($still_gif_path)) {
$result = plugins_url('images/media-upload/' . $id . '.gif', __FILE__);
} else {
if (aphg_convertImage($fullsize_path, $still_gif_path)) {
$result = plugins_url('images/media-upload/' . $id . '.gif', __FILE__);
} else {
$result = "";
}
}
echo json_encode($result);
wp_die();
}
/*
* deleting created gif from media upload when attachment deleting
*/
add_action('delete_attachment', 'aphg_deleting_created_gif');
function aphg_deleting_created_gif($postid) {
$created_gif_path = plugin_dir_path(__FILE__) . 'images/media-upload/' . $postid . '.gif';
if (file_exists($created_gif_path)) {
unlink($created_gif_path);
}
}
/*
* visual composer support
*/
add_action('vc_before_init', 'aphg_visual_composer_custom_param');
function aphg_visual_composer_custom_param() {
/*
* registering new parametr types checking version of visual composer
*/
if (function_exists('vc_add_shortcode_param')) {
vc_add_shortcode_param('aprgGifChekbox', 'aphg_gif_chekbox_param_settings', plugins_url('/js/visual-composer/aphg-vc-radiobutton.js', __FILE__));
vc_add_shortcode_param('aprgGif', 'aphg_gif_param_settings', plugins_url('/js/visual-composer/aphg-vc.js', __FILE__));
} else {
add_shortcode_param('aprgGifChekbox', 'aphg_gif_chekbox_param_settings', plugins_url('/js/visual-composer/aphg-vc-radiobutton.js', __FILE__));
add_shortcode_param('aprgGif', 'aphg_gif_param_settings', plugins_url('/js/visual-composer/aphg-vc.js', __FILE__));
}
function aphg_gif_chekbox_param_settings($settings, $value) {
$checked_hover = ($value == "hover") ? 'checked' : "";
$checked_click = ($value == "click") ? 'checked' : "";
$checked_view = ($value == "view") ? 'checked' : "";
$return_content = '
'
. '
';
return $return_content;
}
function aphg_gif_param_settings($settings, $value) {
$content = "";
if ($value) {
$value_array = explode('{APARG}', $value);
$gif_url = $value_array[0];
$content = ' ';
$content.='';
}
$return_content = '
';
return $return_content;
}
}
/*
* extending visual composer settings
*/
add_action('vc_before_init', 'aphg_visual_composer');
function aphg_visual_composer() {
$img_for_editor = '
';
$content_for_editor = '
HandyGIF ' . $img_for_editor . '
';
/*
* extending visual composer vc_map
*/
vc_map(array(
"name" => __("HandyGIF", "aparg-handygif"),
"base" => "handygif",
"class" => "",
'custom_markup' => $content_for_editor,
"icon" => plugins_url('images/composer_logo.png', __FILE__),
"category" => __("Content", "aparg-handygif"),
"params" => array(
array(
"type" => "aprgGif",
"holder" => "div",
"class" => "",
"heading" => __("Choose GIFs", "aparg-handygif"),
"param_name" => "gif",
"value" => '',
"description" => __("Select GIF from media library", "aparg-handygif"),
),
array(
"type" => "aprgGifChekbox",
"heading" => __("Play On:", "aparg-handygif"),
"holder" => "div",
"param_name" => "gifaction",
"value" => '',
),
array(
'type' => 'vc_link',
'heading' => __('URL (Link)', 'aparg-handygif'),
'param_name' => 'url',
'dependency' => array(
'element' => 'link',
'value' => ''
),
'description' => __('Add custom link.', 'aparg-handygif'),
),
array(
"type" => "textfield",
"heading" => __("Gif Size", "aparg-handygif"),
"param_name" => "size",
'description' => __('Enter image size (Example: "thumbnail", "medium", "large", "full" or other sizes defined by theme). Alternatively enter size in pixels (Example: 200x100 (Width x Height)). Leave parameter empty to use "thumbnail" by default.', 'aparg-handygif'),
),
array(
"type" => "textfield",
"heading" => __("Extra Class Name", "aparg-handygif"),
"param_name" => "class",
"value" => ''
),
array(
"type" => "dropdown",
"heading" => __("Image alignment", "aparg-handygif"),
"param_name" => "alignment",
"value" => array(
__('Left', "aparg-handygif") => 'text-align:left;',
__('Right', "aparg-handygif") => 'text-align:right;',
__('Center', "aparg-handygif") => 'text-align:center;'
),
),
array(
'type' => 'css_editor',
'heading' => __('Css', 'handygif'),
'param_name' => 'css',
'group' => __('Design options', 'aparg-handygif'),
),
)
));
}
/*
* creating shortcode for visual composer
*/
function aphg_shortcode_function($atts, $content) {
$atts = shortcode_atts(
array(
'gif' => '',
'css' => '',
'size' => '',
'class' => '',
'alignment' => '',
'gifaction' => '',
'url' => '',
), $atts, 'handygif'
);
$css = '';
extract(shortcode_atts(array(
'css' => ''
), $atts));
$css_class = apply_filters(VC_SHORTCODE_CUSTOM_CSS_FILTER_TAG, vc_shortcode_custom_css_class($css, ' '), 'handygif', $atts);
$img_value = "";
$title = "";
$url_attribute = "";
$link_target = "_self";
$sizes = '';
$height_auto = '';
$aphg_settings = get_option('aphg_settings');
$gpu_acceleration = ($aphg_settings && $aphg_settings['aphg_gif_gpu']) == 'on' ? 'aphg-gpu-acceleration' : '';
/*
* checking and returning img to shortcode
*/
if ($atts['gif']) {
/*
* checking size
*/
$width = '';
$height = '';
if ($atts['size']) {
$img_size_array = aphg_get_image_sizes();
if (array_key_exists($atts['size'], $img_size_array)) {
$width = $img_size_array[$atts['size']]['width'];
$height = $img_size_array[$atts['size']]['height'];
$sizes = 'width=' . $width . ' height=' . $height . '';
$height_auto = 'height:auto !important;width:' . $width . 'px';
} else {
preg_match("/(\d+(\d*)?)x(\d+(\d*))/", $atts['size'], $matches);
if ($matches && $matches[0] == $atts['size']) {
list($width, $height) = explode('x', $matches[0]);
$sizes = 'width=' . $width . ' height=' . $height . '';
$height_auto = 'height:auto !important;width:' . $width . 'px';
}
}
}
list($gif_url, $still_url) = explode('{APARG}', $atts['gif']);
$gifAction = $atts['gifaction'];
if ($atts['url']) {
$gif_link_array = explode('|', urldecode($atts['url']));
if ($gif_link_array[0]) {
$link_string_array = explode(':', $gif_link_array[0]);
array_shift($link_string_array);
$href = join(':', $link_string_array);
if ($gif_link_array[1]) {
$link_title_array = explode(':', $gif_link_array[1]);
array_shift($link_title_array);
$title = 'title=' . join('', $link_title_array);
}
if ($gif_link_array[2]) {
$link_target_array = explode(':', $gif_link_array[2]);
array_shift($link_target_array);
$link_target = join('', $link_target_array);
}
$url_attribute = 'onclick="window.open(' . '\'' . $href . '\'' . ',\'' . $link_target . '\' )"';
}
}
switch ($gifAction) {
case "hover":
$img_value = '
';
break;
case "click":
$img_value = '
';
break;
case "view" :
$img_value = '
';
break;
default:
$img_value = '
';
}
}
$html = '' . $img_value . '
';
return $html;
}
add_shortcode('handygif', 'aphg_shortcode_function');
/**
* *===== adding functions and required logic for creating wp native editor buttuns =====
*
* @global type $aphg_admin_labels
*/
function aphg_text_editor_hook() {
if (wp_script_is('quicktags') || wp_script_is('tinymce-templates')) {
?>
array('Content-type: application/json'),
'body' => sprintf($rpcRequest)));
$response = wp_remote_retrieve_body($request) ? json_decode(wp_remote_retrieve_body($request), true) : false;
$access_token = false;
if ($response && isset($response['access_token'])) {
$access_token = $response['access_token'];
}
$final_response = array('errorMessage' => 'No search results');
if ($access_token) {
$gif_request = wp_remote_get(
'https://api.gfycat.com/v1/gfycats/search?search_text=' . urlencode($search_text) . '&count=' . $count . '&cursor=' . $cursor, array('headers' => array('Content-type' => 'application/json', 'Authorization' => 'Bearer ' . $access_token))
);
$gif_response = wp_remote_retrieve_body($gif_request) ? json_decode(wp_remote_retrieve_body($gif_request), true) : false;
if ($gif_response) {
$final_response = $gif_response;
}
}
echo json_encode($final_response);
wp_die();
}
/*
* ajax handler function for getting GIF eith API Authorization
*/
add_action('wp_ajax_aphg_get_gifs', 'aphg_get_gifs');
/**
* *========= Hook for allowing data attributes ======
*
* @param arr $allowed
* @param str|arr $context
* @return arr
*/
function aphg_filter_allowed_html($allowed, $context) {
if (is_array($context)) {
return $allowed;
}
if ($context === 'post') {
$allowed['img']['data-gif'] = true;
$allowed['img']['data-playon'] = true;
$allowed['img']['data-played'] = true;
$allowed['img']['data-bot'] = true;
$allowed['img']['data-still'] = true;
$allowed['img']['data-link'] = true;
$allowed['img']['data-editor'] = true;
}
return $allowed;
}
add_filter('wp_kses_allowed_html', 'aphg_filter_allowed_html', 10, 2);
/**
* ===== Adding Admin Notice if Credential are empty =======
*
* @global str $pagenow
*/
function aphg_admin_notice() {
global $pagenow;
$aphg_admin_settings = get_option('aphg_settings');
if (!$aphg_admin_settings || !$aphg_admin_settings['aphg_client_id'] || !$aphg_admin_settings['aphg_client_secret']) {
if ($pagenow === "post.php" && (current_user_can('edit_plugins') || current_user_can('manage_network_plugins'))) {
?>