Advertisement
Guest User

imagemapper.php

a guest
Jan 7th, 2015
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 40.82 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: ImageMapper
  4. Plugin URI: http://wordpress.org/support/plugin/imagemapper
  5. Description: Create interactive and visual image maps with a visual editor!
  6. Version: 1.2.4.1
  7. Author: A.Sandberg AKA Spike, Tarmo Toikkanen <tarmo.toikkanen@iki.fi> with Samatva mods
  8. Author URI: http://spike.viuhka.fi
  9. License: GPL2
  10. */
  11.  
  12. define('IMAGEMAP_POST_TYPE', 'imagemap');
  13. define('IMAGEMAP_AREA_POST_TYPE', 'imagemap_area');
  14. add_action('init', 'imgmap_create_post_type');
  15. add_action('admin_menu', 'imgmap_custom_form');
  16. add_action('admin_menu', 'imgmap_imagemap_tab_menu');
  17. add_action('save_post', 'imgmap_save_meta');
  18. add_action('post_edit_form_tag', 'imgmap_add_post_enctype');
  19. add_action('template_include', 'imgmap_template');
  20. add_action('wp_ajax_imgmap_save_area', 'imgmap_save_area_ajax');
  21. add_action('wp_ajax_imgmap_delete_area', 'imgmap_delete_area_ajax');
  22. add_action('wp_ajax_nopriv_imgmap_load_dialog_post', 'imgmap_load_dialog_post_ajax');
  23. add_action('wp_ajax_imgmap_load_dialog_post', 'imgmap_load_dialog_post_ajax');
  24. add_action('wp_ajax_imgmap_get_area_coordinates', 'imgmap_get_area_coordinates_ajax');
  25. add_action('wp_ajax_imgmap_save_area_title', 'imgmap_save_area_title');
  26. add_action('wp_ajax_imgmap_set_area_color', 'imgmap_set_area_color');
  27. add_action('wp_ajax_imgmap_add_new_style', 'imgmap_add_new_style');
  28. add_action('wp_ajax_imgmap_edit_style', 'imgmap_edit_style');
  29. add_action('wp_ajax_imgmap_delete_style', 'imgmap_delete_style');
  30. add_action('before_delete_post', 'imgmap_permanently_delete_imagemap');
  31. add_action('wp_trash_post', 'imgmap_trash_imagemap');
  32. add_action('manage_'.IMAGEMAP_POST_TYPE.'_posts_custom_column', 'imgmap_manage_imagemap_columns', 10, 2);
  33. add_action('manage_'.IMAGEMAP_AREA_POST_TYPE.'_posts_custom_column', 'imgmap_manage_imagemap_area_columns', 10, 2);
  34. add_action('media_upload_imagemap', 'imgmap_media_upload_tab_action');
  35. add_action('admin_action_imgmap_save_settings', 'imgmap_save_settings');
  36.  
  37. // add_filter('the_content', 'imgmap_replace_shortcode');
  38. add_filter('post_updated_messages', 'imgmap_updated_message');
  39. add_filter('manage_edit-'.IMAGEMAP_POST_TYPE.'_columns', 'imgmap_set_imagemap_columns');
  40. add_filter('manage_edit-'.IMAGEMAP_AREA_POST_TYPE.'_columns', 'imgmap_set_imagemap_area_columns');
  41. add_filter( 'manage_edit-'.IMAGEMAP_AREA_POST_TYPE.'_sortable_columns', 'imgmap_register_sortable_area_columns' );
  42. add_filter('media_upload_tabs', 'imgmap_media_upload_tab');
  43.  
  44.  
  45. $image_maps = array();
  46.  
  47.  
  48. // Test data for highlight style management
  49. $imgmap_colors = array(
  50.     'current_id' => 12,
  51.     'last_chosen' => 1,
  52.     'colors' => array(
  53.         1 => array( 'fillColor' => 'fefefe', 'strokeColor' => 'fefefe', 'fillOpacity' => 0.3, 'strokeOpacity' => 0.8, 'strokeWidth' => 2),
  54.         2 => array( 'fillColor' => '070707', 'strokeColor' => '070707', 'fillOpacity' => 0.3, 'strokeOpacity' => 0.8, 'strokeWidth' => 2),
  55.         3 => array( 'fillColor' => 'c94a4a', 'strokeColor' => 'e82828', 'fillOpacity' => 0.3, 'strokeOpacity' => 0.8, 'strokeWidth' => 2),
  56.         4 => array( 'fillColor' => '1e39db', 'strokeColor' => '1e39db', 'fillOpacity' => 0.3, 'strokeOpacity' => 0.8, 'strokeWidth' => 2),
  57.         5 => array( 'fillColor' => '1ed4db', 'strokeColor' => '1ed4db', 'fillOpacity' => 0.3, 'strokeOpacity' => 0.8, 'strokeWidth' => 2),
  58.         6 => array( 'fillColor' => '4355c3', 'strokeColor' => '1edb4b', 'fillOpacity' => 0.3, 'strokeOpacity' => 0.8, 'strokeWidth' => 2),
  59.         7 => array( 'fillColor' => '3ddb1e', 'strokeColor' => '3ddb1e', 'fillOpacity' => 0.3, 'strokeOpacity' => 0.8, 'strokeWidth' => 2),
  60.         8 => array( 'fillColor' => 'dbc71e', 'strokeColor' => 'dbc71e', 'fillOpacity' => 0.3, 'strokeOpacity' => 0.8, 'strokeWidth' => 2),
  61.         9 => array( 'fillColor' => 'db4f1e', 'strokeColor' => 'db4f1e', 'fillOpacity' => 0.3, 'strokeOpacity' => 0.8, 'strokeWidth' => 2),
  62.         10 => array( 'fillColor' => 'd91edb', 'strokeColor' => 'd91edb', 'fillOpacity' => 0.3, 'strokeOpacity' => 0.8, 'strokeWidth' => 2),
  63.         11 => array( 'fillColor' => '1e34db', 'strokeColor' => '1e34db', 'fillOpacity' => 0.3, 'strokeOpacity' => 0.8, 'strokeWidth' => 2),
  64.         12 => array( 'fillColor' => 'db1e65', 'strokeColor' => 'db1e65', 'fillOpacity' => 0.3, 'strokeOpacity' => 0.8, 'strokeWidth' => 2)
  65.         )
  66. );
  67.  
  68.  
  69. /* Creation of the custom post types
  70.  * Also script and stylesheet importing
  71.  * Note: The plugin uses jQueryUI library, which includes jQuery UI Stylesheet. If you want to use your own stylesheet made with jQuery UI stylesheet generator, please replace the jquery-ui.css link address with your own stylesheet.
  72.  * jQuery UI is only used in the dialog window which opens when user clicks a highlighted area.
  73.  * Later there will be option for changing the stylesheet.
  74.  * */
  75. function imgmap_create_post_type() {
  76.     if(!get_option('imgmap_colors')) {
  77.         global $imgmap_colors; 
  78.         add_option('imgmap_colors', $imgmap_colors);
  79.     }
  80.     /* Create the imagemap post type */
  81.     register_post_type(IMAGEMAP_POST_TYPE,
  82.         array(
  83.             'labels' => array(
  84.                 'name' => __('Image maps'),
  85.                 'singular_name' => __('Image map'),
  86.                 'add_new' => __('Add new Image map'),
  87.                 'all_items' => __('All Image maps'),
  88.                 'add_new_item' => __('Add new Image map'),
  89.                 'edit_item' => __('Edit Image map'),
  90.                 'new_item' => __('New Image map'),
  91.                 'view_item' => __('View Image map'),
  92.                 'search_items' => __('Search Image maps'),
  93.                 'not_found' => __('Image map not found'),
  94.                 'not_found_in_trash' => __('Image map not found in trash'),
  95.             ),
  96.             'public' => true,
  97.             'menu_icon' => plugins_url() . '/imagemapper/imagemap_icon.png',
  98.             'exclude_from_search' => true,
  99.             'has_archive' => true,
  100.             'supports' => array(
  101.                     'title'
  102.                 )
  103.             )
  104.     );
  105.    
  106.     /* Create the imagemap area post type */
  107.     /* Area to highlight. */
  108.     register_post_type(IMAGEMAP_AREA_POST_TYPE,
  109.         array(
  110.             'labels' => array(
  111.                 'name' => __('Image map areas'),
  112.                 'singular_name' => __('Image map area'),
  113.                 'add_new' => __('Add new Image map area'),
  114.                 'all_items' => __('All Image map areas'),
  115.                 'add_new_item' => __('Add new Image map area'),
  116.                 'edit_item' => __('Edit Image map area'),
  117.                 'new_item' => __('New Image map area'),
  118.                 'view_item' => __('View Image map area'),
  119.                 'search_items' => __('Search Image map areas'),
  120.                 'not_found' => __('Image map area not found'),
  121.                 'not_found_in_trash' => __('Image map area not found in trash'),
  122.             ),
  123.             'public' => true,
  124.             'has_archive' => true,
  125.             'menu_icon' => plugins_url() . '/imagemapper/imagemap_area_icon.png',
  126.         )
  127.     );
  128.    
  129.     /* Import ImageMapster and jQuery UI */
  130.     wp_register_script('imgmap_imagemapster', plugins_url() . '/imagemapper/script/jquery.imagemapster.min.js');
  131.     //wp_register_style('jquery_ui', 'http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css');
  132.     wp_register_style('imgmap_style', plugins_url().'/imagemapper/imgmap_style.css');
  133.  
  134.     /* Enqueue jQuery UI, jQuery and ImageMapster */
  135.     wp_enqueue_style(array( 'imgmap_style' ));
  136.    
  137.    
  138.     if(get_option('imgmap-include-jquery', NULL) === NULL)
  139.         update_option('imgmap-include-jquery', true);
  140.    
  141.     if(get_option('imgmap-include-jquery-ui', NULL) === NULL)
  142.         update_option('imgmap-include-jquery-ui', true);
  143.    
  144.     if(get_option('imgmap-include-jquery-ui-dialog', NULL) === NULL)
  145.         update_option('imgmap-include-jquery-ui-dialog', true);
  146.    
  147.     /* Not really necessary to have options to not include jquery because of the Wordpress script enqueue function.
  148.     if(get_option('imgmap-include-jquery'))
  149.         wp_enqueue_script(array( 'jquery'));
  150.    
  151.     if(get_option('imgmap-include-jquery-ui'))
  152.         wp_enqueue_script(array( 'jquery-ui-core'));
  153.        
  154.     if(get_option('imgmap-include-jquery-ui-dialog'))
  155.         wp_enqueue_script(array( 'jquery-ui-dialog'));
  156.     */ 
  157.    
  158.     wp_enqueue_script(array( 'jquery', 'jquery-ui', 'jquery-ui-dialog', 'editor', 'editor_functions', 'imgmap_imagemapster' ));
  159.    
  160.     /* The javascript file server needs to load for plugin's functionality depends on is the page is the admin panel or a frontend page */
  161.     /* (The frontend version obviously doesn't have all the features backend version has, e.g. the imagemap editor) */
  162.     if(is_admin()) {
  163.         wp_register_script('imgmap_admin_script', plugins_url() . '/imagemapper/imagemapper_admin_script.js');
  164.         wp_enqueue_script(array('imgmap_admin_script'));
  165.        
  166.         // WP 3.5 introduced a new better color picker
  167.         if(get_bloginfo('version') >= 3.5) {
  168.             wp_enqueue_style(array( 'wp-color-picker' ));
  169.             wp_enqueue_script(array( 'wp-color-picker' ));
  170.         }
  171.     }
  172.     else {
  173.         wp_register_script('imgmap_script', plugins_url() . '/imagemapper/imagemapper_script.js');
  174.         wp_enqueue_script('imgmap_script');
  175.     }
  176.    
  177.    
  178.     wp_localize_script('imgmap_script', 'imgmap', array(
  179.         'ajaxurl' => admin_url('admin-ajax.php'),
  180.         'pulseOption' => get_option('imgmap-pulse'),
  181.         'admin_logged' => current_user_can('edit_posts'),
  182.         'alt_dialog' => get_option('imgmap-alt-dialog')));
  183. };
  184.  
  185. // Set custom columns for imagemap archive page
  186. function imgmap_set_imagemap_columns($columns) {
  187.     $new_columns['cb'] = '<input type="checkbox" />';
  188.     $new_columns['image'] = __('Image');
  189.     $new_columns['title'] = _x('Imagemap name', 'column name');
  190.     $new_columns['area_count'] = __('Areas');
  191.     $new_columns['date'] = __('Updated');
  192.     $new_columns['author'] = __('Author');
  193.     return $new_columns;
  194. }
  195.  
  196. // ..and do the same for areas
  197. function imgmap_set_imagemap_area_columns($columns) {
  198.     $new_columns['cb'] = '<input type="checkbox" />';
  199.     $new_columns['title'] = _x('Imagemap area name', 'column name');
  200.     $new_columns['parent_image'] = __('Imagemap image');
  201.     $new_columns['parent_title'] = __('Imagemap title');
  202.     $new_columns['date'] = __('Updated');
  203.     $new_columns['author'] = __('Author');
  204.     return $new_columns;
  205. }
  206.  
  207. //Define what to do for custom columns
  208. function imgmap_manage_imagemap_columns($column_name, $id) {
  209.     global $wpdb;
  210.     switch($column_name) {
  211.         case 'image':
  212.             echo '<img class="imagemap-column-image" src="'.get_post_meta($id, 'imgmap_image', true).'" alt>';
  213.             break;
  214.            
  215.         case 'area_count':
  216.             $areas = get_posts('post_parent='.$id.'&post_type='.IMAGEMAP_AREA_POST_TYPE.'&numberposts=-1');
  217.             echo count($areas);
  218.             break;
  219.     }
  220. }
  221. // for the areas too
  222. function imgmap_manage_imagemap_area_columns($column_name, $id) {
  223.     global $wpdb;
  224.     switch($column_name) {
  225.         case 'parent_image':
  226.             $post = get_post($id);
  227.             echo '<img class="imagemap-column-image" src="'.get_post_meta($post->post_parent, 'imgmap_image', true).'" alt>';
  228.             break;
  229.        
  230.         case 'parent_title':
  231.             $post = get_post($id);
  232.             echo '<a href="'.get_edit_post_link($post->post_parent).'">'.get_the_title($post->post_parent).'</a>';
  233.             break;
  234.     }
  235. }
  236.  
  237. //Make the parent title column sortable, so there's a way to sort areas by parent image map.
  238. function imgmap_register_sortable_area_columns( $columns ) {
  239.     $columns['parent_title'] = 'parent_title';
  240.     return $columns;
  241. }
  242.  
  243. /* To enable author to upload an image for the image map. */
  244. function imgmap_add_post_enctype() {
  245.     echo ' enctype="multipart/form-data"';
  246. }
  247.  
  248. /* When updating a post, Wordpress needs to check for the custom fields
  249.  * At the moment it's only the uploaded image.
  250.  * */
  251. function imgmap_save_meta($id = false) {
  252.    
  253.     if(get_post_type($id) == IMAGEMAP_POST_TYPE) {
  254.        
  255.         if(isset($_FILES['imgmap_image'])) {
  256.             $uploadedFile = $_FILES['imgmap_image'];
  257.             if($uploadedFile['error'] == 0){
  258.                
  259.                 $file = wp_handle_upload($uploadedFile, array('test_form' => FALSE));
  260.                
  261.                 if(!strpos('image/', $file['type']) == 0)
  262.                 wp_die('This is not an image!');
  263.                
  264.                 update_post_meta($id, 'imgmap_image', $file['url']);
  265.             }
  266.         }
  267.     }
  268.     if(get_post_type($id) == IMAGEMAP_AREA_POST_TYPE) {
  269.         $area_vars = imgmap_get_imgmap_area_vars($id);
  270.         $area_vars->type = $_POST['area-type'];
  271.         $area_vars->tooltip_text = wp_kses_post($_POST['area-tooltip-text']);
  272.         $area_vars->title_attribute = esc_attr($_POST['area-title-attribute']);
  273.         $area_vars->link_url = esc_url($_POST['area-link-url']);
  274.         $area_vars->link_type = esc_attr($_POST['area-link-type']);
  275.         $area_vars->link_post = esc_attr($_POST['area-link-post']);
  276.         $area_vars->link_page = esc_attr($_POST['area-link-page']);
  277.         // Save area settings in JSON format.
  278.         // Basically when you need one of them, you need all others as well, so it's inefficient to save them in separate columns.
  279.         update_post_meta($id, 'imgmap_area_vars', $area_vars);
  280.     }
  281. }
  282.  
  283. function imgmap_updated_message( $messages ) {
  284.     global $post_ID;
  285.     $post = get_post($post_ID);
  286.     if(get_post_type($post_ID) != IMAGEMAP_POST_TYPE)
  287.         return;
  288.        
  289.     $messages[IMAGEMAP_POST_TYPE] = array(
  290.         0 => '', // Unused. Messages start at index 1.
  291.         1 => sprintf( __('Image map updated. You can add the image map to a post with Upload/Insert media tool.') ),
  292.         2 => __('Custom field updated.'),
  293.         3 => __('Custom field deleted.'),
  294.         4 => __('Image map updated.'),
  295.         5 => isset($_GET['revision']) ? sprintf( __('Image map restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
  296.         6 => sprintf( __('Image map published.')),
  297.         7 => __('Image map saved.'),
  298.         8 => sprintf( __('Image map submitted.')),
  299.         9 => sprintf( __('Image map scheduled for: <strong>%1$s</strong>.'), date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ) ),
  300.         10 => sprintf( __('Image map draft updated.')),
  301.     );
  302.    
  303.     return $messages;
  304. }
  305.  
  306. function imgmap_imagemap_tab_menu() {
  307.     add_submenu_page('edit.php?post_type='.IMAGEMAP_AREA_POST_TYPE, 'Imagemap area styles', 'Highlight styles', 'edit_posts', 'imagemap-area-styles', 'imgmap_area_styles');
  308.     add_submenu_page('edit.php?post_type='.IMAGEMAP_POST_TYPE, 'Image map settings', 'Image map settings', 'edit_posts', 'imagemap-settings', 'imgmap_imagemap_settings');
  309. }
  310.  
  311.  
  312. /* Add custom fields to the custom post type forms.
  313.  * */
  314. function imgmap_custom_form() {
  315.     global $_wp_post_type_features;
  316.    
  317.     add_meta_box('imagemap-image-container', 'Image', 'imgmap_form_image', IMAGEMAP_POST_TYPE, 'normal');
  318.     add_meta_box('imagemap-addarea', 'Add area', 'imgmap_form_addarea', IMAGEMAP_POST_TYPE, 'side');
  319.     add_meta_box('imagemap-areas', 'Areas', 'imgmap_form_areas', IMAGEMAP_POST_TYPE, 'side');
  320.    
  321.     remove_post_type_support(IMAGEMAP_AREA_POST_TYPE, 'editor');
  322.        
  323.     add_meta_box('imagemap-area-highlight', 'Highlight', 'imgmap_area_form_highlight', IMAGEMAP_AREA_POST_TYPE, 'side');
  324.     add_meta_box('imagemap-area-settings', 'Settings', 'imgmap_area_form_settings', IMAGEMAP_AREA_POST_TYPE, 'side');
  325.     add_meta_box('imagemap-area-types', 'Mouse event', 'imgmap_area_form_types', IMAGEMAP_AREA_POST_TYPE, 'normal');
  326. }
  327.  
  328. /* Custom field for the imagemap image.
  329.  * Includes also the imagemap editor.
  330.  *  */
  331. function imgmap_form_image($post) {
  332.     ?>
  333.     <label><h4>Choose an image file to use with the image map. Save the post after choosing the file to upload it.</h4>
  334.     <input type="file" name="imgmap_image" id="file" /></label>
  335.     <h4><?php echo strlen(get_post_meta($post->ID, 'imgmap_image', true)) > 0 ? 'Image map' : ''; ?></h4>
  336.     <div style="position: relative; margin-top: 30px">
  337.         <img src="<?php echo get_post_meta($post->ID, 'imgmap_image', true); ?>" usemap="#imgmap-<?php echo $post->ID ?>" id="imagemap-image" />
  338.         <canvas id="image-area-canvas"></canvas>
  339.         <canvas id="image-coord-canvas"></canvas>
  340.     </div>
  341.     <?php
  342.        
  343.         $areas = get_posts('post_parent='.$post->ID.'&post_type='.IMAGEMAP_AREA_POST_TYPE.'&numberposts=-1');
  344.        
  345.     ?>
  346.     <map name="imgmap-<?php echo $post->ID ?>">
  347.         <?php
  348.             foreach($areas as $a) {
  349.                 echo imgmap_create_area_element($a->ID, $a->post_title);
  350.             }
  351.         ?>
  352.     </map>
  353.     <?php
  354. }
  355.  
  356. function imgmap_media_upload_tab($tabs) {
  357.     $newtab = array('imagemap' => __('Image map', 'imagemap'));
  358.     return array_merge($tabs, $newtab);
  359. }
  360.  
  361. function imgmap_media_upload_tab_action() {
  362.     return wp_iframe('media_imgmap_media_upload_tab_inside');
  363. }
  364.  
  365. function media_imgmap_media_upload_tab_inside() {
  366.     media_upload_header(); ?>
  367.     <p>
  368.         <?php
  369.         $areas = get_posts('post_type='.IMAGEMAP_POST_TYPE.'&numberposts=-1');
  370.         foreach($areas as $a) {
  371.         $title = strlen($a->post_title) == 0 ? '(untitled)' : $a->post_title;
  372.             ?>
  373.             <div data-imagemap="<?php echo $a->ID; ?>" class="insert-media-imagemap" style="background-image: url(<?php echo get_post_meta($a->ID, 'imgmap_image', true); ?>);">
  374.                 <div><?php echo $title ?></div>
  375.             </div>
  376.         <?php }
  377.         ?>
  378.     </p>
  379.     <?php
  380. }
  381.  
  382. /* Displays the image map in a frontend page. */
  383. function imgmap_frontend_image($id, $element_id) {
  384.     echo get_imgmap_frontend_image($id, $element_id);
  385. }
  386.    
  387. function get_imgmap_frontend_image($id, $element_id) {
  388.     $atts = array(
  389.         'id' => $id,
  390.         'element_id' => $element_id);
  391.     return imgmap_frontend_image_shortcode($atts);
  392. }
  393.  
  394. function imgmap_frontend_image_shortcode($atts)
  395. {
  396.     global $element_id_count;  // prevent duplicate maps
  397.     $element_id_count++;        // start with 1
  398.     $id = $atts['id'];          // get the map id from the passed-in attributes
  399.     if (isset($atts['element_id']))
  400.         $element_id = $atts['element_id'];
  401.     else
  402.         $element_id = $id . '-' . $element_id_count;    // build the unique identifier
  403.                             // carry on with the original processing
  404.     $areas = array();
  405.  
  406.     $value = '
  407.     <div class="imgmap-frontend-image">
  408.     <div class="imgmap-dialog-wrapper" id="imgmap-dialog-'.$element_id.'"></div>
  409.     <img src="'.get_post_meta($id, 'imgmap_image', true).'" usemap="#imgmap-'.$element_id.'" id="imagemap-'.$element_id.'" />
  410.     <map name="imgmap-'.$element_id.'">';
  411.     $areas = get_posts('post_parent='.$id.'&post_type='.IMAGEMAP_AREA_POST_TYPE.'&numberposts=-1');
  412.     foreach($areas as $a) {
  413.         $value .= imgmap_create_area_element($a->ID, $a->post_title);
  414.     }
  415.     $value .= '</map>';
  416.    
  417.     $altLink = get_option('imgmap-alternative-link-positions');
  418.    
  419.     if($altLink == 'hidden' || $altLink == 'visible') {
  420.        
  421.         if($altLink == 'hidden') {
  422.             $value .= '
  423.             <a class="altlinks-toggle" data-parent="'.$element_id.'">Show links</a>
  424.             <div class="altlinks-container imgmap-hidden" id="altlinks-container-'.$element_id.'">';
  425.         }
  426.         else
  427.             $value .= '<div class="altlinks-container">';
  428.        
  429.        
  430.         foreach($areas as $a) {
  431.             $title = $a->post_title == '' ? '(untitled)' : $a->post_title;
  432.             $meta = imgmap_get_imgmap_area_vars($a->ID);
  433.             $meta->type = isset($meta->type) ? $meta->type : 'popup';
  434.             $url = $meta->type == 'link' ? ' href="'.imgmap_get_link_url($meta).'"' : '';
  435.             $value .= '<a class="alternative-links-imagemap"'.$url.' data-key="area-'.$a->ID.'" data-type="'.$meta->type.'" data-parent="imgmap-'.$element_id.'">'.$title.'</a>, ';
  436.         }
  437.         $value = substr($value, 0, -2);
  438.         $value .= '</div>';
  439.     }
  440.     $value .= '
  441.     </div>';
  442.     return $value;
  443. }
  444. add_shortcode( 'imagemap', 'imgmap_frontend_image_shortcode' );
  445.  
  446.  
  447. /* Fields for adding new areas to the imagemap using the editor.
  448.  * However the editor functionality is included in the image field. */
  449. function imgmap_form_addarea($post) {
  450.     ?><h4><?php _e('Instructions for area shaping'); ?></h4>
  451.     <p><?php _e('Start creating the shape of the new area by clicking the image of the image map on the left.'); ?></p>
  452.     <p><?php _e('The first and the last point of the path are joined automatically'); ?></p>
  453. <p><?php _e('When the shape is ready, press the button below.');?></p>
  454.     <input type="button" value="Undo" title="Shift + Mouse left" class="button" id="undo-area-button"/>
  455.     <input type="button" value="Add area" class="button" id="add-area-button" style="float:right"/>
  456.     <?php
  457. }
  458.  
  459. /* List of the current areas of the imagemap.
  460.  * Every element in the list has link to edit form of the area and a shortcut for deleting the areas. */
  461. function imgmap_form_areas($post) {
  462.     $areas = get_posts('post_parent='.$post->ID.'&post_type='.IMAGEMAP_AREA_POST_TYPE.'&orderby=id&order=desc&numberposts=-1');
  463.     echo '<ul>';
  464.     foreach($areas as $a) {
  465.         echo imgmap_create_list_element($a->ID);
  466.     }
  467.     echo '</ul>';
  468. }
  469.  
  470. /* Settings for the single imagemap area */
  471. function imgmap_area_form_settings($post) {
  472.     $meta = imgmap_get_imgmap_area_vars($post->ID);
  473.     $meta->title_attribute = isset($meta->title_attribute) ? $meta->title_attribute : '';
  474.     ?>
  475.     <p><input style="width: 100%" type="text" name="area-title-attribute" value="<?php echo $meta->title_attribute; ?>" placeholder="HTML title attribute"></p>
  476.     <p><a title="The HTML title attribute often shows as a small tooltip when mouse hovers over an element. No tooltip is shown if the field is left empty.">What is this?<br>Hover mouse over this text for an example.</a></p>
  477.     <?php
  478. }
  479.  
  480. /* Settings for the single imagemap area highlight */
  481. function imgmap_area_form_highlight($post) {
  482.     $imgmap_colors = get_option('imgmap_colors');
  483.     $meta = imgmap_get_imgmap_area_vars($post->ID);
  484.     ?>
  485.     <h4>Highlight styles</h4>
  486.     <div id="imgmap-area-styles"><?php
  487.     foreach($imgmap_colors['colors'] as $key => $color) {
  488.         echo imgmap_get_style_element($key, $color, $meta->color);
  489.     }
  490.     if(count($imgmap_colors['colors']) == 0)
  491.         echo '<p>'.__('No styles found. Start by adding a new style.').'</p>';
  492.     ?><br style="clear:both;"></div>
  493.     <a style="display: block; padding: 8px;" href="edit.php?post_type=imagemap_area&page=imagemapper.php">Add new</a><?php
  494. }
  495.  
  496. function imgmap_get_style_element($key, $color, $chosen = false, $data = false) {
  497.     echo '
  498.     <div class="imgmap-area-style'.($key == $chosen ? ' chosen' : '').'" data-id="'.$key.'" title="'.$key.'"
  499.     '.($data ? '
  500.     data-fill-color="'.esc_attr($color['fillColor']).'"
  501.     data-fill-opacity="'.esc_attr($color['fillOpacity']).'"
  502.     data-stroke-color="'.esc_attr($color['strokeColor']).'"
  503.     data-stroke-opacity="'.esc_attr($color['strokeOpacity']).'"
  504.     data-stroke-width="'.esc_attr($color['strokeWidth']).'"' :
  505.     '').'>
  506.         <div class="imgmap-area-color" style="
  507.         background-color: '.imgmap_hex_to_rgba($color['fillColor'], $color['fillOpacity']).';
  508.         box-shadow: 0 0 0 '.$color['strokeWidth'].'px '.imgmap_hex_to_rgba($color['strokeColor'], $color['strokeOpacity']).'"
  509.         ></div>
  510.     </div>';
  511. }
  512.  
  513. function imgmap_area_styles() { ?>
  514.     <div class="wrap">
  515.     <h2>Imagemap highlight styles</h2>
  516.     <div class="divide-left">
  517.         <h3>Saved styles</h3>
  518.         <?php
  519.         $imgmap_styles = get_option('imgmap_colors');
  520.         ?>
  521.             <div id="imgmap-area-styles-edit"><?php
  522.                 if(count($imgmap_styles['colors']) == 0)
  523.                     echo '<p>'.__('No styles found. Start by adding a new style.').'</p>';
  524.            
  525.                 foreach($imgmap_styles['colors'] as $key => $color) {
  526.                     echo imgmap_get_style_element($key, $color, false, true); }
  527.                 ?>
  528.         </div><br style="clear:both;">
  529.     </div>
  530.     <div class="divide-right">
  531.     <h3>Add/Edit styles</h3>
  532.         <div id="add-new-imgmap-style">
  533.             <table>
  534.             <tr>
  535.                 <th>Fill color</th>
  536.                 <th>Fill opacity</th>
  537.             </tr>
  538.             <tr>
  539.                 <td>
  540.                     <div class="form-field">
  541.                         <input type="text" maxlength="6" id="imgmap-new-style-fillcolor" class="color-picker-field" placeholder="rrggbb" />
  542.                     </div>
  543.                 </td>
  544.                 <td>
  545.                     <div class="form-field">
  546.                         <input type="number" value="1" min="0" max="1" step="0.1" id="imgmap-new-style-fillopacity" />
  547.                     </div>
  548.                 </td>
  549.             </tr>
  550.             <tr>
  551.                 <th>Stroke color</th>
  552.                 <th>Stroke opacity</th>
  553.                 <th>Stroke width</th>
  554.             </tr>
  555.             <tr>
  556.                 <td>
  557.                     <div class="form-field">
  558.                         <input type="text" maxlength="6" id="imgmap-new-style-strokecolor" class="color-picker-field" placeholder="rrggbb" />
  559.                     </div>
  560.                 </td>
  561.                 <td>
  562.                     <div class="form-field">
  563.                         <input type="number" value="1" min="0" max="1" step="0.1" id="imgmap-new-style-strokeopacity" />
  564.  
  565.                     </div>
  566.                 </td>
  567.                 <td>
  568.                     <div class="form-field">
  569.                         <input type="number" value="1" min="0" step="1" id="imgmap-new-style-strokewidth" />
  570.                     </div>
  571.                 </td>
  572.             </tr>
  573.         </table>
  574.             <p>
  575.                 <input type="button" class="button" id="add-new-imgmap-style-button" value="Add new style">
  576.                 <input type="button" class="button" id="edit-imgmap-style-button" value="Save changes" disabled>
  577.                 <input type="button" class="button" id="delete-imgmap-style-button" value="Delete style" disabled>
  578.             </p>
  579.         </div>
  580.     </div>
  581.     <?php
  582. }
  583.        
  584. function imgmap_imagemap_settings() {
  585.     register_setting('imgmap-settings', 'imgmap-alternative-link-positions');
  586.    
  587.     if(!get_option('imgmap-alternative-link-positions'))
  588.         update_option('imgmap-alternative-link-positions', 'off');
  589.        
  590.     if(!get_option('imgmap-pulse'))
  591.         update_option('imgmap-pulse', 'never');
  592.        
  593.    
  594.     ?>
  595.     <div class="wrap">
  596.         <h2><?php _e('Image map settings'); ?></h2>
  597.         <form method="post" action="<?php echo admin_url('admin.php'); ?>">
  598.         <input type="hidden" name="action" value="imgmap_save_settings" />
  599.         <?php wp_nonce_field('imgmap-settings'); ?>
  600.         <table class="form-table">
  601.             <tr valign="top">
  602.                 <th scope="row"><a title="Provides corresponding links for the areas of an image map below the image. Use if you're concerned of if users are able to use the image map correctly.">Fallback links for areas.</a></th>
  603.                 <td>
  604.                     <input type="radio" name="imgmap-settings-fallback-link-position" value="off" <?php echo get_option('imgmap-alternative-link-positions') == 'off' ? 'checked' : ''; ?> /> <?php _e('No'); ?><br>
  605.                     <input type="radio" name="imgmap-settings-fallback-link-position" value="hidden" <?php echo get_option('imgmap-alternative-link-positions') == 'hidden' ? 'checked' : ''; ?> /> <?php _e('Hidden'); ?><br>
  606.                     <input type="radio" name="imgmap-settings-fallback-link-position" value="visible" <?php echo get_option('imgmap-alternative-link-positions') == 'visible' ? 'checked' : ''; ?> /> <?php _e('Always visible'); ?>
  607.                 </td>
  608.             </tr>
  609.             <tr valign="top">
  610.                 <th scope="row"><a title="Highlights all areas for a short time when mouse is moved over an image map.">Highlight all areas</a></th>
  611.                 <td>
  612.                     <input type="radio" name="imgmap-settings-pulse" value="never" <?php echo get_option('imgmap-pulse') == 'never' ? 'checked' : ''; ?> /> <?php _e('Never'); ?><br>
  613.                     <input type="radio" name="imgmap-settings-pulse" value="first_time" <?php echo get_option('imgmap-pulse') == 'first_time' ? 'checked' : ''; ?> /> <?php _e('When mouse is moved over the image map first time.'); ?><br>
  614.                     <input type="radio" name="imgmap-settings-pulse" value="always" <?php echo get_option('imgmap-pulse') == 'always' ? 'checked' : ''; ?> /> <?php _e('Always when mouse is moved over the image map.'); ?>
  615.                 </td>
  616.             </tr>
  617.             <tr valign="top">
  618.                 <th scope="row"><a title="Use if the default popup window layout doesn't look right.">Use new popup layout</a></th>
  619.                 <td>
  620.                     <input type="radio" name="imgmap-settings-alt-dialog" value="no" <?php echo !get_option('imgmap-alt-dialog') ? 'checked' : ''; ?> /> <?php _e('No'); ?><br>
  621.                     <input type="radio" name="imgmap-settings-alt-dialog" value="yes" <?php echo get_option('imgmap-alt-dialog') ? 'checked' : ''; ?> /> <?php _e('Yes'); ?><br>
  622.                 </td>
  623.             </tr>
  624.         </table>
  625.        
  626.         <?php submit_button(); ?>
  627.         </form>
  628.     </div>
  629.     <?php
  630. }
  631.  
  632. function imgmap_save_settings() {
  633.     update_option('imgmap-alternative-link-positions', $_POST['imgmap-settings-fallback-link-position']);
  634.     update_option('imgmap-pulse', $_POST['imgmap-settings-pulse']);
  635.     update_option('imgmap-alt-dialog', $_POST['imgmap-settings-alt-dialog'] == 'yes');
  636.     /*
  637.     update_option('imgmap-include-jquery', $_POST['imgmap-settings-include-jquery']);
  638.     update_option('imgmap-include-jquery-ui', $_POST['imgmap-settings-include-jquery-ui']);
  639.     update_option('imgmap-include-jquery-ui-dialog', $_POST['imgmap-settings-include-jquery-ui-dialog']);
  640.     */
  641.     wp_redirect($_POST['_wp_http_referer']);
  642. }
  643.  
  644. function imgmap_get_imgmap_area_vars($id) {
  645.     $meta = get_post_meta($id, 'imgmap_area_vars', true);
  646.    
  647.     // In 0.5 and earlier versions imgmap_area_vars were saved as JSON string.
  648.     // It was changed because there was a problem with scandinavian letters and JSON encoding
  649.     if(is_string($meta))
  650.         $meta = json_decode($meta);
  651.    
  652.     // Disables Creating object from empty value warnings.
  653.     if(empty($meta))
  654.         $meta = new StdClass();
  655.    
  656.     return $meta;
  657. }
  658.  
  659. function imgmap_area_form_types($post) {
  660.     // Get area variables from post meta
  661.     $meta = imgmap_get_imgmap_area_vars($post->ID);
  662.     $meta->type = isset($meta->type) ? $meta->type : 'popup';
  663.     $meta->tooltip_text = isset($meta->tooltip_text) ? $meta->tooltip_text : '';
  664.     $meta->link_url = isset($meta->link_url) ? $meta->link_url : '';
  665.     $meta->link_type = isset($meta->link_type) ? $meta->link_type : 'absolute';
  666.     $meta->link_page = isset($meta->link_page) ? $meta->link_page : -1;
  667.     ?>
  668.     <div style="width: 20%; float: left;" id="area-form-types">
  669.         <p><input type="radio" name="area-type" onclick="ShowTypes('link')" value="link" <?php echo $meta->type == 'link' ? 'checked' : '' ?>>
  670.             <input type="button" class="button" onclick="ShowTypes('link')" value="Link" /></p>
  671.         <p><input type="radio" name="area-type" onclick="ShowTypes('tooltip')" value="tooltip" <?php echo $meta->type == 'tooltip' ? 'checked' : '' ?>>
  672.             <input type="button" class="button" onclick="ShowTypes('tooltip')" value="Tooltip" /></p>
  673.         <p><input type="radio" name="area-type" onclick="ShowTypes('popup')" value="popup" <?php echo $meta->type == 'popup' ? 'checked' : '' ?>>
  674.             <input type="button" class="button" onclick="ShowTypes('popup')" value="Popup window" /></p>
  675.     </div>
  676.     <div id="imagemap-area-type-editors">
  677.         <div id="imagemap-area-popup-editor" class="area-type-editors <?php echo $meta->type == 'popup' ? 'area-type-editor-current' : '' ?>">
  678.         <h4>Show text and images in a popup window when user clicks the area</h4>
  679.         <?php
  680.         if(function_exists('wp_editor')) {
  681.             wp_editor($post->post_content, 'content', array( 'editor_css' => '<style> body { min-height: 300px; background-color: white; } </style>' ));
  682.         }
  683.         else if(function_exists('the_editor')) {
  684.             the_editor($post->post_content, 'content', array( 'textarea_rows' => 8 ));
  685.         }
  686.         else {
  687.             echo 'Something went wrong when loading editor.';
  688.         }
  689.         ?></div>
  690.         <div id="imagemap-area-tooltip-editor" class="area-type-editors <?php echo $meta->type == 'tooltip' ? 'area-type-editor-current' : '' ?>">
  691.         <h4>Show a small tooltip when users move their mouse on the area</h4>
  692.             <p><label>Tooltip text <br />
  693.                 <textarea name="area-tooltip-text" cols="50" rows="6"><?php echo $meta->tooltip_text; ?></textarea>
  694.             </label></p>
  695.             <p>HTML elements such as links and images are allowed. Javascript not so much.</p>
  696.         </div>
  697.         <div id="imagemap-area-link-editor" class="area-type-editors <?php echo $meta->type == 'link' ? 'area-type-editor-current' : '' ?>">
  698.         <h4>Select where users should be redirected when they click the area</h4>
  699.             <table>
  700.                 <tr>
  701.             <td><label><input type="radio" name="area-link-type" value="post" <?php echo $meta->link_type == 'post' ? 'checked' : ''; ?>> Link to an existing post:</label></td>
  702.             <td>
  703.                 <select name="area-link-post"><?php
  704.                 $posts = get_posts(array('numberposts' => -1));
  705.                 foreach($posts as $post) { echo '<option value="'.$post->ID.'" '.($meta->link_post == $post->ID ? 'selected' : '').'>'.(strlen($post->post_title) ? $post->post_title : '(untitled, id: '.$post->ID.')').'</option>'; }
  706.                 ?></select>
  707.             </td>
  708.             <tr><td><label><input type="radio" name="area-link-type" value="page" <?php echo $meta->link_type == 'page' ? 'checked' : ''; ?>> Link to an existing page:</label></td><td><?php wp_dropdown_pages(array('name' => 'area-link-page', 'selected' => $meta->link_page)); ?></td></tr>
  709.             <tr><td><label><input type="radio" name="area-link-type" value="absolute" <?php echo $meta->link_type == 'absolute' ? 'checked' : ''; ?>> Link to an url address:</label></td><td><input type="text" name="area-link-url" value="<?php echo $meta->link_url; ?>"></td></tr>
  710.             </tr>
  711.             </table>
  712.         </div>
  713.     </div>
  714.     <br style="clear:both">
  715. <?php }
  716.  
  717. /* Used when user adds a new area to the image map
  718.  * The function returns object with data of the newly-added area and link to edit it.
  719.  * Currently Wordpress should be redirecting user to the area edit form after the area has been saved.
  720.  * However there's a bug with the redirecting and it's redirecting in wrong page. Might be that Wordpress doesn't allow the redirect. */
  721. function imgmap_save_area_ajax() {
  722.     global $wpdb;
  723.     $area = new StdClass();
  724.     $area->coords = $_POST['coords'];
  725.     $area->text = '';
  726.     $area->title = '(untitled image map area)';
  727.     $area->title_attribute = '';
  728.     $area->parent = $_POST['parent_post'];
  729.     $post = array(
  730.     'post_author'    => get_current_user_id(),
  731.     'post_content'   => $area->text,
  732.     'post_parent'    => $area->parent,
  733.     'post_status'    => 'publish',
  734.     'post_name'      => $area->title,
  735.     'post_title'     => $area->title,
  736.     'post_type'      => IMAGEMAP_AREA_POST_TYPE
  737.     );
  738.     $post = wp_insert_post($post);
  739.    
  740.     $area->id = $post;
  741.     $area->link = get_edit_post_link($area->id);
  742.     update_post_meta($area->id, 'coords', $area->coords);
  743.     $meta = new StdClass();
  744.    
  745.     $meta->color = $styles['last_chosen'];
  746.     update_post_meta($area->id, 'imgmap_area_vars', json_encode($meta));
  747.     $area->html = imgmap_create_list_element($area->id, true);
  748.     ob_clean();
  749.     echo json_encode($area);
  750.     die();
  751. }
  752.  
  753. /* Shortlink for deleting an area. (Well, the functionality which happens when the shortlink is pressed. */
  754. function imgmap_delete_area_ajax() {
  755.     echo json_encode(wp_delete_post($_POST['post'], true));
  756.     die();
  757. }
  758.  
  759. /* Creates an area element to the HTML image map */
  760. function imgmap_create_area_element($id, $title) { 
  761.     $imgmap_colors = get_option('imgmap_colors');
  762.     $meta = imgmap_get_imgmap_area_vars($id);
  763.    
  764.     if($meta === null)
  765.         $meta = new StdClass();
  766.    
  767.     if(!isset($meta->color) || !isset($imgmap_colors['colors'][$meta->color]))
  768.         $meta->color = $imgmap_colors['last_chosen'];
  769.        
  770.     if(!isset($imgmap_colors['colors'][$meta->color]))
  771.         $color = array( 'fillColor' => 'fefefe', 'strokeColor' => 'fefefe', 'fillOpacity' => 0.3, 'strokeOpacity' => 0.6, 'strokeWidth' => 1);
  772.     else
  773.         $color = $imgmap_colors['colors'][$meta->color];
  774.    
  775.     $meta->type = isset($meta->type) ? $meta->type : '';
  776.     $meta->tooltip_text = isset($meta->tooltip_text) ? $meta->tooltip_text : '';
  777.     $link = imgmap_get_link_url($meta);
  778.    
  779.     $meta->title_attribute = isset($meta->title_attribute) ? $meta->title_attribute : '';
  780.    
  781.     return '<area data-type="'.esc_attr($meta->type).'" data-tooltip="'.esc_attr($meta->type == 'tooltip' ? $meta->tooltip_text : false ). '" data-fill-color="'.esc_attr(str_replace('#', '', $color['fillColor'])).'" data-fill-opacity="'.esc_attr($color['fillOpacity']).'" data-stroke-color="'.esc_attr(str_replace('#', '', $color['strokeColor'])).'" data-stroke-opacity="'.esc_attr($color['strokeOpacity']).'" data-stroke-width="'.esc_attr($color['strokeWidth']).'" data-mapkey="area-'.$id.'" shape="poly" coords="'.esc_attr(get_post_meta($id, 'coords', true)).'" href="'.esc_attr($link) .'" title="'.(isset($meta->title_attribute) ? $meta->title_attribute : $title).'" />';
  782. }
  783.  
  784. /* Creates an list element to the list of imagemap's areas. */
  785. function imgmap_create_list_element($id, $animated = false) {
  786.     return
  787.     '<li data-listkey="area-'.$id.'" class="area-list-element '.($animated ? 'area-list-element-animated' : '').'">
  788.     <div class="area-list-left">
  789.         <input id="area-checkbox-'.$id.'" data-listkey="area-'.$id.'" type="checkbox" checked>
  790.     </div>
  791.     <div class="area-list-right">
  792.         <label>Title: <input type="text" id="'.$id.'-list-area-title" value="'.get_the_title($id).'" /><div style="clear: both"></div></label>
  793.         <div class="area-list-meta">
  794.             <a class="save-area-link" href="#" onclick="SaveTitle('.$id.')">Save</a>
  795.             <a class="edit-area-link" href="'.get_edit_post_link($id).'">Edit page</a>
  796.             <a class="delete-area" data-area="'.$id.'">Delete</a>
  797.         </div>
  798.     </div>
  799.     </li>';
  800. }
  801.  
  802. function imgmap_get_link_url($meta) {
  803.     if($meta->type != 'link')
  804.         return '#';
  805.        
  806.     switch($meta->link_type) {
  807.         case 'post': return get_permalink($meta->link_post);
  808.         case 'page': return get_permalink($meta->link_page);
  809.         default: return isset($meta->link_url) ? $meta->link_url : '';
  810.     }
  811. }
  812.  
  813. /* Template for the imagemap frontend page.
  814.  * Checks first the theme folder.
  815.  * Note: If you want to edit the image map template, please check the single_imgmap.php template file in plugin's directory. */
  816. function imgmap_template($template) {
  817.     $post = get_the_ID();
  818.     if(get_post_type() == IMAGEMAP_POST_TYPE) {
  819.         if(locate_template(array('single-imgmap.php')) != '')
  820.             include locate_template(array('single-imgmap.php'));
  821.         else
  822.             include 'single-imgmap.php';
  823.         return;
  824.     }
  825.     return $template;
  826. }
  827.  
  828. /* Loads post in a jQuery dialog when a highlighted area is clicked.
  829.  * Checks first the theme folder, too */
  830. function imgmap_load_dialog_post_ajax() {
  831.     $post = get_post($_POST['id']);
  832.     if(locate_template(array('single-imgmap-dialog.php')) != '')
  833.         include locate_template(array('single-imgmap-dialog.php'));
  834.     else
  835.         include 'single-imgmap-dialog.php';
  836.     die();
  837. }
  838.  
  839. /* Returns array of area data of an imagemap. */
  840. function imgmap_get_area_coordinates_ajax() {
  841.     $return = array();
  842.     $areas = get_posts('post_parent='.$_POST['post'].'&post_type='.IMAGEMAP_AREA_POST_TYPE.'&orderby=id&order=desc&numberposts=-1');
  843.     $imgmap_colors = get_option('imgmap_colors');
  844.     foreach($areas as $a) {
  845.         $newArea = new StdClass();
  846.         $newArea->coords = get_post_meta($a->ID, 'coords', true);
  847.         $vars = imgmap_get_imgmap_area_vars($a->ID);
  848.         $newArea->style = isset($imgmap_colors['colors'][$vars->color]) ? $imgmap_colors['colors'][$vars->color] : array( 'fillColor' => 'fefefe', 'strokeColor' => 'fefefe', 'fillOpacity' => 0.3, 'strokeOpacity' => 0.6, 'strokeWidth' => 1);;
  849.         $newArea->id = $a->ID;
  850.         $return[] = $newArea;
  851.     }
  852.     echo json_encode($return);
  853.     die();
  854. }
  855.  
  856. /* Be sure to delete areas when deleting parent post */
  857. function imgmap_permanently_delete_imagemap($post_id) {
  858.     imgmap_delete_imagemap($post_id, true);
  859. }
  860.  
  861. /* ...and be sure to trash areas when trashing parent post as well. */
  862. function imgmap_trash_imagemap($post_id) {
  863.     imgmap_delete_imagemap($post_id, false);
  864. }
  865.  
  866. /* Delete areas when deleting imagemap.
  867.  * Doesn't actually restore trashed imagemap areas when restoring the imagemap. */
  868. function imgmap_delete_imagemap($post_id, $permanent) {
  869.    
  870.     $args = array(
  871.     'post_parent' => $post_id,
  872.     'post_type' => IMAGEMAP_POST_TYPE
  873.     );
  874.    
  875.     $posts = get_posts( $args );
  876.    
  877.     if (is_array($posts) && count($posts) > 0) {
  878.         // Delete all the Children of the Parent Page
  879.         foreach($posts as $post){
  880.             wp_delete_post($post->ID, $permanent);
  881.         }
  882.     }
  883. }
  884.  
  885. /* Insert image map code in posts */
  886. /* removed by Samatva - the *wrong* way to do shortcodes - breaks with "curly" quotation marks
  887. function imgmap_replace_shortcode($content) {
  888.     global $imagemaps;
  889.     preg_match_all('/\[imagemap id=\"(.*?)\"\]/', $content, $maps);
  890.     foreach($maps[1] as $map) {
  891.         if(!isset($imagemaps[$map]))
  892.             $imagemaps[$map] = 0;
  893.         $imagemaps[$map]++;
  894.            
  895.         $content = preg_replace('/\[imagemap id=\"'.$map.'\"\]/', get_imgmap_frontend_image($map, $map.'-'.$imagemaps[$map]), $content, 1);
  896.     }
  897.     return $content;
  898. }
  899. */
  900. function imgmap_save_area_title() {
  901.     if(current_user_can('manage_options')) {
  902.         $id = $_POST['id'];
  903.         $post = get_post($id);
  904.         $post->post_title = $_POST['title'];
  905.         echo wp_update_post($post);
  906.     }
  907.     die();
  908. }
  909.  
  910. function imgmap_set_area_color() {
  911.     if(current_user_can('manage_options')) {
  912.         $id = $_POST['post'];
  913.         $color = $_POST['color'];
  914.         $meta = imgmap_get_imgmap_area_vars($id);
  915.         echo json_encode($meta);
  916.         $meta->color = $color;
  917.         update_post_meta($id, 'imgmap_area_vars', json_encode($meta));
  918.     }
  919.     die();
  920. }
  921.  
  922. function imgmap_add_new_style() {
  923.     if(current_user_can('manage_options')) {
  924.         if(substr($_POST['fillColor'], 0, 1) == '#')
  925.             $_POST['fillColor'] = substr($_POST['fillColor'], 1);
  926.  
  927.         $style = array(
  928.             'fillColor' => $_POST['fillColor'],
  929.             'fillOpacity' => $_POST['fillOpacity'],
  930.             'strokeColor' => $_POST['strokeColor'],
  931.             'strokeOpacity' => $_POST['strokeOpacity'],
  932.             'strokeWidth' => $_POST['strokeWidth']
  933.         );
  934.         $style_option = get_option('imgmap_colors');
  935.         $key = $style_option['current_id'] + 1;
  936.         $style_option['colors'][$key] = $style;
  937.         $style_option['current_id']++;
  938.        
  939.         update_option('imgmap_colors', $style_option);
  940.         echo imgmap_get_style_element($key, $style, true, true);
  941.     }
  942.     die();
  943. }
  944.  
  945. function imgmap_edit_style() {
  946.     if(current_user_can('manage_options')) {
  947.         $style = array(
  948.             'fillColor' => $_POST['fillColor'],
  949.             'fillOpacity' => $_POST['fillOpacity'],
  950.             'strokeColor' => $_POST['strokeColor'],
  951.             'strokeOpacity' => $_POST['strokeOpacity'],
  952.             'strokeWidth' => $_POST['strokeWidth']
  953.         );
  954.         $id = $_POST['id'];
  955.         $style_option = get_option('imgmap_colors');
  956.         $style_option['colors'][$id] = $style;
  957.        
  958.         update_option('imgmap_colors', $style_option);
  959.         echo imgmap_get_style_element($id, $style, true, true);
  960.     }
  961.     die();
  962. }
  963.  
  964. function imgmap_delete_style() {
  965.     if(current_user_can('manage_options')) {
  966.         $style_option = get_option('imgmap_colors');
  967.         $id = $_POST['id'];
  968.         unset($style_option['colors'][$id]);
  969.         update_option('imgmap_colors', $style_option);
  970.     }
  971.     die();
  972. }
  973.  
  974. function imgmap_hex_to_rgba($hex, $opacity = false) {
  975.    
  976.     if(substr($hex, 0, 1) == '#')
  977.         $hex = substr($hex, 1);
  978.        
  979.     $red = substr($hex, 0, 2);
  980.     $green = substr($hex, 2, 2);
  981.     $blue = substr($hex, 4, 2);
  982.    
  983.     $red = hexdec($red);
  984.     $green = hexdec($green);
  985.     $blue = hexdec($blue);
  986.    
  987.     if(is_numeric($opacity))
  988.         return 'rgba('.$red.', '.$green.', '.$blue.', '.$opacity.')';
  989.     else
  990.         return 'rgb('.$red.', '.$green.', '.$blue.')';
  991. }
  992.  
  993. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement