Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Custom URL Redirect Plugin
- Plugin URI: http://www.aavatar.co.in
- Description: This plugin is an addon for the wp calendar plugin which will add an option for sharing on google calendar
- Author: Aavatar ([email protected])
- Author URI: http://www.aavatar.co.in
- Version: 1.1.0
- License: Commercial and/or GPLv2
- Copyright 2009-2013 Aavatar Inc (http://www.aavatar.co.in)
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License (Version 2 - GPLv2) as published by
- the Free Software Foundation.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, inform us at [email protected]
- */
- if (!defined('WP_LOAD_PATH')) {
- /** classic root path if wp-content and plugins is below wp-config.php */
- $classic_root = dirname(dirname(dirname(dirname(__FILE__)))) . '/';
- if (file_exists($classic_root . 'wp-load.php'))
- define('WP_LOAD_PATH', $classic_root);
- else
- if (file_exists($path . 'wp-load.php'))
- define('WP_LOAD_PATH', $path);
- else
- exit("Could not find wp-load.php");
- }
- require_once( WP_LOAD_PATH . 'wp-load.php');
- /* * *************************************************************************
- * Below are options not available on the admin page that you can
- * customize here
- *
- * Note that if you make any changes below you must make these same changes
- * every time you update CURP.
- *
- * ************************************************************************** */
- define('CURP_URL', plugin_dir_url(__FILE__));
- define('CURP_PATH', plugin_dir_path(__FILE__));
- add_action('admin_menu', 'CURP_admin_menu');
- function CURP_admin_menu() {
- add_menu_page('Custom ShortURL', 'Custom ShortURL', 10, 'custom_short_url', 'custom_short_url_func');
- }
- function custom_short_url_func() {
- include 'admin/custom-url-shorten.php';
- }
- register_activation_hook(__FILE__, 'CURP_activation_func');
- function CURP_activation_func() {
- global $wpdb;
- $query = 'CREATE TABLE IF NOT EXISTS `' . $wpdb->get_blog_prefix() . 'short_url` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `old_url` varchar(255) NOT NULL,
- `new_url` varchar(255) NOT NULL,
- `created_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
- PRIMARY KEY (`id`)
- )';
- $wpdb->query($query);
- }
- add_filter('single_template', 'CURP_single_template');
- function CURP_single_template() {
- global $post;
- if ($post->post_type == 'post') {
- $now = time();
- $creationDate = strtotime($post->post_date);
- $datediff = abs($now - $creationDate);
- $datediffnew = floor($datediff / (60 * 60 * 24));
- $content1Date=explode('-',get_option('content1_days'));
- $content2Date=explode('-',get_option('content2_days'));
- $content4Date=explode('-',get_option('content4_days'));
- $content5Date=explode('-',get_option('content5_days'));
- $content6Date=explode('-',get_option('content6_days'));
- if (is_user_logged_in())
- $single_template = dirname(__FILE__) . '/templates/content3.php';
- else if ((intval($datediffnew) >= intval($content1Date[0])) && (intval($datediffnew) <= intval($content1Date[1])))
- $single_template = dirname(__FILE__) . '/templates/content1.php';
- else if ((intval($datediffnew) >= intval($content2Date[0])) && (intval($datediffnew) <= intval($content2Date[1])))
- $single_template = dirname(__FILE__) . '/templates/content2.php';
- else if ((intval($datediffnew) >= intval($content4Date[0])) && (intval($datediffnew) <= intval($content4Date[1])))
- $single_template = dirname(__FILE__) . '/templates/content4.php';
- else if ((intval($datediffnew) >= intval($content5Date[0])) && (intval($datediffnew) <= intval($content5Date[1])))
- $single_template = dirname(__FILE__) . '/templates/content5.php';
- else if ((intval($datediffnew) >= intval($content6Date[0])) && (intval($datediffnew) <= intval($content6Date[1])))
- $single_template = dirname(__FILE__) . '/templates/content6.php';
- else
- $single_template = dirname(__FILE__) . '/templates/content2.php';
- }
- return $single_template;
- }
- add_filter('the_content', 'CURP_add_link');
- function CURP_add_link($content) {
- global $post, $wpdb;
- if ($post->post_type == 'post') {
- $shortURL = $wpdb->get_row('SELECT * FROM `' . $wpdb->get_blog_prefix() . 'short_url` WHERE new_url="' . rtrim(get_permalink($post->ID), '//') . '"', ARRAY_A);
- return $content . '<a href="javascript:void(0)" onclick="window.location=\'' . $shortURL['old_url'] . '\'">Link to original post</a>';
- }
- return $content;
- }
- function shorten_the_url($post) {
- global $wpdb;
- $short_url_list = array();
- $url_list = explode("\n", $post['url_list']);
- if (count($url_list) > 0) {
- for ($i = 0; $i < count($url_list); $i++) {
- $newUrl = ltrim($url_list[$i], 'http://');
- $postSlug = substr(md5(uniqid(rand(1, 6))), 0, 10);
- // $postTitle = substr($newUrl, strpos($newUrl, '/'), strlen($url_list[$i]));
- $postTitle = end(explode('/',$newUrl));
- $postTitle = str_replace(array('//', '.', '/'), '-', trim($postTitle, '//'));
- $postTitle = $postTitle .'-'.$postSlug;
- $short_url_list[$i] = get_site_url() . '/' .$postSlug;
- $oldPost = get_post(url_to_postid($url_list[$i]));
- $newPost = array('post_title' => $postTitle, 'post_name' => $postSlug, 'post_status' => 'publish', 'post_type' => 'post');
- $postID = wp_insert_post($newPost);
- update_post_meta($postID, 'original_url', $url_list[$i]);
- }
- }
- return $short_url_list;
- }
- add_action("wp_ajax_org_url", "org_url_handler");
- add_action("wp_ajax_nopriv_org_url", "org_url_handler");
- function org_url_handler(){
- $orgUrl=get_post_meta($_POST['postID'], 'original_url', true);
- echo ($orgUrl);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment