__construct(); } function register_post_type() { register_post_type( 'surl', array( 'labels' => array( 'name' => __( 'Simple URLs' ), 'singular_name' => __( 'URL' ), 'add_new' => __( 'Add New' ), 'add_new_item' => __( 'Add New URL' ), 'edit' => __( 'Edit' ), 'edit_item' => __( 'Edit URL' ), 'new_item' => __( 'New URL' ), 'view' => __( 'View URL' ), 'view_item' => __( 'View URL' ), 'search_items' => __( 'Search URL' ), 'not_found' => __( 'No URLs found' ), 'not_found_in_trash' => __( 'No URLs found in Trash' ) ), 'public' => true, 'query_var' => true, 'menu_position' => 20, 'supports' => array( 'title' ), 'rewrite' => array( 'slug' => 'go', 'with_front' => false ) ) ); } function columns_filter( $columns ) { $columns = array( 'cb' => '', 'title' => __('Title'), 'url' => __('Redirect to'), 'permalink' => __('Permalink'), 'clicks' => __('Clicks') ); return $columns; } function columns_data( $column ) { global $post; $url = get_post_meta($post->ID, '_surl_redirect', true); $count = get_post_meta($post->ID, '_surl_count', true); if ( $column == 'url' ) { echo make_clickable( esc_url( $url ? $url : '' ) ); } elseif ( $column == 'permalink' ) { echo make_clickable( get_permalink() ); } elseif ( $column == 'clicks' ) { echo esc_html( $count ? $count : 0 ); } } function add_meta_box() { add_meta_box('surl', __('URL Information', 'surl'), array( &$this, 'meta_box' ), 'surl', 'normal', 'high'); } function meta_box() { global $post; printf( '', wp_create_nonce( plugin_basename(__FILE__) ) ); printf( '
', '_surl_redirect', __('Redirect URI', 'surl') ); printf( '', 'width: 99%;', '_surl_redirect', '_surl_redirect', esc_attr( get_post_meta( $post->ID, '_surl_redirect', true ) ) ); $count = isset( $post->ID ) ? get_post_meta($post->ID, '_surl_count', true) : 0; printf( 'This URL has been accessed %d times.', esc_attr( $count ) ); } function meta_box_save( $post_id, $post ) { $key = '_surl_redirect'; // verify the nonce if ( !isset($_POST['_surl_nonce']) || !wp_verify_nonce( $_POST['_surl_nonce'], plugin_basename(__FILE__) ) ) return; // don't try to save the data under autosave, ajax, or future post. if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return; if ( defined('DOING_AJAX') && DOING_AJAX ) return; if ( defined('DOING_CRON') && DOING_CRON ) return; // is the user allowed to edit the URL? if ( ! current_user_can( 'edit_posts' ) || $post->post_type != 'surl' ) return; $value = isset( $_POST[$key] ) ? $_POST[$key] : ''; if ( $value ) { // save/update update_post_meta($post->ID, $key, $value); } else { // delete if blank delete_post_meta($post->ID, $key); } } function count_and_redirect() { if ( !is_singular('surl') ) return; global $wp_query; // Update the count $count = isset( $wp_query->post->ID ) ? get_post_meta($wp_query->post->ID, '_surl_count', true) : 0; update_post_meta( $wp_query->post->ID, '_surl_count', $count + 1 ); // Handle the redirect $redirect = isset( $wp_query->post->ID ) ? get_post_meta($wp_query->post->ID, '_surl_redirect', true) : ''; if ( !empty( $redirect ) ) { wp_redirect( esc_url_raw( $redirect ), 301); exit; } else { wp_redirect( home_url(), 302 ); exit; } } } $SimpleURLs = new SimpleURLs;