Skip to content

Instantly share code, notes, and snippets.

@Masamasamasashito
Created June 12, 2018 06:15
Show Gist options
  • Save Masamasamasashito/3a2ee4b7fedb44eaa321d914c3446d93 to your computer and use it in GitHub Desktop.
Save Masamasamasashito/3a2ee4b7fedb44eaa321d914c3446d93 to your computer and use it in GitHub Desktop.
TCD プラグイン Workflow
<?php
/*
Plugin Name: TCD Workflow
Plugin URI: http://design-plus1.com/tcd-w/
Description: Easily create contact forms in your website
Version: 1.5.3
Author: DesignPlus
Author URI: http://design-plus1.com/tcd-w/wp-tcd
License: GPL
Copyright: DesignPlus
Text Domain: tcd-contact-form
*/
/* Copyright 2016 DesignPlus (email : designplus208@gmail.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, 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, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
global $jal_db_version;
$jal_db_version = '2.5';
function jal_install() {
global $wpdb;
global $jal_db_version;
$installed_ver = get_option( "jal_db_version" );
$table_name = $wpdb->prefix . 'tcdcontact';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
name tinytext NOT NULL,
type tinytext NOT NULL,
data text,
UNIQUE KEY id (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
if ( $installed_ver != $jal_db_version ) {
update_option( "jal_db_version", $jal_db_version );
} else {
add_option( 'jal_db_version', $jal_db_version );
}
}
function myplugin_update_db_check() {
global $jal_db_version;
if ( get_site_option( 'jal_db_version' ) != $jal_db_version ) {
jal_install();
}
}
add_action( 'plugins_loaded', 'myplugin_update_db_check' );
register_activation_hook( __FILE__, 'jal_install' );
function tcd_add_form() {
$data = json_decode(stripslashes($_POST['data']), true);
global $wpdb;
$wpdb->insert($wpdb->prefix . 'tcdcontact', array(
'name' => $data['name'],
'time' => current_time('mysql'),
'type' => $data['type'],
'data' => stripslashes($_POST['data'])
));
header('Location:admin.php?page=tcd-contact-form');
}
function tcd_update_form() {
$data = json_decode(stripslashes($_POST['data']), true);
$id = ($_POST['id']);
global $wpdb;
$wpdb->update($wpdb->prefix . 'tcdcontact', array(
'name' => $data['name'],
'type' => $data['type'],
'data' => stripslashes($_POST['data'])
), array('id' => $id));
header('Location:admin.php?page=tcd-contact-form');
}
function tcd_delete_form() {
$id = intval($_GET['id']);
global $wpdb;
$wpdb->delete($wpdb->prefix . 'tcdcontact', array('id' => $id));
header('Location:admin.php?page=tcd-contact-form');
}
function tcd_copy_form() {
$id = intval($_GET['id']);
global $wpdb;
$from = $wpdb->prefix . 'tcdcontact';
$form = $wpdb->get_results("
SELECT *
FROM $from
WHERE id = '$id'
");
$form = $form[0];
$form = json_decode(json_encode($form), true);
$wpdb->insert($wpdb->prefix . 'tcdcontact', array(
'name' => $form['name'],
'time' => current_time('mysql'),
'type' => $form['type'],
'data' => $form['data'],
));
header('Location:admin.php?page=tcd-contact-form');
}
function tcd_contact_script_enqueue() {
wp_enqueue_script('jquery-ui-core');
wp_enqueue_script('jquery-ui-sortable');
wp_enqueue_script('angular', plugins_url('angular.js', __FILE__));
wp_enqueue_script('angular-ui', plugins_url('angular-ui.js', __FILE__));
// wp_enqueue_script('my_script', plugins_url('my_script.js', __FILE__));
wp_register_style('tcd_contact_css', plugins_url('tcd-contact-admin.css', __FILE__), false, '1.0.0');
wp_enqueue_style('tcd_contact_css');
}
function tcd_contact_add_js_css() {
wp_enqueue_script('angular', plugins_url('angular.js', __FILE__));
// wp_enqueue_script('tcd-slider', plugins_url('tcd-slider.js', __FILE__));
wp_enqueue_style('tcd-contact-form', plugins_url("tcd-contact-admin.css", __FILE__));
// wp_enqueue_style('tcd-slider', plugins_url("tcd-slider.css", __FILE__));
}
function tcd_contact_slideshow_shortcode($opts, $content){
echo "<div class='tcd-slideshow'>$content</div>";
}
$globalPadding = null;
function tcd_contact_row_shortcode($opts, $content, $tag){
global $globalPadding;
if(@$opts["padding"]){
$globalPadding = intval($opts["padding"]) . "px";
}
if( $tag == "row" ){
echo "<div class='dp-flow-row' style='margin:0 -$globalPadding 0 -$globalPadding'>";
echo do_shortcode($content);
echo "</div>";
} else if( $tag == "half" ){
echo "<div class='dp-flow-col' style='width:50%; padding:0 $globalPadding 0 $globalPadding'>" . do_shortcode($content) . "</div>";
} else if( $tag == "third" ){
echo "<div class='dp-flow-col' style='width:33.32%; padding:0 $globalPadding 0 $globalPadding'>" . do_shortcode($content) . "</div>";
} else if( $tag == "fourth" ){
echo "<div class='dp-flow-col' style='width:25%; padding:0 $globalPadding 0 $globalPadding'>" . do_shortcode($content) . "</div>";
} else if( $tag == "fifth" ){
echo "<div class='dp-flow-col' style='width:20%; padding:0 $globalPadding 0 $globalPadding'>" . do_shortcode($content) . "</div>";
}
}
function tcd_contact_shortcode($opts){
$formId = (int)"form." + rand(100000, 999999);
$url = plugins_url("/img", __FILE__);
if(isset($opts['id'])){
global $wpdb;
$from = $wpdb->prefix . 'tcdcontact';
$id = intval($opts['id']);
$form = $wpdb->get_results("
SELECT *
FROM $from
WHERE id = '$id'
");
$form = $form[0];
$form = json_decode(json_encode($form), true);
$data = $phpData = json_decode($form['data'], true);
$data['id'] = $form['id'];
} else {
$str = str_replace("(", "[", $opts['data']);
$str = str_replace(")", "]", $str);
$form = json_decode(json_encode($str), true);
$data = $phpData = json_decode($str, true);
}
if( $phpData['type'] == 'flow' ){
ob_start();
include "flow.php";
return ob_get_clean();
} else if( $phpData['type'] == 'table' ) {
ob_start();
include "table.php";
return ob_get_clean();
} else if( $phpData['type'] == 'menu' ){
ob_start();
include "menu-view.php";
return ob_get_clean();
} else if( $phpData['type'] == 'layout' ){
ob_start();
include "template-view.php";
return ob_get_clean();
} else if( $phpData['type'] == 'form' ){
$adminUrl = admin_url( 'admin-ajax.php' );
ob_start();
include "form.php";
return ob_get_clean();
}
}
function tcd_preview_form() {
include "preview.php";
}
function tcd_form_list() {
include "form-list.php";
}
function tcd_new_form() {
include "new-form.php";
}
function tcd_create_layout() {
include "create-layout.php";
}
function tcd_contact_plugin_setup_menu(){
add_menu_page('TCD Workflow', 'TCD Workflow', 'manage_options', 'tcd-contact-form', 'tcd_form_list');
add_submenu_page('tcd-contact-form', 'Add new', 'Add new', 'manage_options', 'tcd-add-contact-form', 'tcd_new_form');
// add_submenu_page('tcd-contact-form', 'Create layout', 'Create layout', 'manage_options', 'tcd-create-layout', 'tcd_create_layout');
add_submenu_page(null, 'Preview', 'Preview', 'manage_options', 'tcd-preview-form', 'tcd_preview_form');
}
function tcd_submit_form_callback() {
global $wpdb;
$from = $wpdb->prefix . 'tcdcontact';
$id = intval($_REQUEST['id']);
$custMail = $_REQUEST['mail'];
$form = $wpdb->get_results("
SELECT *
FROM $from
WHERE id = '$id'
");
$form = json_decode(json_encode($form[0]), true);
$form = json_decode($form['data'], true);
$steps = json_decode(stripcslashes($_REQUEST['steps']), true);
$emails = explode(",", $form['mailto']);
foreach( $emails as $email ){
wp_mail($email, $form['mailsubj'], $_REQUEST['data']);
}
if( $form['mailnotif'] == true ){
$message = $form['mailcustcontent'];
$subj = $form['mailcustsubj'];
$sendername = $form['mailcustsender'];
$sitename = strtolower( $_SERVER['SERVER_NAME'] );
if ( substr( $sitename, 0, 4 ) == 'www.' ) {
$sitename = substr( $sitename, 4 );
}
if( $form['mailcustsendermail'] && $sendername ){
$sendermail = $form['mailcustsendermail'];
$headers = array("From: $sendername <$sendermail>");
} else {
$headers = array("From: $sendername <wordpress@$sitename>");
}
if( strlen($form['mailreplyto']) > 0 ){
array_push($headers, "Reply-To: $sendername <" . $form['mailreplyto'] . '>');
}
foreach( $steps as $step ){
$message = str_replace("[" . $step['label1'] . "]", $step['answer'], $message);
$subj = str_replace("[" . $step['label1'] . "]", $step['answer'], $subj);
}
wp_mail($custMail, $subj, $message, $headers);
}
wp_die(1);
}
function tcd_enqueue_script_admin_check(){
if (is_admin ()){
wp_enqueue_media ();
}
}
load_plugin_textdomain( 'tcd-contact-form', false, basename( dirname( __FILE__ ) ) . '/languages' );
add_action ( 'admin_enqueue_scripts', 'tcd_enqueue_script_admin_check');
add_action('admin_enqueue_scripts', 'tcd_contact_script_enqueue');
add_action('wp_enqueue_scripts', 'tcd_contact_add_js_css');
add_action( 'wp_ajax_tcd_submit_form', 'tcd_submit_form_callback' );
add_action( 'wp_ajax_nopriv_tcd_submit_form', 'tcd_submit_form_callback' );
add_action( 'admin_post_tcd_add_form', 'tcd_add_form' );
add_action( 'admin_post_tcd_update_form', 'tcd_update_form' );
add_action( 'admin_post_tcd_delete_form', 'tcd_delete_form' );
add_action( 'admin_post_tcd_copy_form', 'tcd_copy_form' );
add_filter('widget_text', 'do_shortcode');
add_shortcode( 'tcd-form', 'tcd_contact_shortcode' );
add_shortcode( 'tcd-table', 'tcd_contact_shortcode' );
add_shortcode( 'tcd-flow', 'tcd_contact_shortcode' );
add_shortcode( 'tcd-menu', 'tcd_contact_shortcode' );
add_shortcode( 'tcd-layout', 'tcd_contact_shortcode' );
add_shortcode( 'tcd-snippet', 'tcd_contact_shortcode' );
add_shortcode( 'tcd-slideshow', 'tcd_contact_slideshow_shortcode' );
add_shortcode('row', 'tcd_contact_row_shortcode');
add_shortcode('half', 'tcd_contact_row_shortcode');
add_shortcode('third', 'tcd_contact_row_shortcode');
add_shortcode('quarter', 'tcd_contact_row_shortcode');
add_shortcode('fifth', 'tcd_contact_row_shortcode');
add_action('admin_menu', 'tcd_contact_plugin_setup_menu');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment