bws_custom_code_tab

The Google Sitemap by BestWebSoft bws custom code tab function.

Description

bws_custom_code_tab(); 

Usage

  1. if ( !function_exists( 'bws_custom_code_tab' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'google-sitemap-by-bestwebsoft/bws_menu/bws_functions.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = bws_custom_code_tab(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/bws_menu/bws_functions.php  
  1. function bws_custom_code_tab() { 
  2. if ( ! current_user_can( 'edit_plugins' ) ) 
  3. wp_die( __( 'You do not have sufficient permissions to edit plugins for this site.', 'bestwebsoft' ) ); 
  4.  
  5. global $bstwbsftwppdtplgns_options; 
  6.  
  7. $message = $content = ''; 
  8. $is_css_active = $is_php_active = false; 
  9.  
  10. $upload_dir = wp_upload_dir(); 
  11. $folder = $upload_dir['basedir'] . '/bws-custom-code'; 
  12. if ( ! $upload_dir["error"] ) { 
  13. if ( ! is_dir( $folder ) ) 
  14. wp_mkdir_p( $folder, 0755 ); 
  15.  
  16. $index_file = $upload_dir['basedir'] . '/bws-custom-code/index.php'; 
  17. if ( ! file_exists( $index_file ) ) { 
  18. if ( $f = fopen( $index_file, 'w+' ) ) 
  19. fclose( $f ); 
  20. }  
  21.  
  22. $css_file = 'bws-custom-code.css'; 
  23. $real_css_file = $folder . '/' . $css_file; 
  24.  
  25. $php_file = 'bws-custom-code.php';  
  26. $real_php_file = $folder . '/' . $php_file;  
  27.  
  28. $is_multisite = is_multisite(); 
  29. if ( $is_multisite ) 
  30. $blog_id = get_current_blog_id(); 
  31.  
  32. if ( isset( $_REQUEST['bws_update_custom_code'] ) && check_admin_referer( 'bws_update_' . $css_file ) ) { 
  33.  
  34. /** CSS */ 
  35. $newcontent_css = wp_unslash( $_POST['bws_newcontent_css'] );  
  36. if ( ! empty( $newcontent_css ) && isset( $_REQUEST['bws_custom_css_active'] ) ) { 
  37. if ( $is_multisite ) 
  38. $bstwbsftwppdtplgns_options['custom_code'][ $blog_id ][ $css_file ] = $upload_dir['baseurl'] . '/bws-custom-code/' . $css_file; 
  39. else 
  40. $bstwbsftwppdtplgns_options['custom_code'][ $css_file ] = $upload_dir['baseurl'] . '/bws-custom-code/' . $css_file; 
  41. } else { 
  42. if ( $is_multisite ) { 
  43. if ( isset( $bstwbsftwppdtplgns_options['custom_code'][ $blog_id ][ $css_file ] ) ) 
  44. unset( $bstwbsftwppdtplgns_options['custom_code'][ $blog_id ][ $css_file ] ); 
  45. } else { 
  46. if ( isset( $bstwbsftwppdtplgns_options['custom_code'][ $css_file ] ) ) 
  47. unset( $bstwbsftwppdtplgns_options['custom_code'][ $css_file ] ); 
  48. if ( $f = fopen( $real_css_file, 'w+' ) ) { 
  49. fwrite( $f, $newcontent_css ); 
  50. fclose( $f ); 
  51. $message .= sprintf( __( 'File %s edited successfully.', 'bestwebsoft' ), '<i>' . $css_file . '</i>' ) . ' '; 
  52. } else { 
  53. $error .= __( 'Not enough permissions to create or update the file', 'bestwebsoft' ) . ' ' . $real_css_file . '. '; 
  54.  
  55. /** PHP */ 
  56. $newcontent_php = wp_unslash( trim( $_POST['bws_newcontent_php'] ) ); 
  57. if ( file_exists( $index_file ) ) { 
  58. if ( ! empty( $newcontent_php ) && isset( $_REQUEST['bws_custom_php_active'] ) ) { 
  59. if ( $is_multisite ) 
  60. $bstwbsftwppdtplgns_options['custom_code'][ $blog_id ][ $php_file ] = $real_php_file; 
  61. else 
  62. $bstwbsftwppdtplgns_options['custom_code'][ $php_file ] = $real_php_file; 
  63. } else { 
  64. if ( $is_multisite ) { 
  65. if ( isset( $bstwbsftwppdtplgns_options['custom_code'][ $blog_id ][ $php_file ] ) ) 
  66. unset( $bstwbsftwppdtplgns_options['custom_code'][ $blog_id ][ $php_file ] ); 
  67. } else { 
  68. if ( isset( $bstwbsftwppdtplgns_options['custom_code'][ $php_file ] ) ) 
  69. unset( $bstwbsftwppdtplgns_options['custom_code'][ $php_file ] ); 
  70.  
  71. if ( $f = fopen( $real_php_file, 'w+' ) ) { 
  72. $newcontent_php = $newcontent_php; 
  73. fwrite( $f, $newcontent_php ); 
  74. fclose( $f ); 
  75. $message .= sprintf( __( 'File %s edited successfully.', 'bestwebsoft' ), '<i>' . $php_file . '</i>' ); 
  76. } else { 
  77. $error .= __( 'Not enough permissions to create or update the file', 'bestwebsoft' ) . ' ' . $real_php_file . '. '; 
  78. } else { 
  79. $error .= __( 'Not enough permissions to create the file', 'bestwebsoft' ) . ' ' . $index_file . '. '; 
  80. }  
  81.  
  82. if ( ! empty( $error ) ) 
  83. $error .= ' <a href="https://codex.wordpress.org/Changing_File_Permissions" target="_blank">' . __( 'Learn more', 'bestwebsoft' ) . '</a>'; 
  84.  
  85. if ( $is_multisite ) 
  86. update_site_option( 'bstwbsftwppdtplgns_options', $bstwbsftwppdtplgns_options ); 
  87. else 
  88. update_option( 'bstwbsftwppdtplgns_options', $bstwbsftwppdtplgns_options ); 
  89.  
  90. if ( file_exists( $real_css_file ) ) { 
  91. update_recently_edited( $real_css_file ); 
  92. $content_css = esc_textarea( file_get_contents( $real_css_file ) ); 
  93. if ( ( $is_multisite && isset( $bstwbsftwppdtplgns_options['custom_code'][ $blog_id ][ $css_file ] ) ) || 
  94. ( ! $is_multisite && isset( $bstwbsftwppdtplgns_options['custom_code'][ $css_file ] ) ) ) { 
  95. $is_css_active = true; 
  96. if ( file_exists( $real_php_file ) ) { 
  97. update_recently_edited( $real_php_file ); 
  98. $content_php = esc_textarea( file_get_contents( $real_php_file ) ); 
  99. if ( ( $is_multisite && isset( $bstwbsftwppdtplgns_options['custom_code'][ $blog_id ][ $php_file ] ) ) || 
  100. ( ! $is_multisite && isset( $bstwbsftwppdtplgns_options['custom_code'][ $php_file ] ) ) ) { 
  101. $is_php_active = true; 
  102. } else { 
  103. $content_php = "<?php" . "\n" . "if ( ! defined( 'ABSPATH' ) ) exit;" . "\n" . "if ( ! defined( 'BWS_GLOBAL' ) ) exit;" . "\n\n" . "/** Start your code here */" . "\n"; 
  104.  
  105. if ( ! empty( $message ) ) { ?> 
  106. <div id="message" class="below-h2 updated notice is-dismissible"><p><?php echo $message; ?></p></div> 
  107. <?php } ?>  
  108. <form action="" method="post"> 
  109. <?php foreach ( array( 'css', 'php' ) as $extension ) { ?>  
  110. <p> 
  111. <?php if ( 'css' == $extension ) 
  112. _e( 'These styles will be added to the header on all pages of your site.', 'bestwebsoft' ); 
  113. else 
  114. printf( __( 'This PHP code will be hooked to the %s action and will be printed on front end only.', 'bestwebsoft' ), '<a href="http://codex.wordpress.org/Plugin_API/Action_Reference/init" target="_blank"><code>init</code></a>' ); ?> 
  115. </p> 
  116. <p><big> 
  117. <?php if ( ! file_exists( ${"real_{$extension}_file"} ) || ( is_writeable( ${"real_{$extension}_file"} ) ) ) { 
  118. echo __( 'Editing', 'bestwebsoft' ) . ' <strong>' . ${"{$extension}_file"} . '</strong>'; 
  119. } else { 
  120. echo __( 'Browsing', 'bestwebsoft' ) . ' <strong>' . ${"{$extension}_file"} . '</strong>'; 
  121. } ?> 
  122. </big></p> 
  123. <p><label><input type="checkbox" name="bws_custom_<?php echo $extension; ?>_active" value="1" <?php if ( ${"is_{$extension}_active"} ) echo "checked"; ?> /> <?php _e( 'Activate', 'bestwebsoft' ); ?></label></p> 
  124. <textarea cols="70" rows="25" name="bws_newcontent_<?php echo $extension; ?>" id="bws_newcontent_<?php echo $extension; ?>"><?php if ( isset( ${"content_{$extension}"} ) ) echo ${"content_{$extension}"}; ?></textarea> 
  125. <p class="description"> 
  126. <a href="<?php echo ( 'css' == $extension ) ? 'https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Getting_started' : 'http://php.net/' ?>" target="_blank"> 
  127. <?php printf( __( 'Learn more about %s', 'bestwebsoft' ), strtoupper( $extension ) ); ?>  
  128. </a> 
  129. </p>  
  130. <?php } 
  131. if ( ( ! file_exists( $real_css_file ) || is_writeable( $real_css_file ) ) && ( ! file_exists( $real_php_file ) || is_writeable( $real_php_file ) ) ) { ?> 
  132. <p class="submit"> 
  133. <input type="hidden" name="bws_update_custom_code" value="submit" />  
  134. <?php submit_button( __( 'Save Changes', 'bestwebsoft' ), 'primary', 'submit', false );  
  135. wp_nonce_field( 'bws_update_' . $css_file ); ?> 
  136. </p> 
  137. <?php } else { ?> 
  138. <p><em><?php printf( __( 'You need to make this files writable before you can save your changes. See %s the Codex %s for more information.', 'bestwebsoft' ),  
  139. '<a href="https://codex.wordpress.org/Changing_File_Permissions" target="_blank">',  
  140. '</a>' ); ?></em></p> 
  141. <?php } ?> 
  142. </form> 
  143. <?php }