New Paste

 

Recent Pastes

Administrate




PageLines PasteBin.

PasteID: 7pq
Pasted by Anonymous, 2013-07-24 04:16:23 GMT
Expires Never
Paste size 10.62 Kb
Toggle Expand   Wrap   Raw
Copy Contents   Copy URL
 
  1. <?php
  2. /*
  3. Plugin Name: Poppy
  4. Plugin URI: http://www.pagelines.com
  5. Description: Adds a simple contact form shortcode to be used anywhere on your site.
  6. Author: PageLines
  7. PageLines: true
  8. Version: 1.3
  9. Demo: http://poppy.pagelines.me
  10. */
  11.  
  12. class PageLinesPoppy {
  13.  
  14. 	var $version = 1.3;
  15. 	function __construct() {
  16.  
  17. 		$this->base_dir	= plugin_dir_path( __FILE__ );
  18. 		$this->base_url = plugins_url( '', __FILE__ );
  19. 		$this->icon		= plugins_url( '/icon.png', __FILE__ );
  20. 		$this->less		= $this->base_dir . '/style.less';
  21. 		add_filter( 'pagelines_lesscode', array( &$this, 'get_less' ), 10, 1 );
  22. 		add_action( 'pagelines_setup', array( &$this, 'admin_page' ) );
  23. 		add_action( 'init', array( &$this, 'add_shortcode' ) );
  24. 		add_action( 'wp_enqueue_scripts', array( &$this, 'hooks_with_activation' ) );
  25. 		add_action( 'wp_ajax_nopriv_ajaxcontact_send_mail', array( &$this, 'ajaxcontact_send_mail' ) );
  26. 		add_action( 'wp_ajax_ajaxcontact_send_mail', array( &$this, 'ajaxcontact_send_mail' ) );
  27. 		add_action( 'plugins_loaded', array( &$this, 'translate') );
  28. 	}
  29.  
  30. 	function translate() {
  31.  		$plugin_dir = basename( dirname( __FILE__ ) );
  32.  		load_plugin_textdomain( 'pagelines-poppy', false, $plugin_dir );
  33. 	}
  34.  
  35. 	function hooks_with_activation() {
  36. 		wp_enqueue_script( 'poppyjs', plugins_url( '/script.js', __FILE__ ), array( 'jquery' ), $this->version );
  37. 		wp_localize_script( 'poppyjs', 'poppyjs', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
  38.  
  39. 		if( ! function_exists( 'pl_detect_ie' ) )
  40. 			return;
  41. 		$ie_ver = pl_detect_ie();
  42. 		if( $ie_ver < 10 )
  43. 			wp_enqueue_script( 'formalize', plugins_url( '/formalize.min.js', __FILE__ ), array( 'jquery' ), $this->version );
  44. 	}
  45.  
  46. 	function get_less( $less ){
  47.  
  48. 		$less .= pl_file_get_contents( $this->less );
  49. 		return $less;
  50.  
  51. 	}
  52. 	function add_shortcode() {
  53. 		add_shortcode( 'poppy', array(  &$this, 'draw_button' ) );
  54. 	}
  55.  
  56. 	function draw_button( $atts, $content = null ) {
  57.  
  58. 		extract( shortcode_atts( array(
  59. 		    'class' => '',
  60. 		    'type'	=> 'button',
  61. 		), $atts ) );
  62.  
  63. 		if( ! $content )
  64. 			$content = 'Contact';
  65. 		if( 'button' == $type )
  66. 			$class = 'btn ' . $class;
  67. 		if( 'label' == $type ) {
  68. 			$class = 'label ' . $class;
  69. 			$type = 'span';
  70. 		}
  71. 		$class = rtrim( $class ) . ' poppy-pointer';
  72.  
  73. 		$button = sprintf( '<%s class="%s" data-toggle="modal" href="#poppy-modal">%s</%s>',
  74. 			$type,
  75. 			$class,
  76. 			$content,
  77. 			$type
  78. 			);
  79. 		add_action( 'wp_footer', array( &$this, 'form' ) );
  80. 		return $button;
  81. 	}
  82.  
  83. 	function form() {
  84. 		ob_start();
  85. 		$email		= __( 'Email Address', 'pagelines-poppy' );
  86. 		$name		= __( 'Name', 'pagelines-poppy' );
  87. 		$message	= __( 'Your Message...', 'pagelines-poppy' );
  88. 		$send		= __( 'Send Message', 'pagelines-poppy' );
  89. 	?>
  90. <div id="poppy-modal" class="hide fade modal poppy" >
  91. 	<div class="modal-header"><a class="close" data-dismiss="modal" aria-hidden="true">×</a>
  92. 		<h3><?php echo ploption( 'poppy_form_title' ) ?></h3>
  93. 	</div>
  94. 	<div class="modal-body">
  95. 		<div class="poppy-response"></div>
  96. 		<form class="poppy-form" id="ajaxcontactform" action="" method="post" enctype="multipart/form-data">
  97. 			<fieldset>
  98. 				<div class="control-group">
  99. 					<div class="controls form-inline">
  100. 						<?php
  101. 						printf( '<input class="poppy-input poppy-name" placeholder="%1$s" id="ajaxcontactname" type="text" name="%1$s">', $name );
  102. 						printf( '<input class="poppy-input poppy-email" placeholder="%1$s" id="ajaxcontactemail" type="text" name="%1$s">',$email );
  103. 						if ( ploption( 'poppy_enable_extra' ) && '' != ploption( 'poppy_extra_field' ) )
  104. 							printf( '<input class="poppy-input poppy-custom" placeholder="%1$s" id="ajaxcontactcustom" type="text" name="%1$s">', stripslashes( ploption( 'poppy_extra_field' ) ) );
  105. 						?>
  106. 					</div>
  107. 				</div>
  108. 			<div class="control-group">
  109. 				<div class="controls">
  110. 					<div class="textarea">
  111. 						<?php printf( '<textarea class="poppy-msg" row="8" placeholder="%s" id="ajaxcontactcontents" name="%s"></textarea>', $message, $message ); ?>
  112. 					</div>
  113. 				</div>
  114. 			</div>
  115.  
  116. 			<?php if ( ploption( 'poppy_enable_captcha' ) ) $this->captcha(); ?>
  117.  
  118. 			<div class="controls">
  119. 				<?php printf( '<a class="btn btn-primary send-poppy">%s</a>', $send ); ?>
  120. 			</div>
  121. 			</fieldset>
  122. 		</form>
  123. 	</div>
  124. </div>
  125. 		<?php
  126. 		$form = ob_get_clean();
  127. 		echo $form;
  128. 	}
  129.  
  130.  
  131. 	function captcha() {
  132.  
  133. 		$code = sprintf( '<div class="control-group">
  134. 		<label class="control-label">Captcha</label>
  135. 		<div class="controls">
  136. 			<input class="span2 poppy-captcha" placeholder="%s" id="ajaxcontactcaptcha" type="text" name="ajaxcontactcaptcha" />
  137. 		</div>
  138. 	</div>', stripslashes( ploption( 'poppy_captcha_question' ) ) );
  139. 	echo $code;
  140. 	}
  141.  
  142.  
  143. 	function admin_page() {
  144.  
  145. 		if ( ! function_exists( 'ploption' ) )
  146. 			return;
  147. 		$option_args = array(
  148.  
  149. 			'name'		=> 'Poppy',
  150. 			'array'		=> $this->options_array(),
  151. 			'icon'		=> $this->icon,
  152. 			'position'	=> 6
  153. 		);
  154. 		pl_add_options_page( $option_args );
  155. 	}
  156.  
  157. 	function options_array() {
  158.  
  159. 		$options = array(
  160.  
  161. 			'poppy_options'	=> array(
  162. 				'type'	=> 'multi_option',
  163. 				'title'	=>	__( 'Poppy Options', 'pagelines-poppy' ),
  164. 				'shortexp'	=> __( 'Configure poppy popup forms, click more info for shortcode examples', 'pagelines-poppy' ),
  165. 				'layout'	=> 'full',
  166. 				'title'		=> 'Poppy Options',
  167. 				'shortexp'	=> 'Configure your Poppy forms here.',
  168. 				'exp'	=> 'Here are a few examples:<br /><br /><strong>[poppy]</strong><br />Creates a standard button with the word "Contact"<br /><br /><strong>[poppy]Email me![/poppy]</strong><br />Same as above with custom text.<br /><br /><strong>[poppy type="a"]Contact me.[/poppy]</strong><br />This uses a standard HTML link.<br /><br /><strong>[poppy type="button" class="btn-important"]Email.[/poppy]</strong><br />Here we are using the bootstrap button, and adding a class.<br /><br /><strong>[poppy type="label" class="label-warning"]CONTACT[/poppy]</strong><br />What about a bootstrap label?<br /><br /><strong>[poppy type="i" class="icon-envelope icon-4x"]&nbsp;[/poppy]</strong><br />Finally a giant font-awesome envelope!',
  169. 				'selectvalues'	=> array(
  170. 					'poppy_form_title' => array(
  171. 						'type' 		=> 'text',
  172. 						'inputlabel'	=> __( 'Form Title', 'pagelines-poppy' ),
  173. 						'default'	=> __( 'Contact Us!', 'pagelines-poppy' ),
  174. 						'shortexp' => __( 'Main title for the form.', 'pagelines-poppy' )
  175. 						),
  176. 					'poppy_email'	=> array(
  177. 						'type'	=> 'text',
  178. 						'inputlabel'	=> __( 'Default email send address', 'pagelines-poppy' ),
  179. 						'exp'	=> __( 'Email address to send for To. Leave blank to use admin email', 'pagelines-poppy' )
  180. 						),
  181. 					'poppy_enable_extra'	=> array(
  182. 						'type'	=> 'check',
  183. 						'default'	=> false,
  184. 						'inputlabel'	=> __( 'Enable extra custom field', 'pagelines-poppy' )
  185. 						),
  186. 					'poppy_extra_field'	=> array(
  187. 						'type'	=> 'text',
  188. 						'default'	=> '',
  189. 						'inputlabel'	=> __( 'Extra field text', 'pagelines-poppy' )
  190. 						),
  191. 					'poppy_enable_captcha'	=> array(
  192. 						'type'	=> 'check',
  193. 						'default'	=> true,
  194. 						'inputlabel'	=> __( 'Enable simple antispam question', 'pagelines-poppy' )
  195. 						),
  196. 					'poppy_captcha_question'	=> array(
  197. 						'type'	=> 'text',
  198. 						'default'	=> '2 + 5',
  199. 						'inputlabel'	=> __( 'Antispam question', 'pagelines-poppy' )
  200. 						),
  201. 					'poppy_captcha_answer'	=> array(
  202. 						'type'	=> 'text',
  203. 						'default'	=> '7',
  204. 						'inputlabel'	=> __( 'Antispam answer', 'pagelines-poppy' )
  205. 						),
  206. 					'poppy_email_layout'	=> array(
  207. 						'type'	=> 'text',
  208. 						'inputlabel'	=> __( 'Format for email subject. Possible values: %name% %blog%', 'pagelines-poppy' ),
  209. 						'default'	=> '[%blog%] New message from %name%.',
  210.  
  211. 						)
  212. 					)
  213. 				)
  214. 			);
  215. 	return $options;
  216. 	}
  217.  
  218. 	function ajaxcontact_send_mail(){
  219.  
  220. 		$data = stripslashes_deep( $_POST );
  221.  
  222. 		$defaults = array(
  223. 			'name'	=> '',
  224. 			'email'	=> '',
  225. 			'custom'=> '',
  226. 			'msg'	=> '',
  227. 			'cap'	=> '',
  228. 			'width'	=> '',
  229. 			'height'=> '',
  230. 			'agent' => ''
  231. 		);
  232.  
  233. 		$data = wp_parse_args($data, $defaults);
  234.  
  235. 		$name			= $data['name'];
  236. 		$email			= $data['email'];
  237. 		$custom			= $data['custom'];
  238. 		$custom_field	= ( ploption( 'poppy_enable_extra' ) ) ? ploption( 'poppy_extra_field' ) : '';
  239. 		$contents		= $data['msg'];
  240. 		$admin_email	= ( ploption( 'poppy_email' ) ) ? ploption( 'poppy_email' ) : get_option( 'admin_email' );
  241. 		$captcha		= $data['cap'];
  242. 		$captcha_ans	= ploption( 'poppy_captcha_answer' );
  243. 		$width			= $data['width'];
  244. 		$height			= $data['height'];
  245. 		$ip				= $_SERVER['REMOTE_ADDR'];
  246. 		$agent			= $data['agent'];
  247.  
  248. 		if ( ploption( 'poppy_enable_captcha' ) ){
  249. 			if( '' == $captcha )
  250. 				die( __( 'Captcha cannot be empty!', 'pagelines-poppy' ) );
  251. 			if( $captcha !== $captcha_ans )
  252. 				die( __( 'Captcha does not match.', 'pagelines-poppy' ) );
  253. 		}
  254.  
  255. 		if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
  256. 			die( __( 'Email address is not valid.', 'pagelines-poppy' ) );
  257. 		} elseif( strlen( $name ) == 0 ) {
  258. 			die( __( 'Name cannot be empty.', 'pagelines-poppy' ) );
  259. 		} elseif( strlen( $contents ) == 0 ) {
  260. 			die( __( 'Content cannot be empty.', 'pagelines-poppy' ) );
  261. 		}
  262.  
  263. 		// create an email.
  264. 		$subject_template	= ( '' != ploption( 'poppy_email_layout' ) ) ? ploption( 'poppy_email_layout' ) : '[%blog%] New message from %name%.';
  265. 		$subject			= str_replace( '%blog%', get_bloginfo( 'name' ), str_replace( '%name%', $name, $subject_template ) );
  266. 		$custom 			= ( $custom_field ) ? sprintf( '%s: %s', $custom_field, $custom ) : '';
  267. 		$fields = 'Name: %s %7$sEmail: %s%7$sContents%7$s=======%7$s%s %7$s%7$sUser Info.%7$s=========%7$sIP: %s %7$sScreen Res: %s %7$sAgent: %s %7$s%7$s%8$s';
  268.  
  269. 		$template = sprintf( $fields,
  270. 			$name,
  271. 			$email,
  272. 			$contents,
  273. 			$ip,
  274. 			sprintf( '%sx%s', $width, $height ),
  275. 			$agent,
  276. 			"\n",
  277. 			$custom
  278. 			);
  279.  
  280. 		if( true === ( $result = wp_mail( $admin_email, $subject, $template ) ) )
  281. 			die( 'ok' );
  282.  
  283. 		if ( ! $result ) {
  284.  
  285. 			global $phpmailer;
  286.  
  287. 			if( isset( $phpmailer->ErrorInfo ) ) {
  288. 				die( sprintf( 'Error: %s', $phpmailer->ErrorInfo ) );
  289. 			} else {
  290. 				die( __( 'Unknown wp_mail() error.', 'pagelines-poppy' ) );
  291. 			}
  292. 		}		
  293. 	}
  294. }
  295. new PageLinesPoppy;
  296.  
 
 
 

 
 
 
 
 
Written by Xan Manning, 2010.
YOU NEED FLASH!