· 9 years ago · Oct 26, 2016, 08:50 PM
1<?php
2
3/*
4 Plugin Name: Custom URL Redirect Plugin
5 Plugin URI: http://www.aavatar.co.in
6 Description: This plugin is an addon for the wp calendar plugin which will add an option for sharing on google calendar
7 Author: Aavatar (lucky.e2077@gmail.com)
8 Author URI: http://www.aavatar.co.in
9 Version: 1.1.0
10 License: Commercial and/or GPLv2
11
12 Copyright 2009-2013 Aavatar Inc (http://www.aavatar.co.in)
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License (Version 2 - GPLv2) as published by
16 the Free Software Foundation.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, inform us at bhupendra@aavatar.co.in
25 */
26
27
28if (!defined('WP_LOAD_PATH')) {
29 /** classic root path if wp-content and plugins is below wp-config.php */
30 $classic_root = dirname(dirname(dirname(dirname(__FILE__)))) . '/';
31 if (file_exists($classic_root . 'wp-load.php'))
32 define('WP_LOAD_PATH', $classic_root);
33 else
34 if (file_exists($path . 'wp-load.php'))
35 define('WP_LOAD_PATH', $path);
36 else
37 exit("Could not find wp-load.php");
38}
39require_once( WP_LOAD_PATH . 'wp-load.php');
40
41/* * *************************************************************************
42 * Below are options not available on the admin page that you can
43 * customize here
44 *
45 * Note that if you make any changes below you must make these same changes
46 * every time you update CURP.
47 *
48 * ************************************************************************** */
49
50
51define('CURP_URL', plugin_dir_url(__FILE__));
52
53define('CURP_PATH', plugin_dir_path(__FILE__));
54
55add_action('admin_menu', 'CURP_admin_menu');
56
57function CURP_admin_menu() {
58 add_menu_page('Custom ShortURL', 'Custom ShortURL', 10, 'custom_short_url', 'custom_short_url_func');
59}
60
61function custom_short_url_func() {
62 include 'admin/custom-url-shorten.php';
63}
64
65register_activation_hook(__FILE__, 'CURP_activation_func');
66
67function CURP_activation_func() {
68 global $wpdb;
69 $query = 'CREATE TABLE IF NOT EXISTS `' . $wpdb->get_blog_prefix() . 'short_url` (
70 `id` int(11) NOT NULL AUTO_INCREMENT,
71 `old_url` varchar(255) NOT NULL,
72 `new_url` varchar(255) NOT NULL,
73 `created_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
74 PRIMARY KEY (`id`)
75 )';
76 $wpdb->query($query);
77}
78
79add_filter('single_template', 'CURP_single_template');
80
81function CURP_single_template() {
82 global $post;
83 if ($post->post_type == 'post') {
84 $now = time();
85 $creationDate = strtotime($post->post_date);
86 $datediff = abs($now - $creationDate);
87 $datediffnew = floor($datediff / (60 * 60 * 24));
88 $content1Date=explode('-',get_option('content1_days'));
89 $content2Date=explode('-',get_option('content2_days'));
90 $content4Date=explode('-',get_option('content4_days'));
91 $content5Date=explode('-',get_option('content5_days'));
92 $content6Date=explode('-',get_option('content6_days'));
93 if (is_user_logged_in())
94 $single_template = dirname(__FILE__) . '/templates/content3.php';
95 else if ((intval($datediffnew) >= intval($content1Date[0])) && (intval($datediffnew) <= intval($content1Date[1])))
96 $single_template = dirname(__FILE__) . '/templates/content1.php';
97 else if ((intval($datediffnew) >= intval($content2Date[0])) && (intval($datediffnew) <= intval($content2Date[1])))
98 $single_template = dirname(__FILE__) . '/templates/content2.php';
99 else if ((intval($datediffnew) >= intval($content4Date[0])) && (intval($datediffnew) <= intval($content4Date[1])))
100 $single_template = dirname(__FILE__) . '/templates/content4.php';
101 else if ((intval($datediffnew) >= intval($content5Date[0])) && (intval($datediffnew) <= intval($content5Date[1])))
102 $single_template = dirname(__FILE__) . '/templates/content5.php';
103 else if ((intval($datediffnew) >= intval($content6Date[0])) && (intval($datediffnew) <= intval($content6Date[1])))
104 $single_template = dirname(__FILE__) . '/templates/content6.php';
105 else
106 $single_template = dirname(__FILE__) . '/templates/content2.php';
107 }
108 return $single_template;
109}
110
111add_filter('the_content', 'CURP_add_link');
112
113function CURP_add_link($content) {
114 global $post, $wpdb;
115
116 if ($post->post_type == 'post') {
117 $shortURL = $wpdb->get_row('SELECT * FROM `' . $wpdb->get_blog_prefix() . 'short_url` WHERE new_url="' . rtrim(get_permalink($post->ID), '//') . '"', ARRAY_A);
118 return $content . '<a href="javascript:void(0)" onclick="window.location=\'' . $shortURL['old_url'] . '\'">Link to original post</a>';
119 }
120 return $content;
121}
122
123function shorten_the_url($post) {
124 global $wpdb;
125 $short_url_list = array();
126 $url_list = explode("\n", $post['url_list']);
127 if (count($url_list) > 0) {
128 for ($i = 0; $i < count($url_list); $i++) {
129 $newUrl = ltrim($url_list[$i], 'http://');
130 $postSlug = substr(md5(uniqid(rand(1, 6))), 0, 10);
131// $postTitle = substr($newUrl, strpos($newUrl, '/'), strlen($url_list[$i]));
132 $postTitle = end(explode('/',$newUrl));
133 $postTitle = str_replace(array('//', '.', '/'), '-', trim($postTitle, '//'));
134 $postTitle = $postTitle .'-'.$postSlug;
135 $short_url_list[$i] = get_site_url() . '/' .$postSlug;
136 $oldPost = get_post(url_to_postid($url_list[$i]));
137 $newPost = array('post_title' => $postTitle, 'post_name' => $postSlug, 'post_status' => 'publish', 'post_type' => 'post');
138 $postID = wp_insert_post($newPost);
139 update_post_meta($postID, 'original_url', $url_list[$i]);
140 }
141 }
142 return $short_url_list;
143}
144add_action("wp_ajax_org_url", "org_url_handler");
145add_action("wp_ajax_nopriv_org_url", "org_url_handler");
146
147function org_url_handler(){
148 $orgUrl=get_post_meta($_POST['postID'], 'original_url', true);
149 echo ($orgUrl);
150}
151
152?>