1+ <?php
2+ /*
3+ Plugin Name: Contact Form 7 Submission limit
4+ Version: 1.0
5+ Plugin URI: https://github.com/alhazmy13/Contact-Form-7-Submission-limit
6+ Description:
7+ Author: Abdullah Alhazmy
8+ Author URI: http://alhazmy13.net/
9+ License: GPLv2 or later
10+ Text Domain: contact-form-7-submission-limit
11+ */
12+
13+ /*
14+ Copyright 2017 Abdullah Alhazmy
15+
16+ This program is free software; you can redistribute it and/or
17+ modify it under the terms of the GNU General Public License
18+ as published by the Free Software Foundation; either version 2
19+ of the License, or (at your option) any later version.
20+
21+ This program is distributed in the hope that it will be useful,
22+ but WITHOUT ANY WARRANTY; without even the implied warranty of
23+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24+ GNU General Public License for more details.
25+
26+ You should have received a copy of the GNU General Public License
27+ along with this program; if not, write to the Free Software
28+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29+ */
30+
31+ //Exit if accessed directly
32+
33+
34+ if ( ! defined ( 'ABSPATH ' ) ) {
35+ exit ;
36+ }
37+
38+ function far_plugin_meta ( $ links , $ file ) { // add some links to plugin meta row
39+ if ( strpos ( $ file , 'contact-form-7-submission-limit.php ' ) !== false ) {
40+ $ links = array_merge ( $ links , array ( '<a href=" ' . esc_url ( get_admin_url (null , 'tools.php?page=contact-form-7-submission-limit ' ) ) . '">Settings</a> ' ) );
41+ // $links = array_merge( $links, array( '<a href="http://infolific.com/technology/software-worth-using/real-time-find-and-replace-for-wordpress/#pro-version" target="_blank">Pro Version</a> (less than $10)' ) );
42+ }
43+ return $ links ;
44+ }
45+
46+ /*
47+ * Add a submenu under Tools
48+ */
49+ function far_add_pages () {
50+ $ page = add_submenu_page ( 'tools.php ' , 'Contact Form 7 Submission limit ' , 'Contact Form 7 Submission limit ' , 'activate_plugins ' , 'contact-form-7-submission-limit ' , 'far_options_page ' );
51+ add_action ( "admin_print_scripts- $ page " , "far_admin_scripts " );
52+ }
53+
54+ function far_options_page () {
55+ if ( isset ( $ _POST ['setup-update ' ] ) ) {
56+ $ _POST = stripslashes_deep ( $ _POST );
57+
58+ // If atleast one find has been submitted
59+ if ( isset ( $ _POST ['farfind ' ] ) && is_array ( $ _POST ['farfind ' ] ) ) {
60+ foreach ( $ _POST ['farfind ' ] as $ key => $ find ){
61+
62+ // If empty ones have been submitted we get rid of the extra data submitted if any.
63+ if ( empty ($ find ) ){
64+ unset( $ _POST ['farfind ' ][$ key ] );
65+ unset( $ _POST ['limit ' ][$ key ] );
66+ unset( $ _POST ['farreplace ' ][$ key ] );
67+ }
68+
69+ // Convert line feeds on non-regex only
70+ if ( !isset ( $ _POST ['farregex ' ][$ key ] ) ) {
71+ $ _POST ['farfind ' ][$ key ] = str_replace ( "\r\n" , "\n" , $ find );
72+ }
73+ }
74+ }
75+ unset( $ _POST ['setup-update ' ] );
76+ unset( $ _POST ['import-text ' ] );
77+ unset( $ _POST ['export-text ' ] );
78+ unset( $ _POST ['submit-import ' ] );
79+
80+ // Delete the option if there are no settings. Keeps the database clean if they aren't using it and uninstalled.
81+ if ( empty ( $ _POST ['farfind ' ] ) ) {
82+ delete_option ( 'far_plugin_settings ' );
83+ } else {
84+ update_option ( 'far_plugin_settings ' , $ _POST );
85+ }
86+ echo '<div id="message" class="updated fade"> ' ;
87+ echo '<p><strong>Options Updated</strong></p> ' ;
88+ echo '</div> ' ;
89+ }
90+ ?>
91+ <div class="wrap" style="padding-bottom:5em;">
92+ <h2>Contact-Form7 Submmition Limit</h2>
93+ <p>Click "Add" to begin. Then enter your find and replace cases below. Click and drag to change the order. </p>
94+ <div id="far-items">
95+
96+ <form method="post" action="<?php echo esc_url ( $ _SERVER ["REQUEST_URI " ] ); ?> ">
97+ <?php echo '<textarea hidden id="test" name="test"> ' . json_encode (get_form_list ()) .'</textarea> ' ;?>
98+ <input type="button" class="button left" value="Add" onClick="addFormField(); return false;" />
99+ <input type="submit" class="button left" value="Update Settings" name="update" id="update" />
100+ <input type="hidden" name="setup-update" />
101+ <br style="clear: both;" />
102+ <?php $ far_settings = get_option ( 'far_plugin_settings ' ); ?>
103+ <ul id="far_itemlist">
104+ <?php
105+ $ i = 0 ;
106+ // If there are any finds already set
107+ if ( isset ( $ far_settings ['farfind ' ] ) && is_array ( $ far_settings ['farfind ' ] ) ){
108+ $ i = 1 ;
109+ foreach ( $ far_settings ['farfind ' ] as $ key => $ find ){
110+
111+ if ( isset ( $ far_settings ['farreplace ' ][$ key ] ) ) {
112+ $ far_replace = $ far_settings ['farreplace ' ][$ key ];
113+ } else {
114+ $ far_replace = '' ;
115+ }
116+
117+ if ( isset ( $ far_settings ['limit ' ][$ key ] ) ) {
118+ $ limit = $ far_settings ['limit ' ][$ key ];
119+ } else {
120+ $ limit = '' ;
121+ }
122+
123+
124+ echo "<li id='row $ i'> " ;
125+
126+ echo "<div style='float: left'> " ;
127+ echo "<div style='float: left'> " ;
128+ echo "<label for='farfind $ i'>Form ID:</label> " ;
129+ echo "<br /> " ;
130+ echo "<select name='farfind[ $ i]' id='farfind $ i'> " ;
131+ $ forms_list = get_form_list ();
132+ if ($ forms_list ){
133+ foreach ($ forms_list as $ key => $ value ) {
134+ if ($ find == $ value ->ID ){
135+ echo "<option name='farfind[ $ i]' id='farfind $ i' value=' " . $ value ->ID ."' selected> " . $ value ->post_title . "</option> " ;
136+ }else {
137+ echo "<option name='farfind[ $ i]' id='farfind $ i' value=' " . $ value ->ID ."'> " . $ value ->post_title . "</option> " ;
138+ }
139+ }
140+ }
141+ echo "</select> " ;
142+ echo "</div> " ;
143+ echo "<br /> " ;
144+
145+ echo "<div style='float: left'> " ;
146+ echo "<label for='limit $ i'>Limit:</label> " ;
147+ echo "<br /> " ;
148+ echo "<input class='textbox' type='number' name='limit[ $ i]' id='limit $ i' value=' " .$ limit ."'></input> " ;
149+ echo "</div> " ;
150+ echo "<br /> " ;
151+
152+ echo "<div style='float: left'> " ;
153+ echo "<label for='farreplace $ i'>Replace With:</label> " ;
154+ echo "<br /> " ;
155+ echo "<textarea class='left' name='farreplace[ $ i]' id='farreplace $ i'> " . esc_textarea ( $ far_replace ) . "</textarea> " ;
156+ echo "</div> " ;
157+
158+ echo "</div> " ;
159+
160+ echo "</li> " ;
161+ unset($ regex_checked );
162+ $ i = $ i + 1 ;
163+ }
164+ } else {
165+ // Do nothing
166+ }
167+ ?>
168+ </ul>
169+ <div id="divTxt"></div>
170+ <div class="clearpad"></div>
171+ <input type="button" class="button left" value="Add" onClick="addFormField(); return false;" />
172+ <input type="submit" class="button left" value="Update Settings" />
173+ <input type="hidden" id="id" value="<?php echo $ i ; /* used so javascript returns unique ids */ ?> " />
174+ </form>
175+ </div>
176+
177+
178+ </div>
179+ <?php } ?>
180+ <?php
181+ /*
182+ * Scripts needed for the admin side
183+ */
184+ function far_admin_scripts () {
185+ wp_enqueue_script ( 'far_dynamicfields ' , plugins_url () . '/contact-form-7-submission-limit/js/jquery.dynamicfields.js ' , array ('jquery ' ) );
186+ wp_enqueue_script ( 'jquery-ui-1 ' , plugins_url () . '/contact-form-7-submission-limit/js/jquery-ui-1.10.3.custom.min.js ' , array ('jquery ' ) );
187+ wp_enqueue_style ( 'far_styles ' , plugins_url () . '/contact-form-7-submission-limit/css/far.css ' );
188+ }
189+
190+ /*
191+ * Apply find and replace rules
192+ */
193+ function far_ob_call ( $ buffer ) { // $buffer contains entire page
194+ $ far_settings = get_option ( 'far_plugin_settings ' );
195+ if ( is_array ( $ far_settings ['farfind ' ] ) ) {
196+ foreach ( $ far_settings ['farfind ' ] as $ key => $ find ) {
197+ if (is_form_in_list ($ buffer ,$ find ) && is_submition_above_limit ($ find ,$ far_settings ['limit ' ][$ key ])){
198+ $ buffer = preg_replace ("/<form\b[^>]*[class= \"wpcf7\-form \"]\b[^>]*>(.*?)<\/form>/is " , $ far_settings ['farreplace ' ][$ key ], $ buffer );
199+ }
200+ }
201+ }
202+ return $ buffer ;
203+ }
204+
205+ function is_form_in_list ($ buffer ,$ form_id ){
206+ $ pattern = '/<input(.*?)name=\"_wpcf7\"(.*)value=\"(.*?)\"/i ' ;
207+ preg_match_all ($ pattern , $ buffer , $ matches );
208+ return $ matches [3 ][0 ] == $ form_id ;
209+ }
210+
211+ function is_submition_above_limit ($ form_id ,$ limit ){
212+ global $ wpdb ;
213+ $ form_name = $ wpdb ->get_results ('SELECT `post_title` FROM ' . $ wpdb ->prefix . 'posts WHERE ID = ' . $ form_id );
214+ if ($ form_name ){
215+ $ form_name = $ form_name [0 ]->post_title ;
216+ }else {
217+ return false ;
218+ }
219+ $ result = $ wpdb ->get_results ('SELECT count(*) as "count" FROM ' . $ wpdb ->prefix . 'cf7dbplugin_submits WHERE form_name = \'' .$ form_name . '\'' );
220+ if ($ result && $ limit <= $ result [0 ]->count ){
221+ return true ;
222+ }
223+ return false ;
224+ }
225+
226+ function get_form_list (){
227+ global $ wpdb ;
228+ $ formList = $ wpdb ->get_results ('SELECT `post_title`,ID FROM ' . $ wpdb ->prefix . 'posts WHERE post_type = \'wpcf7_contact_form \'' );
229+ if ($ formList ){
230+ return $ formList ;
231+ }else {
232+ return null ;
233+ }
234+ }
235+ function far_template_redirect () {
236+ ob_start ();
237+ ob_start ( 'far_ob_call ' );
238+ }
239+
240+ //Add left menu item in admin
241+ add_action ( 'admin_menu ' , 'far_add_pages ' );
242+
243+ //Add additional links below plugin description on plugin page
244+ add_filter ( 'plugin_row_meta ' , 'far_plugin_meta ' , 10 , 2 );
245+
246+ //Handles find and replace for public pages
247+ add_action ( 'template_redirect ' , 'far_template_redirect ' );
0 commit comments