Guest User

Untitled

a guest
Mar 2nd, 2014
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3. Plugin Name: gw2pcl - Guild Wars 2 Player's Character List
  4. Description: Users can manage their own Guild Wars 2 characters. A list of all characters assigned to one player can be integrated in any post or page. If no user is given as an attribute to the short code all existing characters are printed out.
  5. Usage: [gw2pcl who="<<user_login>>"]
  6. Version: 2.0.2
  7. Author: max.mueller
  8. Based on gw2pcl written by Arne.
  9. License: GPL3
  10. */
  11. global $gw2pcl_database_version;
  12.  
  13. $gw2pcl_database_version = 1;
  14.  
  15. add_action ( 'admin_menu', 'gw2pcl_menu' );
  16. register_activation_hook(__FILE__,'gw2pcl_create_database');
  17. if (get_site_option('gw2pcl_database_version') != gw2pcl_database_version) gw2pcl_create_database();
  18. add_shortcode( 'gw2pcl', 'gw2pcl_shortcode' );
  19. add_action('wp_head', 'gw2pcl_css');
  20. register_uninstall_hook( __FILE__, 'gw2pcl_uninstall' );
  21.  
  22. // If your wordpress installation is configured to german all texts will be translated. Otherwise it'll stay English.
  23. load_plugin_textdomain( 'gw2pcl', false, basename(dirname(__FILE__)) . '/languages/' );
  24.  
  25.  
  26. function gw2pcl_css() {
  27.     echo '<link rel="stylesheet" href="' . plugin_dir_url( __FILE__ ) . 'gw2pcl.css" />' . "\n";
  28. }
  29.  
  30.  
  31. function gw2pcl_menu() {
  32.     add_menu_page( __( 'Manage my Characters', 'gw2pcl' ), __( 'My Chars', 'gw2pcl' ), 'upload_files', 'gw2pclmainmenu', 'gw2pcl_page', plugin_dir_url( __FILE__ ).'pics/gw2.png', '99.000020120924171044' );
  33. }
  34.  
  35.  
  36.  
  37. /**
  38.  *  Displaying Guild Member's Character List on the Frontend
  39.  */
  40.  
  41. function gw2pcl_shortcode( $attributes ) {
  42.     // Allow following atributes, which are optional and have default values (=> 'default value').
  43.     extract( shortcode_atts( array(
  44.         'who' => 'nobody',
  45.     ), $attributes ) );
  46.  
  47.     global $wpdb;
  48.     $gw2pcl_table_name = $wpdb->prefix . 'gw2pcl';
  49.     $output = "";
  50.    
  51.     // IF no user is given as an attribute print all the characters.
  52.     if ( $who == "nobody" ) {
  53.         $chars = $wpdb->get_results( "
  54.             SELECT id, user_id, name, isCommander, picture_file, race, profession, orderCol, crafting1, crafting2, fractals, dungeons, build, teaser
  55.             FROM $gw2pcl_table_name
  56.             ORDER BY user_id ASC, name ASC
  57.         " );
  58.     } else {
  59.         // Predetermine which user_id is assigned to the transmitted user (shortcode attribute "who").
  60.         $current_user_id = $wpdb->get_var( "SELECT id FROM $wpdb->users WHERE user_login = '$who'" );
  61.        
  62.         // Get this specific user's characters.
  63.         $chars = $wpdb->get_results( "
  64.             SELECT id, user_id, name, isCommander, picture_file, race, profession, orderCol, crafting1, crafting2, fractals, dungeons, build, teaser
  65.             FROM $gw2pcl_table_name
  66.             WHERE user_id = $current_user_id
  67.             ORDER BY user_id ASC, name ASC
  68.         " );
  69.     }
  70.  
  71.     $alternate = 1;
  72.     $last_uid = 0;
  73.    
  74.     foreach ( $chars as $char ) {
  75.         if ( $char->user_id != $last_uid) {
  76.             $user = get_user_by('id', $char->user_id);
  77.             $output .= '<div class="gw2user">' . esc_html( $user->display_name ) . '</div>' . "\n";
  78.             $last_uid = $char->user_id;
  79.         }
  80.         $output .= '<div class="gw2row ' . ( $alternate ? 'gw2odd' : 'gw2even' ) . '">' . "\n";
  81.         $output .= '<div class="gw2desc">';
  82.        
  83.         $output .= '<div class="gw2pic">';
  84.         if ( file_exists( $char->picture_file ) ) {
  85.    
  86.             $upload_path = wp_upload_dir();
  87.             $image_sized = image_resize( $char->picture_file, 160, 300, true, null, null, 100 );       
  88.             $image_sized = is_wp_error($image_sized) ? $char->picture_file : $image_sized;
  89.             $image_sized = str_replace( $upload_path['basedir'], $upload_path['baseurl'], $image_sized );  
  90.  
  91.             $output .= '<img src="' . $image_sized . '" alt="' . esc_html( $char->name ) . '" />';
  92.         } else {
  93.             $output .= '<img src="' . plugin_dir_url( __FILE__ ).'pics/nopic.png' . '" alt="' . esc_html( $char->name ) . '" />';
  94.         }
  95.        
  96.         $output .= '</div>' . "\n";
  97.  
  98.         // name and commander badge
  99.         $output .= '<strong><span style="font-size:2em;">' . esc_html($char->name);
  100.         $output .= ' ' . isCommanderFunction($char->name) . '</span></strong><br />';
  101.        
  102.         // race
  103.         $output .= get_race_pic( $char->race ) . ' ' . get_race( $char->race ) . '<br />';
  104.  
  105.         //profession
  106.         $output .= get_profession_pic( $char->profession ) . ' ' . get_profession( $char->profession ) . '<br />';
  107.  
  108.         // order
  109.         if ( $char->orderCol != 0 ) {
  110.             $output .= get_orderCol_pic( $char->orderCol ) . ' ' . get_orderCol( $char->orderCol ) . '<br />';
  111.         }
  112.  
  113.         // crafting
  114.         if ( $char->crafting1 != 0 ) {
  115.             $output .= get_crafting_skill_pic( $char->crafting1 ) . ' ' . get_crafting_skill( $char->crafting1 );
  116.         }
  117.         if ( ( $char->crafting1 != 0 ) and ( $char->crafting2 != 0 ) ) $output .= ' ' . __( 'and', 'gw2pcl' ) . ' ';
  118.         if ( $char->crafting2 != 0 ) {
  119.             $output .= get_crafting_skill_pic( $char->crafting2 ) . ' ' . get_crafting_skill( $char->crafting2 );
  120.         }
  121.         $output .= '<br />';
  122.          
  123.         // fractals      
  124.         if ( $char->fractals != 0 ) {
  125.                     $output .= '<img src="' . plugin_dir_url( __FILE__ ) . 'pics/fractals.jpg" />';
  126.                     $output .= ' ' . __( 'Fractals Level', 'gw2pcl' ) . ': ' . $char->fractals;
  127.                 }
  128.  
  129.         // story mode
  130.         // TODO !!! Code reuse as a function as above. !!!
  131.         if ( !strstr($char->dungeons, "a:0") ) {
  132.                     $output .= '<br />';
  133.                     $output .= __( 'Story Mode Completed', 'gw2pcl' ) . ':<br />';
  134.                     $dungeons = unserialize( $char->dungeons );
  135.                     if ($dungeons['ac'] == 1) $output .= '<img title="' . __( 'Ascalonian Catacombs', 'gw2pcl' ) . '" src="' . plugin_dir_url( __FILE__ ) . 'pics/dungeons/ac.png" class="gw2pcl_dungeonpic" /> ';
  136.                     if ($dungeons['cm'] == 1) $output .= '<img title="' . __( 'Caudecus\'s Manor', 'gw2pcl' ) . '" src="' . plugin_dir_url( __FILE__ ) . 'pics/dungeons/cm.png" class="gw2pcl_dungeonpic" /> ';
  137.                     if ($dungeons['ta'] == 1) $output .= '<img title="' . __( 'Twilight Arbor', 'gw2pcl' ) . '" src="' . plugin_dir_url( __FILE__ ) . 'pics/dungeons/ta.png" class="gw2pcl_dungeonpic" /> ';
  138.                     if ($dungeons['se'] == 1) $output .= '<img title="' . __( 'Sorrow\'s Embrace', 'gw2pcl' ) . '" src="' . plugin_dir_url( __FILE__ ) . 'pics/dungeons/se.png" class="gw2pcl_dungeonpic" /> ';
  139.                     if ($dungeons['cof'] == 1) $output .= '<img title="' . __( 'Citadel of Flame', 'gw2pcl' ) . '" src="' . plugin_dir_url( __FILE__ ) . 'pics/dungeons/cof.png" class="gw2pcl_dungeonpic" /> ';
  140.                     if ($dungeons['hotw'] == 1) $output .= '<img title="' . __( 'Honor of the Waves', 'gw2pcl' ) . '" src="' . plugin_dir_url( __FILE__ ) . 'pics/dungeons/hotw.png" class="gw2pcl_dungeonpic" /> ';
  141.                     if ($dungeons['coe'] == 1) $output .= '<img title="' . __( 'Crucible of Eternity', 'gw2pcl' ) . '" src="' . plugin_dir_url( __FILE__ ) . 'pics/dungeons/coe.png" class="gw2pcl_dungeonpic" /> ';
  142.                     if ($dungeons['arah'] == 1) $output .= '<img title="' . __( 'Arah', 'gw2pcl' ) . '" src="' . plugin_dir_url( __FILE__ ) . 'pics/dungeons/arah.png" class="gw2pcl_dungeonpic" /> ';
  143.                 }
  144.  
  145.         // build
  146.         if ( strlen( $char->build ) > 2 ) {
  147.             $output .= '<br />'."\n".'<img title="' . __( 'Build', 'gw2pcl' ) . '" src="' . plugin_dir_url( __FILE__ ) . 'pics/build.png" class="gw2pcl_dungeonpic" /> ' '<em>:' . esc_html( $char->build ) . '</em>';
  148.         }
  149.  
  150.         // teaser
  151.         if ( strlen( $char->teaser ) > 2 ) {
  152.             $output .= '<br />'."\n".'<em>ยป' . esc_html( $char->teaser ) . 'ยซ</em>';
  153.         }
  154.  
  155.         $output .= '</div>' . "\n";
  156.                 $output .= '<div style="clear:both;"></div>';
  157.         $output .= '</div>' . "\n";
  158.         $alternate = 1 - $alternate;
  159.     }
  160.    
  161.     return $output;
  162. }
  163.  
  164.  
  165.  
  166. /**
  167.  *  DISPLAYS PAGE IN BACKEND AND DOES THE DB-EDITING
  168.  */
  169.  
  170.  
  171. function gw2pcl_page() {
  172.     global $wpdb;
  173.     $table_name = $wpdb->prefix . 'gw2pcl';
  174.     $cur_user = get_userdata( get_current_user_id() );
  175.     $user_level = $cur_user->user_level;
  176.  
  177.     // Ensuring I'm either at least an editor or the owner of this char
  178.     $where_two = "";
  179.     if ( $user_level < 7 ) {
  180.         $where_two = ' AND user_id = '.get_current_user_id();
  181.     }
  182.    
  183.  
  184.     /**
  185.      *  ADD NEW CHAR TO DATABASE
  186.      */
  187.  
  188.     if ( isset( $_POST['gw2pcl_add'] ) ) {
  189.  
  190.         if ( ! isset( $_POST['_gw2pcl_nonce'] ) || ! wp_verify_nonce( $_POST['_gw2pcl_nonce'], 'gw2pcl_nonce' ) )   return;
  191.        
  192.         $name       = stripslashes( $_POST['name'] );
  193.         $isCommander= intval      ( $_POST['isCommander'] );
  194.         $build      = addslashes  ( $_POST['build'] );
  195.         $teaser     = stripslashes( $_POST['teaser'] );
  196.         $race       = intval      ( $_POST['race'] );
  197.         $profession = intval      ( $_POST['profession'] );
  198.         $orderCol   = intval      ( $_POST['orderCol'] );
  199.         $crafting1  = intval      ( $_POST['crafting1'] );
  200.         $crafting2  = intval      ( $_POST['crafting2'] );
  201.         $fractals   = intval      ( $_POST['fractals'] );
  202.                 if (($fractals > 999) or ($fractals < 2)) $fractals = 0;
  203.                
  204.                 $dungeons = array();
  205.                 if ( intval( $_POST['ac'] ) == 1)   $dungeons['ac'] = 1;
  206.                 if ( intval( $_POST['cm'] ) == 1)   $dungeons['cm'] = 1;
  207.                 if ( intval( $_POST['ta'] ) == 1)   $dungeons['ta'] = 1;
  208.                 if ( intval( $_POST['se'] ) == 1)   $dungeons['se'] = 1;
  209.                 if ( intval( $_POST['cof'] ) == 1)  $dungeons['cof'] = 1;
  210.                 if ( intval( $_POST['hotw'] ) == 1) $dungeons['hotw'] = 1;
  211.                 if ( intval( $_POST['coe'] ) == 1)  $dungeons['coe'] = 1;
  212.                 if ( intval( $_POST['arah'] ) == 1) $dungeons['arah'] = 1;
  213.        
  214.         if ($name == "") {
  215.             echo '<div id="message" class="updated fade"><p><strong>' . __( 'Name can\'t be left blank.', 'gw2pcl' ) . '</strong></p></div>';
  216.         } else {
  217.            
  218.             // Only JPG and PNG. Now with GIFs.
  219.             if ( ! empty( $_FILES['picture']['name'] ) ) {
  220.                 $mimes = array(
  221.                     'jpg|jpeg' => 'image/jpeg',
  222.                     'png' => 'image/png'
  223.                     'gif' => 'image/gif'
  224.                 );
  225.                 $charpic = wp_handle_upload( $_FILES['picture'], array( 'mimes' => $mimes, 'test_form' => false ) );
  226.                 if ( ! empty ( $charpic['file'] ) ) {
  227.                     $picture_file = $charpic['file'];
  228.                 }
  229.             }
  230.        
  231.             $wpdb->query(
  232.                 $wpdb->prepare("
  233.                     INSERT INTO $table_name
  234.                     ( name, isCommander, race, profession, orderCol, crafting1, crafting2, picture_file, teaser, build,  fractals, dungeons, user_id )
  235.                     VALUES ( %s, %d, %d, %d, %d, %d, %d, %s, %s, %s, %d, %s, %d )
  236.                 ", $name, $isCommander, $race, $profession, $orderCol, $crafting1, $crafting2, $picture_file, $build, $teaser, $fractals, serialize($dungeons), get_current_user_id() )
  237.             );
  238.        
  239.             echo '<div id="message" class="updated fade"><p><strong>' . __( 'Character added.', 'gw2pcl' ) . '</strong></p></div>';
  240.         }
  241.     }
  242.    
  243.    
  244.     /**
  245.      *  MODIFY ENTRY
  246.      */
  247.        
  248.     if ( isset( $_POST['gw2pcl_change'] ) ) {
  249.  
  250.         if ( ! isset( $_POST['_gw2pcl_nonce'] ) || ! wp_verify_nonce( $_POST['_gw2pcl_nonce'], 'gw2pcl_nonce' ) )   return;
  251.        
  252.         $name       = stripslashes( $_POST['name'] );
  253.         $isCommander= intval      ( $_POST['isCommander'] );
  254.         $build      = addslashes  ( $_POST['build'] );
  255.         $teaser     = stripslashes( $_POST['teaser'] );
  256.         $id         = intval      ( $_POST['gw2pcl_id'] );
  257.         $race       = intval      ( $_POST['race'] );
  258.         $profession = intval      ( $_POST['profession'] );
  259.         $orderCol   = intval      ( $_POST['orderCol'] );
  260.         $crafting1  = intval      ( $_POST['crafting1'] );
  261.         $crafting2  = intval      ( $_POST['crafting2'] );
  262.         $fractals   = intval      ( $_POST['fractals'] );
  263.                 if (($fractals > 999) or ($fractals < 2)) $fractals = 0;
  264.         $delete     = intval      ( $_POST['deleteoldpic'] or 0 );
  265.  
  266.                 $dungeons = array();
  267.                 if ( intval( $_POST['ac'] ) == 1)   $dungeons['ac'] = 1;
  268.                 if ( intval( $_POST['cm'] ) == 1)   $dungeons['cm'] = 1;
  269.                 if ( intval( $_POST['ta'] ) == 1)   $dungeons['ta'] = 1;
  270.                 if ( intval( $_POST['se'] ) == 1)   $dungeons['se'] = 1;
  271.                 if ( intval( $_POST['cof'] ) == 1)  $dungeons['cof'] = 1;
  272.                 if ( intval( $_POST['hotw'] ) == 1) $dungeons['hotw'] = 1;
  273.                 if ( intval( $_POST['coe'] ) == 1)  $dungeons['coe'] = 1;
  274.                 if ( intval( $_POST['arah'] ) == 1) $dungeons['arah'] = 1;
  275.  
  276.                 // Am I allowed to do that?
  277.  
  278.         if ( $id > 0 ) {
  279.             $entry = $wpdb->get_row('SELECT * FROM ' . $table_name . ' WHERE id = '.$id . $where_two );
  280.             if ($entry != null) {
  281.                 $picture_file = $entry->picture_file;
  282.             } else {
  283.                 wp_die( 'No' );
  284.             }
  285.         } else {
  286.             wp_die( 'No' );
  287.         }
  288.        
  289.         // Then do it
  290.         // unnice code reuse
  291.  
  292.         if ($name == "") {
  293.             echo '<div id="message" class="updated fade"><p><strong>' . __( 'Name can\'t be left blank.', 'gw2pcl' ) . '</strong></p></div>';
  294.         } else {
  295.        
  296.             // Delete old images if a new one was uploaded
  297.             // or the user wanted the old one to be deleted
  298.            
  299.             if ( ( ! empty( $_FILES['picture']['name'] ) ) or ( $delete ) ) {
  300.                     @unlink( $entry->picture_file );
  301.                     $otherpath = pathinfo( $entry->picture_file );
  302.                     @unlink( $otherpath['dirname'] . '/' . $otherpath['filename'] . '-150x200.' . $otherpath['extension'] );
  303.                     $picture_file = "";
  304.             }
  305.        
  306.             if ( ! empty( $_FILES['picture']['name'] ) ) {
  307.                 $mimes = array(
  308.                     'jpg|jpeg' => 'image/jpeg',
  309.                     'png' => 'image/png'
  310.                 );
  311.                 $charpic = wp_handle_upload( $_FILES['picture'], array( 'mimes' => $mimes, 'test_form' => false ) );
  312.                 if ( ! empty ( $charpic['file'] ) ) {
  313.                     $picture_file = $charpic['file'];
  314.                 }
  315.             }
  316.        
  317.             $wpdb->query(
  318.                 $wpdb->prepare("
  319.                     UPDATE $table_name SET
  320.                     name = %s,
  321.                     isCommander = %d,
  322.                     race = %d,
  323.                     profession = %d,
  324.                     orderCol = %d,
  325.                     crafting1 = %d,
  326.                     crafting2 = %d,
  327.                     picture_file = %s,
  328.                     build = %s,
  329.                     teaser = %s,
  330.                                 fractals = %d,
  331.                                 dungeons = %s
  332.                     WHERE id = %d
  333.                 ", $name, $isCommander, $race, $profession, $orderCol, $crafting1, $crafting2, $picture_file, $build, $teaser, $fractals, serialize($dungeons), $id)
  334.             );
  335.        
  336.             echo '<div id="message" class="updated fade"><p><strong>' . __( 'Character edited.', 'gw2pcl' ) . '</strong></p></div>';
  337.         }
  338.     }
  339.    
  340.     // Default Values
  341.     $name       = "";
  342.     $isCommander= 0;
  343.     $build      = "";
  344.     $teaser     = "";
  345.     $race       = 3;
  346.     $profession = 6;
  347.     $orderCol   = 0;
  348.     $crafting1  = 0;
  349.     $crafting2  = 0;
  350.         $fractals   = 0;
  351.         $dungeons   = array();
  352.    
  353.     /**
  354.      *  Delete selected
  355.      */
  356.      
  357.     if ( isset( $_POST['gw2pcl_delete'] ) ) {
  358.         $delete = $_POST["post"];
  359.        
  360.         if ( sizeof( $delete ) < 1 ) {
  361.        
  362.             echo '<div id="message" class="updated fade"><p><strong>' . __( 'Nothing to delete.', 'gw2pcl' ) . '</strong></p></div>';
  363.        
  364.         } else {
  365.        
  366.             $delete_string = "";
  367.             foreach ($delete as $todelete) {
  368.                 $delete_string = $delete_string . intval($todelete) . ', ';
  369.             }
  370.             $delete_string = substr($delete_string, 0, strlen($delete_string) - 2);
  371.            
  372.             // Deleting Images and resized Images
  373.             $file_list = $wpdb->get_results( 'SELECT user_id, picture_file FROM ' . $table_name . ' WHERE id IN ( ' . $delete_string  . ' )' . $where_two );
  374.                    
  375.             foreach ( $file_list as $image_file ) {
  376.                 if ( $image_file->picture_file ) {
  377.                     @unlink( $image_file->picture_file );
  378.                     $otherpath = pathinfo( $image_file->picture_file );
  379.                     @unlink( $otherpath['dirname'] . '/' . $otherpath['filename'] . '-150x200.' . $otherpath['extension'] );
  380.                 }
  381.             }
  382.            
  383.             $wpdb->query( 'DELETE FROM ' . $table_name . ' WHERE id IN ( ' . $delete_string  . ' )' . $where_two );
  384.            
  385.             echo '<div id="message" class="updated fade"><p><strong>' . __( 'Deleted selected Characters.', 'gw2pcl' ) . '</strong></p></div>';
  386.            
  387.         }
  388.     }
  389.    
  390.    
  391.     /**
  392.      *  DISPLAY LIST OF CHARS IN THE BACKEND
  393.      */
  394.    
  395.     ?>
  396.     <h1><?php __( 'Manage my Characters', 'gw2pcl' ); ?></h1>
  397.     <p><?php _e( 'On this page you can enter all your characters for the list of all guild members.', 'gw2pcl' ); ?></p>
  398. <?php
  399.     if ( $user_level >= 7 ) {
  400.         echo '<p>' . sprintf( __( 'To insert the list of all characters on a page or an article enter the shortcode %s.', 'gw2pcl' ), '<code>[gw2pcl]</code>' ) . '</p>' . "\n";
  401.         echo '<p>' . sprintf( __( 'To insert the list of all characters assigned to a certain user on a page or an article enter the shortcode %s.', 'gw2pcl' ), '<code>[gw2pcl who=\'user_login\']</code>' ) . '</p>' . "\n";
  402.         if ( isset( $_POST['gw2pcl_view_all'] ) ) $_SESSION["gw2pcl-viewall"] = true;
  403.         if ( isset( $_POST['gw2pcl_view_mine'] ) ) $_SESSION["gw2pcl-viewall"] = false;
  404.         if ( $_SESSION["gw2pcl-viewall"] == false) {
  405.             echo '<form method="post" action=""><p><input type="submit" name="gw2pcl_view_all" id="gw2pcl_view_all" value="' . __( 'view all', 'gw2pcl' ) . '" /></p></form>'."\n";
  406.         } else {
  407.             echo '<form method="post" action=""><p><input type="submit" name="gw2pcl_view_mine" id="gw2pcl_view_mine" value="' . __( 'view just mine', 'gw2pcl' ) . '" /></p></form>'."\n";
  408.         }
  409.     }
  410. ?>
  411.     <form method="post" action="">
  412.     <table class="wp-list-table widefat" cellspacing="0">
  413.         <thead>
  414.             <tr>
  415.                 <th scope='col' id='cb' class='manage-column column-cb check-column'  style=""><input type="checkbox" /></th>
  416.                 <th><?php _e( 'Picture', 'gw2pcl' ); ?></th>
  417.                 <th><?php _e( 'Details', 'gw2pcl' ); ?></th>
  418.             </tr>
  419.         </thead>
  420.         <tbody id="the-list">
  421.     <?php
  422.    
  423.     $chars = $wpdb->get_results( "
  424.         SELECT *
  425.         FROM $table_name
  426.         " . ((($user_level >= 7) and ($_SESSION["gw2pcl-viewall"])) ? "" : "WHERE user_id = " . get_current_user_id() ) . "
  427.         ORDER BY name
  428.     " );
  429.    
  430.     $alternate = 1;
  431.    
  432.     foreach ( $chars as $char ) {
  433.         echo '<tr' . ( $alternate ? ' class="alternate"' : '' ) . '>' . "\n";
  434.         echo '<th scope="row" class="check-column"><input type="checkbox" name="post[]" value="' . $char->id . '" /></th>' . "\n";
  435.         echo '<td>';
  436.        
  437.         if ( file_exists( $char->picture_file ) ) {
  438.    
  439.             $upload_path = wp_upload_dir();
  440.             $image_sized = image_resize( $char->picture_file, 150, 200, true, null, null, 100 );       
  441.             $image_sized = is_wp_error($image_sized) ? $char->picture_file : $image_sized;
  442.             $image_sized = str_replace( $upload_path['basedir'], $upload_path['baseurl'], $image_sized );  
  443.  
  444.             echo '<img src="' . $image_sized . '" alt="' . esc_html( $char->name ) . '" />';
  445.         } else {
  446.             echo '<img src="' . plugin_dir_url( __FILE__ ).'pics/nopic.png' . '" alt="' . esc_html( $char->name ) . '" />';
  447.         }
  448.        
  449.         echo '</td>' . "\n";
  450.         echo '<td>';
  451.         echo '<strong><span style="font-size:2em;">' . esc_html($char->name);
  452.         echo ' ' . isCommanderFunction($char->name) . '</span></strong><br />';
  453.  
  454.         $user = (get_user_meta( $char->user_id, 'nickname', true ));
  455.         if ( $user == "" ) {
  456.             $user = '<strong><span style="color:#f00;">' . __( 'User doesn\'t exist!', 'gw2pcl' ) . '</span></strong>';
  457.         } else {
  458.             $user = '<em>' . $user . '</em>';
  459.         }
  460.         echo $user . '<br />';
  461.        
  462.         echo get_race( $char->race );
  463.        
  464.         echo '<br />';
  465.        
  466.         echo get_profession( $char->profession );
  467.  
  468.         echo '<br />';
  469.        
  470.         echo get_orderCol( $char->orderCol );
  471.  
  472.         echo '<br />';
  473.        
  474.         if ( $char->crafting1 != 0 ) echo get_crafting_skill( $char->crafting1 );
  475.         if ( ( $char->crafting1 != 0 ) and ( $char->crafting2 != 0 ) ) echo ' ' . __( 'and', 'gw2pcl' ) . ' ';
  476.         if ( $char->crafting2 != 0 ) echo get_crafting_skill( $char->crafting2 );
  477.         if ( ( $char->crafting1 == 0 ) and ( $char->crafting2 == 0 ) ) _e( 'no crafting skills', 'gw2pcl' );
  478.  
  479.         echo '<br />';
  480.                
  481.                 if ( ( $char->fractals) == 0) {
  482.                     _e( 'hasn\'t been doing fractals so far', 'gw2pcl' );
  483.                 } else {
  484.                     _e( 'Can enter fractals level', 'gw2pcl' );
  485.                     echo ': ' . $char->fractals;
  486.                 }
  487.  
  488.         if ( strlen( $char->build ) > 2 ) {
  489.             echo '<br />'."\n".'<em>' . esc_html( $char->build ) . '</em>';
  490.         }
  491.  
  492.         if ( strlen( $char->teaser ) > 2 ) {
  493.             echo '<br />'."\n".'<em>ยป' . esc_html( $char->teaser ) . 'ยซ</em>';
  494.         }
  495.        
  496.         echo '<form method="POST" action="#edit"><p><input type="submit" name="gw2pcl_edit" id="gw2pcl_edit" value="' . __( 'edit', 'gw2pcl' ) . '" /><input type="hidden" name="gw2pcl_id" value="' . $char->id . '" /></p></form>' . "\n";
  497.         echo '</td>' . "\n";
  498.         echo '</tr>' . "\n";
  499.         $alternate = 1 - $alternate;
  500.     }
  501.    
  502.     ?>
  503.         </tbody>
  504.     </table>
  505.     <p><input type="submit" name="gw2pcl_delete" id="gw2pcl_delete" value="<?php _e( 'Delete selected', 'gw2pcl' ); ?>" /></p>
  506.     </form>
  507. <?php
  508.  
  509.  
  510.     /**
  511.      *  FORM FOR ADDING AND MODIFYING NEW CHARS
  512.      *  Man, that's ugly. I'm not used to do that on my own.
  513.      */
  514.      
  515. ?>
  516.     <h2><?php
  517.         if ( isset( $_POST['gw2pcl_edit'] ) ) {
  518.             _e( 'Edit Character', 'gw2pcl' );
  519.             $id = intval( $_POST['gw2pcl_id'] );
  520.             if ( $id > 0 ) {
  521.                 $entry = $wpdb->get_row('SELECT * FROM ' . $table_name . ' WHERE id = '.$id . $where_two );
  522.                 if ($entry != null) {
  523.                     $name       = $entry->name;
  524.                     $isCommander= $entry->isCommander;
  525.                     $race       = $entry->race;
  526.                     $build      = $entry->build;
  527.                     $teaser     = $entry->teaser;
  528.                     $profession = $entry->profession;
  529.                     $orderCol   = $entry->orderCol;
  530.                     $crafting1  = $entry->crafting1;
  531.                     $crafting2  = $entry->crafting2;
  532.                                         $fractals   = $entry->fractals;
  533.                                         $dungeons   = unserialize( $entry->dungeons );
  534.                 }
  535.             }
  536.         } else {
  537.             _e( 'Add New Character', 'gw2pcl' );
  538.         }
  539.     ?></h2>
  540.  
  541.     <form method="post" enctype="multipart/form-data" action="">
  542. <?php wp_nonce_field( 'gw2pcl_nonce', '_gw2pcl_nonce', false ); ?>
  543.     <table class="form-table">
  544.     <tr valign="top">
  545.     <th scope="row"><label for="name"><?php _e( 'Name', 'gw2pcl' ); ?>:</label></th>
  546.     <td><input name="name" type="text" id="name" class="regular-text" value="<?php echo $name; ?>" /></td>
  547.     <td rowspan="0" width="170" valign="top">
  548.     <?php
  549.         if ( isset( $_POST['gw2pcl_edit'] ) ) {
  550.        
  551.             // When editing a char display the image, too.
  552.             if ( file_exists( $entry->picture_file ) ) {
  553.        
  554.                 $upload_path = wp_upload_dir();
  555.                 $image_sized = image_resize( $entry->picture_file, 150, 200, true, null, null, 100 );      
  556.                 $image_sized = is_wp_error($image_sized) ? $entry->picture_file : $image_sized;
  557.                 $image_sized = str_replace( $upload_path['basedir'], $upload_path['baseurl'], $image_sized );  
  558.  
  559.                 echo '<img src="' . $image_sized . '" alt="' . esc_html( $entry->name ) . '" />';
  560.             } else {
  561.                 echo '<img src="' . plugin_dir_url( __FILE__ ).'pics/nopic.png' . '" alt="' . esc_html( $entry->name ) . '" />';
  562.             }
  563.  
  564.         }
  565.     ?>
  566.     </td>
  567.  
  568.     <?php
  569.     //Choose if character is a commander or not.
  570.     ?>
  571.     <tr valign="top">
  572.     <th scope="row"><?php _e( 'Commander', 'gw2pcl' ); ?>:</th>
  573.         <td>
  574.         <input name="isCommander" type="checkbox" id="isCommander" class="checkbox" value="1" <?php if ($isCommander == 1) echo 'checked="checked" '; ?>/><label for="isCommander"> <?php _e( 'Char is a commander.', 'gw2pcl' ); ?></label><br />
  575.         </td>
  576.     </tr>
  577.  
  578.  
  579.     <tr valign="top">
  580.     <th scope="row"><label for="race"><?php _e( 'Race', 'gw2pcl' ); ?>:</label></th>
  581.     <td><select name="race" id="race">
  582.     <?php
  583.         for ( $i=1;$i<=5;$i++ ) {
  584.             echo '<option' . ( $i == $race ? ' selected="selected"' : '' ) . ' value="' . $i . '">' . get_race( $i ) . '</option>' . "\n";
  585.         }
  586.     ?></select></td>
  587.     </tr>
  588.     <tr valign="top">
  589.     <th scope="row"><label for="profession"><?php _e( 'Profession', 'gw2pcl' ); ?>:</label></th>
  590.     <td><select name="profession" id="profession">
  591.     <?php
  592.         for ( $i=1;$i<=8;$i++ ) {
  593.             echo '<option' . ( $i == $profession ? ' selected="selected"' : '' ) . ' value="' . $i . '">' . get_profession( $i ) . '</option>' . "\n";
  594.         }
  595.     ?></select></td>
  596.     </tr>
  597.     <tr valign="top">
  598.     <th scope="row"><label for="orderCol"><?php _e( 'Order', 'gw2pcl' ); ?>:</label></th>
  599.     <td><select name="orderCol" id="orderCol">
  600.     <?php
  601.         for ( $i=0;$i<=3;$i++ ) {
  602.             echo '<option' . ( $i == $orderCol ? ' selected="selected"' : '' ) . ' value="' . $i . '">' . get_orderCol( $i ) . '</option>' . "\n";
  603.         }
  604.     ?></select></td>
  605.     </tr>
  606.     <tr valign="top">
  607.     <th scope="row"><label for="crafting1"><?php _e( 'First Crafting Skill', 'gw2pcl' ); ?>:</label></th>
  608.     <td><select name="crafting1" id="crafting1">
  609.     <?php
  610.         for ( $i=0;$i<=8;$i++ ) {
  611.             echo '<option' . ( $i == $crafting1 ? ' selected="selected"' : '' ) . ' value="' . $i . '">' . get_crafting_skill( $i ) . '</option>' . "\n";
  612.         }
  613.     ?></select></td>
  614.     </tr>
  615.     <tr valign="top">
  616.     <th scope="row"><label for="crafting2"><?php _e( 'Second Crafting Skill', 'gw2pcl' ); ?>:</label></th>
  617.     <td><select name="crafting2" id="crafting2">
  618.     <?php
  619.         for ( $i=0;$i<=8;$i++ ) {
  620.             echo '<option' . ( $i == $crafting2 ? ' selected="selected"' : '' ) . ' value="' . $i . '">' . get_crafting_skill( $i ) . '</option>' . "\n";
  621.         }
  622.     ?></select></td>
  623.     </tr>
  624.     <tr valign="top">
  625.     <th scope="row"><label for="fractals"><?php _e( 'Ready to do fractals level', 'gw2pcl' ); ?>:</label></th>
  626.     <td><input name="fractals" type="text" id="fractals" class="regular-text" value="<?php echo $fractals; ?>" /></td>
  627.     </tr>
  628.     <tr valign="top">
  629.     <th scope="row"><?php _e( 'Story Mode Completed', 'gw2pcl' ); ?>:</th>
  630.         <td>
  631.         <input name="ac" type="checkbox" id="ac" class="checkbox" value="1" <?php if ($dungeons['ac'] == 1) echo 'checked="checked" '; ?>/><label for="ac"> <?php _e( 'Ascalonian Catacombs', 'gw2pcl' ); ?></label><br />
  632.         <input name="cm" type="checkbox" id="cm" class="checkbox" value="1" <?php if ($dungeons['cm'] == 1) echo 'checked="checked" '; ?>/><label for="cm"> <?php _e( 'Caudecus\'s Manor', 'gw2pcl' ); ?></label><br />
  633.         <input name="ta" type="checkbox" id="ta" class="checkbox" value="1" <?php if ($dungeons['ta'] == 1) echo 'checked="checked" '; ?>/><label for="ta"> <?php _e( 'Twilight Arbor', 'gw2pcl' ); ?></label><br />
  634.         <input name="se" type="checkbox" id="se" class="checkbox" value="1" <?php if ($dungeons['se'] == 1) echo 'checked="checked" '; ?>/><label for="se"> <?php _e( 'Sorrow\'s Embrace', 'gw2pcl' ); ?></label><br />
  635.         <input name="cof" type="checkbox" id="cof" class="checkbox" value="1" <?php if ($dungeons['cof'] == 1) echo 'checked="checked" '; ?>/><label for="cof"> <?php _e( 'Citadel of Flame', 'gw2pcl' ); ?></label><br />
  636.         <input name="hotw" type="checkbox" id="hotw class="checkbox" value="1" <?php if ($dungeons['hotw'] == 1) echo 'checked="checked" '; ?>/><label for="hotw"> <?php _e( 'Honor of the Waves', 'gw2pcl' ); ?></label><br />
  637.         <input name="coe" type="checkbox" id="coe" class="checkbox" value="1" <?php if ($dungeons['coe'] == 1) echo 'checked="checked" '; ?>/><label for="coe"> <?php _e( 'Crucible of Eternity', 'gw2pcl' ); ?></label><br />
  638.         <input name="arah" type="checkbox" id="arah" class="checkbox" value="1" <?php if ($dungeons['arah'] == 1) echo 'checked="checked" '; ?>/><label for="arah"> <?php _e( 'Arah', 'gw2pcl' ); ?></label>
  639.         </td>
  640.     </tr>
  641.     <tr valign="top">
  642.     <th scope="row"><label for="picture"><?php _e( 'Upload Picture', 'gw2pcl' ); ?>:</label></th>
  643.     <td><input type="file" name="picture" id="picture" /></td>
  644.     </tr>
  645. <?php
  646.     if ( isset( $_POST['gw2pcl_edit'] ) ) {
  647.         echo '<tr>' . "\n";
  648.         echo '<td>' . "\n";
  649.         echo '<input type="checkbox" name="deleteoldpic" id="deleteoldpic" value="1" /><label for="deleteoldpic"> ' . _e( 'delete old picture', 'gw2pcl' ) . '</label>' . "\n";
  650.         echo '</td>' . "\n";
  651.         echo '<td>' . "\n";
  652.         echo '</td>' . "\n";
  653.         echo '</tr>' . "\n";
  654.     }
  655. ?>
  656.     <tr valign="top">
  657.     <th scope="row"><label for="build"><?php _e( 'Build Link', 'gw2pcl' ); ?>:</label></th>
  658.     <td><input name="build" type="text" id="build" class="regular-text" value="<?php echo $build; ?>" /></td>
  659.     </tr>
  660.     </table>
  661.  
  662.     <tr valign="top">
  663.     <th scope="row"><label for="teaser"><?php _e( 'Short Phrase', 'gw2pcl' ); ?>:</label></th>
  664.     <td><input name="teaser" type="text" id="teaser" class="regular-text" value="<?php echo $teaser; ?>" /></td>
  665.     </tr>
  666.     </table>
  667. <?php
  668.     if ( isset( $_POST['gw2pcl_edit'] ) ) {
  669.         echo '<input type="hidden" name="gw2pcl_id" value="' . $id . '" />' . "\n";
  670.         echo '<p><input type="submit" name="gw2pcl_change" value="' . __( 'Change', 'gw2pcl' ) . '" /></p>' . "\n";
  671.     } else {
  672.         echo '<p><input type="submit" name="gw2pcl_add" value="' . __( 'Save', 'gw2pcl' ) . '" /></p>' . "\n";
  673.     }
  674. ?>
  675.         <a name="edit"></a>
  676.     </form>
  677.  
  678.     <?php
  679. }
  680.  
  681.  
  682. /**
  683.  *  Creating our own database table.
  684.  */
  685.  
  686. function gw2pcl_create_database() {
  687.     global $wpdb;
  688.     global $gw2pcl_database_version;
  689.    
  690.     $table_name = $wpdb->prefix . 'gw2pcl';
  691.  
  692.     $sql = "CREATE TABLE $table_name (
  693.         id mediumint(9) NOT NULL AUTO_INCREMENT,
  694.         user_id bigint(20) unsigned NOT NULL DEFAULT '0',
  695.         name VARCHAR(255) DEFAULT '' NOT NULL,
  696.         build VARCHAR(255) DEFAULT '' NOT NULL,
  697.         teaser VARCHAR(255) DEFAULT '' NOT NULL,
  698.         picture_file VARCHAR(255) DEFAULT '' NOT NULL,
  699.         race int(11) NOT NULL DEFAULT '0',
  700.         profession int(11) NOT NULL DEFAULT '0',
  701.         orderCol int(11) NOT NULL DEFAULT '0',
  702.         crafting1 int(11) NOT NULL DEFAULT '0',
  703.         crafting2 int(11) NOT NULL DEFAULT '0',
  704.         fractals int(11) NOT NULL DEFAULT '0',
  705.         dungeons TEXT NOT NULL DEFAULT '',
  706.         isCommander tinyint(1) NOT NULL DEFAULT '0',
  707.         UNIQUE KEY id (id)
  708.     );";
  709.    
  710.     require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
  711.     dbDelta($sql);
  712.    
  713.     add_option("gw2pcl_database_version", $gw2pcl_database_version);
  714. }
  715.  
  716.  
  717. /**
  718.  * Uninstall: delete files and database
  719.  */
  720.  
  721. function gw2pcl_uninstall() {
  722.     global $wpdb;
  723.     $table_name = $wpdb->prefix . 'gw2pcl';
  724.    
  725.     $file_list = $wpdb->get_results( '
  726.         SELECT id, picture_file
  727.         FROM $table_name
  728.         WHERE NOT picture_file = ""
  729.     ' );
  730.  
  731.     foreach ( $file_list as $image_file ) {
  732.         @unlink( $image_file->picture_file );
  733.         $otherpath = pathinfo( $image_file->picture_file );
  734.         @unlink( $otherpath['dirname'] . '/' . $otherpath['filename'] . '-150x200.' . $otherpath['extension'] );
  735.     }
  736.    
  737.     $wpdb->query(  'DROP TABLE ' . $table_name );
  738. }
  739.  
  740. /**
  741.  *  HELPER FUNCTIONS
  742.  *  HINT: User strtolower after code outsourcing into it's own files.
  743.  */
  744.  
  745. // Define wether the current char is a commander or not and display the commander icon if he is.
  746. function isCommanderFunction($charName) {
  747.     global $wpdb;
  748.     $table_name = $wpdb->prefix . 'gw2pcl';
  749.     $isCommander = $wpdb->get_var( "SELECT isCommander FROM $table_name WHERE name = '$charName'" );
  750.    
  751.     if ( $isCommander == '1' )
  752.         return (' <img src="' . plugin_dir_url( __FILE__ ) . 'pics/commander.png" alt="commander" />');
  753. }
  754.  
  755. function get_race( $id ) {
  756.     switch ( $id ) {
  757.         case 1:
  758.             return( __( 'Asura', 'gw2pcl' ) );
  759.             break;
  760.         case 2:
  761.             return( __( 'Charr', 'gw2pcl' ) );
  762.             break;
  763.         case 3:
  764.             return( __( 'Human', 'gw2pcl' ) );
  765.             break;
  766.         case 4:
  767.             return( __( 'Norn', 'gw2pcl' ) );
  768.             break;
  769.         case 5:
  770.             return( __( 'Sylvari', 'gw2pcl' ) );
  771.             break;
  772.     }
  773.     return( __( 'unknown race', 'gw2pcl' ) );
  774. }
  775.  
  776. function get_race_pic( $id ) {
  777.     $pic = "";
  778.     switch ( $id ) {
  779.         case 1:
  780.             $pic .= 'asura';
  781.             break;
  782.         case 2:
  783.             $pic .= 'charr';
  784.             break;
  785.         case 3:
  786.             $pic .= 'human';
  787.             break;
  788.         case 4:
  789.             $pic .= 'norn';
  790.             break;
  791.         case 5:
  792.             $pic .= 'sylvari';
  793.             break;
  794.     }
  795.     return ('<img src="' . plugin_dir_url( __FILE__ ) . 'pics/' . $pic . '.png" alt="' . get_race( $id ) . '" />');
  796. }
  797.  
  798. function get_profession( $id ) {
  799.     switch ( $id ) {
  800.         case 1:
  801.             return( __( 'Guardian', 'gw2pcl' ) );
  802.             break;
  803.         case 2:
  804.             return( __( 'Warrior', 'gw2pcl' ) );
  805.             break;
  806.         case 3:
  807.             return( __( 'Engineer', 'gw2pcl' ) );
  808.             break;
  809.         case 4:
  810.             return( __( 'Ranger', 'gw2pcl' ) );
  811.             break;
  812.         case 5:
  813.             return( __( 'Thief', 'gw2pcl' ) );
  814.             break;
  815.         case 6:
  816.             return( __( 'Elementalist', 'gw2pcl' ) );
  817.             break;
  818.         case 7:
  819.             return( __( 'Mesmer', 'gw2pcl' ) );
  820.             break;
  821.         case 8:
  822.             return( __( 'Necromancer', 'gw2pcl' ) );
  823.             break;
  824.     }
  825.     return( __( 'unknown profession', 'gw2pcl' ) );
  826. }
  827.  
  828. function get_profession_pic( $id ) {
  829.     $pic = "";
  830.     switch ( $id ) {
  831.         case 1:
  832.             $pic .= 'guardian';
  833.             break;
  834.         case 2:
  835.             $pic .= 'warrior';
  836.             break;
  837.         case 3:
  838.             $pic .= 'engineer';
  839.             break;
  840.         case 4:
  841.             $pic .= 'ranger';
  842.             break;
  843.         case 5:
  844.             $pic .= 'thief';
  845.             break;
  846.         case 6:
  847.             $pic .= 'elementalist';
  848.             break;
  849.         case 7:
  850.             $pic .= 'mesmer';
  851.             break;
  852.         case 8:
  853.             $pic .= 'necromancer';
  854.             break;
  855.     }
  856.     return ('<img src="' . plugin_dir_url( __FILE__ ) . 'pics/' . $pic . '.png" alt="' . get_profession( $id ) . '" />');
  857. }
  858.  
  859. function get_orderCol( $id ) {
  860.         switch ( $id ) {
  861.             case 0:
  862.                 return( __( 'No order', 'gw2pcl' ) );
  863.                 break;
  864.             case 1:
  865.                 return( __( 'Order of Whisperers', 'gw2pcl' ) );
  866.                 break;
  867.             case 2:
  868.                 return( __( 'Durmand Priory', 'gw2pcl' ) );
  869.                 break;
  870.             case 3:
  871.                 return( __( 'The Vigil', 'gw2pcl' ) );
  872.                 break;
  873.         }
  874.         return( __( 'unknown order', 'gw2pcl' ) );
  875. }
  876.  
  877.  
  878. function get_orderCol_pic( $id ) {
  879.     $pic = "";
  880.     switch ( $id ) {
  881.         case 1:
  882.             $pic .= 'orderOfWhisperers';
  883.             break;
  884.         case 2:
  885.             $pic .= 'durmandPriory';
  886.             break;
  887.         case 3:
  888.             $pic .= 'theVigil';
  889.             break;
  890.     }
  891.     return ('<img src="' . plugin_dir_url( __FILE__ ) . 'pics/orders/' . $pic . '.png" alt="' . get_orderCol( $id ) . '" />');
  892. }
  893.  
  894.  
  895.  
  896. function get_crafting_skill( $id ) {
  897.     switch ( $id ) {
  898.         case 0:
  899.             return ( __( 'I don\'t craft', 'gw2pcl' ) );
  900.             break;
  901.         case 1:
  902.             return ( __( 'Armorsmith', 'gw2pcl' ) );
  903.             break;
  904.         case 2:
  905.             return ( __( 'Artificer', 'gw2pcl' ) );
  906.             break;
  907.         case 3:
  908.             return ( __( 'Chef', 'gw2pcl' ) );
  909.             break;
  910.         case 4:
  911.             return ( __( 'Huntsman', 'gw2pcl' ) );
  912.             break;
  913.         case 5:
  914.             return ( __( 'Jeweler', 'gw2pcl' ) );
  915.             break;
  916.         case 6:
  917.             return ( __( 'Leatherworker', 'gw2pcl' ) );
  918.             break;
  919.         case 7:
  920.             return ( __( 'Tailor', 'gw2pcl' ) );
  921.             break;
  922.         case 8:
  923.             return ( __( 'Weaponsmith', 'gw2pcl' ) );
  924.             break;
  925.     }
  926.     return ( __( 'unknown crafting skill', 'gw2pcl' ) );
  927. }
  928.  
  929.  
  930. function get_crafting_skill_pic( $id ) {
  931.     $pic = "";
  932.     switch ( $id ) {
  933.         case 1:
  934.             $pic = 'armorsmith';
  935.             break;
  936.         case 2:
  937.             $pic = 'artificer';
  938.             break;
  939.         case 3:
  940.             $pic = 'chef';
  941.             break;
  942.         case 4:
  943.             $pic = 'huntsman';
  944.             break;
  945.         case 5:
  946.             $pic = 'jeweler';
  947.             break;
  948.         case 6:
  949.             $pic = 'leatherworker';
  950.             break;
  951.         case 7:
  952.             $pic = 'tailor';
  953.             break;
  954.         case 8:
  955.             $pic = 'weaponsmith';
  956.             break;
  957.     }
  958.     return ('<img src="' . plugin_dir_url( __FILE__ ) . 'pics/' . $pic . '.png" alt="' . get_crafting_skill( $id ) . '" />');
  959. }
  960.  
  961. ?>
Advertisement
Add Comment
Please, Sign In to add comment