diff --git a/causality-admin.js b/causality-admin.js new file mode 100644 index 0000000..789b50f --- /dev/null +++ b/causality-admin.js @@ -0,0 +1,16 @@ +var causalityAddOverlayField = function(e) { + jQuery("#causality_overlay_fields").append("
TitleURL

"); + e.preventDefault(); +} + +jQuery("#causality_settings_form").submit(function(event) { + overlays = {}; + jQuery.each(jQuery(".causality_overlay_field"), function(i, f) { + title = jQuery(this).find(".causality_overlay_title").val(); + url = jQuery(this).find(".causality_overlay_url").val(); + if (title && url) { + overlays[title] = url; + } + }); + jQuery("#causality_overlays").val(JSON.stringify(overlays)); +}); \ No newline at end of file diff --git a/causality.css b/causality.css index abbc5f1..6e65b11 100644 --- a/causality.css +++ b/causality.css @@ -5,4 +5,7 @@ .jcrop-holder div div .jcrop-tracker { background-size: cover; } + +#causality_upload, .causality-button, #causality_output, #causality_jcrop_container { + display: none; } \ No newline at end of file diff --git a/causality.js b/causality.js index 6a52881..4afe6c7 100644 --- a/causality.js +++ b/causality.js @@ -1,45 +1,87 @@ -var jcrop_api; +var jcrop_api, causalityStyle; + +var causalityActivateUpload = function() { + jQuery("#causality_activate_upload, #causality_facebook_login").fadeOut(function() {jQuery("#causality_upload").fadeIn();}); +} + +var causalityInitFacebookSDK = function() { + jQuery.ajaxSetup({ cache: true }); + jQuery.getScript('//connect.facebook.net/en_US/sdk.js', function() { + FB.init({ + appId: causality_fb_app_id, + version: 'v2.7' + }); + }); +} + +var causalityFacebookLogin = function() { + FB.getLoginStatus(function(response) { + if (response.status === 'connected') { + causalityGetFacebookPhoto(); + } else { + FB.login(function() { + causalityGetFacebookPhoto(); + }); + } + }); +} + +var causalityGetFacebookPhoto = function() { + FB.api('/me/picture?width=960&height=960', function(response) { + if (response && !response.error) { + casualityInitCropInterface(response.data.url); + } + }); +} var casualityUploadHandler = function() { - input = this; + var input = this; if (input.files && input.files[0]) { + causalityReadFile(input.files[0]); casualityInitCropInterface(input.files[0]); } }; -var casualityInitCropInterface = function(file) { +var causalityReadFile = function(file) { var reader = new FileReader(); - reader.onload = function (e) { - var read = e.target.result; - profileImage = new Image(); - profileImage.id = 'causality_jcrop_img'; - profileImage.onload = function() { - jQuery("#causality_jcrop").append(profileImage); - console.log(jQuery(".entry-content").width()); - jQuery("#causality_jcrop").Jcrop({aspectRatio: 1, trueSize: [profileImage.naturalWidth, profileImage.naturalHeight]}, function() { - jcrop_api = this; - }); - var dim = jcrop_api.getBounds(); - var x = 0, y = 0, x_ = dim[0], y_ = dim[1]; + reader.onload = function(e) { + casualityInitCropInterface(e.target.result); + } + reader.readAsDataURL(file); +}; + +var casualityInitCropInterface = function(imagesrc) { + jQuery("#causality_facebook_login, #causality_activate_upload, #causality_upload").fadeOut(function(){jQuery("#causality_jcrop_container").fadeIn()});; + jQuery("#causality_jcrop_container").html('
'); + profileImage = new Image(); + profileImage.crossOrigin = "Anonymous"; + profileImage.id = 'causality_jcrop_img'; + profileImage.onload = function() { + jQuery("#causality_jcrop").html(profileImage); + jQuery(profileImage).width(jQuery(profileImage).width()); //Set the width to an explicit px based on current width (which should be max-width: 100%). This is required because jCrop computes the max-width wrong (ignores padding). + jQuery("#causality_jcrop").Jcrop({aspectRatio: 1, trueSize: [profileImage.naturalWidth, profileImage.naturalHeight]}, function() { + jcrop_api = this; + }); + var dim = jcrop_api.getBounds(); + var x = 0, y = 0, x_ = dim[0], y_ = dim[1]; - var x_r = (x_ / 1) - y_; - var y_r = (y_ / 1) - x_; + var x_r = (x_ / 1) - y_; + var y_r = (y_ / 1) - x_; - if (x_r > 0) { - x = x_r / 2; - } - if (y_r > 0) { - y = y_r / 2; - } + if (x_r > 0) { + x = x_r / 2; + } + if (y_r > 0) { + y = y_r / 2; + } - jcrop_api.setSelect([x, y, dim[0], dim[1]]); - }; - profileImage.src = read; + jcrop_api.setSelect([x, y, dim[0], dim[1]]); + jQuery("#causality_crop").fadeIn(); }; - reader.readAsDataURL(file); + profileImage.src = imagesrc; }; -var casualityCrop = function() { +var causalityCrop = function() { var selection = jcrop_api.tellSelect(); console.log(selection); causalityRenderProfile(selection.x, selection.y, selection.w, selection.h, selection.x2, selection.y2); @@ -56,10 +98,32 @@ var causalityRenderProfile = function(x, y, w, h, x2, y2) { ctx.drawImage(profileImage, x, y, w, h, 0, 0, 960, 960); ctx.drawImage(overlayImage, 0, 0, 960, 960); data = canvas.toDataURL(); - jQuery("#causality_output").html(''); + jQuery("#causality_output").html(''); + jQuery("#causality_jcrop_container, #causality_crop").fadeOut(function() {jQuery("#causality_output, #causality_overlay").fadeIn();});; }; }; +var causalitySetOverlay = function(url) { + causality_overlay = url; + if (!causalityStyle) { + causalityStyle = document.createElement('style'); + causalityStyle.type = 'text/css'; + jQuery("head").append(causalityStyle); + } + jQuery(causalityStyle).html(".jcrop-holder div div .jcrop-tracker{background: url('" + causality_overlay + "');"); + if (jQuery("#causality_output").html()) { + causalityCrop(); + } else { + jQuery("#causality_overlay").fadeOut(function(){jQuery("#causality_facebook_login, #causality_activate_upload").fadeIn();}); + } +} + jQuery('#causality_upload').change(casualityUploadHandler); -jQuery("#causality_crop_button").click(casualityCrop); +jQuery("#causality_crop").click(causalityCrop); +jQuery("#causality_overlay").change(function() { + if (this.value) { + causalitySetOverlay(this.value); + } +}); +causalityInitFacebookSDK(); diff --git a/causality.php b/causality.php index 8000e7e..e6e1683 100644 --- a/causality.php +++ b/causality.php @@ -39,9 +39,9 @@ function causality_settings_init() { ); add_settings_field( - 'causality_image_url', - 'Campaign image URL', - 'causality_settings_image_url_render', + 'causality_fb_app_id', + 'Facebook App ID', + 'causality_settings_fb_app_id_render', 'causality_settings', 'causality_settings_section' ); @@ -51,7 +51,11 @@ function causality_settings_init() { ); register_setting( - 'causality', 'causality_image_url' + 'causality', 'causality_overlays' + ); + + register_setting( + 'causality', 'causality_fb_app_id' ); } @@ -64,21 +68,38 @@ function causality_settings_title_render() { echo ""; // XSS ok. } -function causality_settings_image_url_render() { - $setting = esc_attr( get_option( 'causality_image_url' ) ); - echo ""; // XSS ok. +function causality_settings_overlays_render() { + $setting = get_option( 'causality_overlays' ); + echo ""; // XSS ok. + echo '
'; + if ( $overlays = json_decode( $setting, true ) ) { + foreach ( $overlays as $title => $url ) { + echo "
TitleURL

"; + } + } else { + echo "
TitleURL

"; + } + echo '
'; + echo ''; +} + +function causality_settings_fb_app_id_render() { + $setting = esc_attr( get_option( 'causality_fb_app_id' ) ); + echo ""; // XSS ok. } /** * Settings page */ function causality_settings_page() { + wp_enqueue_script( 'causality-admin', plugins_url( 'causality-admin.js', __FILE__ ), [ 'jquery' ] ); ?>

Causality settings

+
@@ -106,13 +127,27 @@ function causality_shortcode( $attributes ) { * Render Causality */ function causality_render() { + $overlays = json_decode( get_option( 'causality_overlays' ), true ); + if ( ! ( $overlays ) ) { + return '

ERROR: No overlays configured

'; + } $html = ''; - $html .= ''; - $html .= ""; + $html .= ''; $html .= '

' . esc_attr( get_option( 'causality_title' ) ) . '

'; - $html .= ''; - $html .= '
'; - $html .= ''; + $html .= '
'; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= '
'; + $html .= ''; $html .= ''; $html .= '
'; return $html;