<?php

function rscf_message_plugin_url( $path = '' ) {
  $url = plugins_url( $path, RSCF_MESSAGE_FILE );

  if ( is_ssl()
  and 'http:' == substr( $url, 0, 5 ) ) {
    $url = 'https:' . substr( $url, 5 );
  }

  return $url;
}
function rscf_file_require ( $file_path ) {
    require_once ( $file_path );
}
/*
 * Render HTML
 */
if ( ! function_exists( 'rsfc_message_settings_render' ) ) {
function rsfc_message_settings_render( $title, $body ) {
    echo '<div id="wrapper" class="mobile">';
    echo '<div class="row">';
    echo '<div class="col-md-12">';
    echo '<div class="panel_s">';
    //title start
    echo '<div class="panel-body">';
    echo '<h4>' . esc_html_e( $title ) . '</h4>';
    echo '</div>';
    //title end
    echo '<div class="panel-body">';
    echo $body;
    echo '</div>';
    echo '</div>';
    echo '</div>';
    echo '</div>';
    echo '</div>';
}
}

if ( ! function_exists( 'rscf_message_admin_email_setting_changed' ) ) {
function rscf_message_admin_email_setting_changed() {
    ob_start();
    ?>
    <input type="hidden" id="security" name="security" value="<?php echo wp_create_nonce( 'of_ajax_nonce' ); ?>"/>
   <form id="mail_form" method="post" action="<?php echo esc_attr( $_SERVER['REQUEST_URI'] ) ?>">
      <?php 
        settings_errors(); 
        settings_fields( 'rscf_message_smtp_form_settings_admin' );
        do_settings_sections( 'rscf_message_smtp_settings_page' );
      ?>
          <button id="of_save_admin" type="button" class="btn btn-info save-and-add-contact save_button save_mail"> <?php esc_html_e( 'Save Changes', 'contact' ); ?></button>
    </form>

    <?php

    $body = ob_get_clean();

    echo $body;
}
}
add_action( 'rscf_message_admin_email_setting', 'rscf_message_admin_email_setting_changed', 10 );

if ( ! function_exists( 'rscf_message_admin_message_process_changed' ) ) {
function rscf_message_admin_message_process_changed() {
    ob_start();
    ?>
    <input type="hidden" id="security_process" name="security" value="<?php echo wp_create_nonce( 'of_ajax_nonce' ); ?>"/>
    <form id="process_form" method="post" action="<?php echo esc_attr( $_SERVER['REQUEST_URI'] ) ?>">
     <?php 
        settings_errors();
        settings_fields( 'rscf_message_form_settings' );
        do_settings_sections( 'rscf_message_process_settings' );
     ?>
     <button id="of_save_process" type="button" class="btn btn-info save-and-add-contact save_button save_process"> <?php esc_html_e( 'Save Changes', 'contact' ); ?></button>
    </form>

    <?php

    $body = ob_get_clean();

    echo $body;
}
}
add_action( 'rscf_message_admin_process_setting', 'rscf_message_admin_message_process_changed', 10 );

if ( ! function_exists( 'rscf_message_admin_message_style_setting' ) ) {
function rscf_message_admin_message_style_setting() {
    ob_start();
    ?>
    <input type="hidden" id="security_process" name="security" value="<?php echo wp_create_nonce( 'of_ajax_nonce' ); ?>"/>
    <form id="style_process_form" method="post" action="<?php echo esc_attr( $_SERVER['REQUEST_URI'] ) ?>">
     <?php 
        settings_errors();
        settings_fields( 'rscf_message_design_settings' );
        do_settings_sections( 'rscf_message_style_settings' );
     ?>
     <button id="of_save_style" type="button" class="btn btn-info save-and-add-contact save_button save_style"> <?php esc_html_e( 'Save Changes', 'contact' ); ?></button>
    </form>

    <?php

    $body = ob_get_clean();

    echo $body;
}
}
add_action( 'rscf_message_form_style', 'rscf_message_admin_message_style_setting', 30 );


if ( ! function_exists( 'rscf_message_css_code' ) ) {
function rscf_message_css_code() {
    ob_start();
    ?>
    <style>
      <?php echo rscf_message_front_design_data('css_code'); ?>
    </style>
    <?php

    $body = ob_get_clean();

    echo $body;
}
}
add_action( 'wp_head', 'rscf_message_css_code', 100 );

if ( ! function_exists( 'rscf_message_js_code' ) ) {
function rscf_message_js_code() {
    ob_start();
    ?>
    <script>
      <?php echo rscf_message_front_design_data('js_code'); ?>
    </script>
    <?php

    $body = ob_get_clean();

    echo $body;
}
}
add_action( 'wp_footer', 'rscf_message_js_code', 100);

if ( ! function_exists( 'rscf_message_admin_message_document_setting' ) ) {
function rscf_message_admin_message_document_setting(){
  ob_start();
    ?>
    <h4>How to configure a Contact Form.</h4>
    <h5> Using Gmail</h5>
    <p>To be able to use a Gmail email address in your contact form, you need to make some changes in your Gmail account settings. This only applies if you want to send From the Gmail account. Skip this part if you use Gmail only in the To field.</p>

<p>Google may block sign-in attempts from IPs which are located far from the usual geographical location of the mailbox owner. In such case, the “Suspicious sign in is prevented” email will appear in the Gmail inbox.</p>

<p>There are three steps:</p>

<p>1. Sign into your Gmail account at https://myaccount.google.com/security?pli=1#signin and find the <strong>Password & sign-in method</strong> section. Turn <strong>2-Step Verification</strong> off, if you are using it (mail script will not be able to log in to the account when 2FA is on): </p>
<div class="doc_s">
  <div class="doc-body">
    <img src="<?php echo RSCF_MESSAGE_PLUGIN_IMAGE .'contact_from_01.png';?>" alt="1">
  </div>
</div>
<p>2. Scroll down to the <strong>Apps with account access</strong> section and set <strong>Allow less secure apps: ON</strong>:</p>
<div class="doc_s">
  <div class="doc-body">
    <img src="<?php echo RSCF_MESSAGE_PLUGIN_IMAGE .'contact_from_02.png';?>" alt="2">
  </div>
</div>
<p> Next, go to https://accounts.google.com/DisplayUnlockCaptcha and click <strong>Continue</strong>:</p>
<div class="doc_s">
  <div class="doc-body">
    <img src="<?php echo RSCF_MESSAGE_PLUGIN_IMAGE .'contact_from_03.png';?>" alt="3">
  </div>
</div>

<p>
  Now you can configure and test your contact form. Use the following SMTP settings:</p>
  <div class="doc_s">
  <pre>
  Email sending method: SMTP;
  SMTP authentication: enabled;
  SMTP username: yourmailbox@gmail.com;
  SMTP password: password for this email address;
  SMTP hostname: smtp.gmail.com;
  SMTP port: 465 with SSL on.
  </pre>

</div>
  <?php

    $body = ob_get_clean();

    echo $body;
}
}
add_action( 'rscf_message_admin_message_document', 'rscf_message_admin_message_document_setting', 30 );

if ( ! function_exists( 'rscf_message_admin_message_shortcode_setting' ) ) {
function rscf_message_admin_message_shortcode_setting(){
  ob_start();
    ?>
    <h4>Display Form</h4>
    <p>[show_contact_form]</p>

  <?php

    $body = ob_get_clean();

    echo $body;
}
}
add_action( 'rscf_message_admin_message_shortcode', 'rscf_message_admin_message_shortcode_setting', 40 );
if ( ! function_exists( 'rscf_message_admin_message_support' ) ) {
function rscf_message_admin_message_support(){
  ob_start();
    ?>
    <div class="support-box">
      <h4>Support</h4>
      <ul class="contact-infobox-list-social">
          <li class="facebook"><a href="https://www.facebook.com/abu.sayed.russell.036" rel="nofollow" target="_blank" title="Facebook"><i class="fa fa-facebook-square" aria-hidden="true"></i>
  </a></li>
          <li class="skype"><a href="skype:abusayedrussell?chat" rel="nofollow" target="_blank" title="Skype"><i class="fa fa-skype" aria-hidden="true"></i>
  </a></li>
          <li class="linkedin"><a href="https://linkedin.com/in/abusayedrussell" rel="nofollow" target="_blank" title="Linkedin"><i class="fa fa-linkedin-square" aria-hidden="true"></i>
  </a></li>
          <li class="twitter"><a href="https://twitter.com/abusayedrussell" rel="nofollow" target="_blank" title="Twitter"><i class="fa fa-twitter-square" aria-hidden="true"></i>
  </a></li>
          <li><strong>Connect with us!</strong></li>
      </ul>
    </div>

  <?php
    $body = ob_get_clean();
    echo $body;
}
}
add_action( 'rscf_message_admin_support', 'rscf_message_admin_message_support', 50 );

//Option Data
if ( ! function_exists( 'rscf_message_smtp_data' ) ) {
function rscf_message_smtp_data($key_name){
 $smtps  = get_option( 'russell_smtp' ) ?: array();
 $option_data = isset($smtps[1][$key_name]) ? $smtps[1][$key_name]: "";
return $option_data;
}
}
if ( ! function_exists( 'rscf_front_message_data' ) ) {
  function rscf_message_front_message_data($key_name){
  $processOption  = get_option( 'russell_process' ) ?: array();
  $option_data = isset($processOption[1][$key_name]) ? $processOption[1][$key_name]: "";
  return $option_data;
  }
}
if ( ! function_exists( 'rscf_message_front_design_data' ) ) {
function rscf_message_front_design_data($key_name){
  $design_options  = get_option( 'russell_design' ) ?: array();
  $option_data = isset($design_options[1][$key_name]) ? $design_options[1][$key_name]: "";
  return $option_data;
}
}
//Time Ago
if ( ! function_exists( 'rscf_time_ago' ) ) {
function rscf_time_ago( $datetime, $full = false ) {
 $today = time();
 $createdday = strtotime( $datetime );
 $datediff = abs( $today - $createdday );
 $difftext = "";
 $years = floor( $datediff / ( 365 * 60 * 60 * 24 ) );
 $months = floor( ( $datediff - $years * 365 * 60 * 60 * 24 ) / ( 30 * 60 * 60 * 24 ) );
 $days = floor( ( $datediff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24 ) / ( 60 * 60 * 24 ) );
 $hours = floor( $datediff / 3600 );
 $minutes = floor( $datediff / 60 );
 $seconds = floor( $datediff );
 // Years
 if ( $difftext == "" ) {
   if ( $years > 1 )
     $difftext = $years . ' years ago';
   elseif ( $years == 1 )
     $difftext = $years . ' year ago';
 }
 // Mounth
 if ( $difftext == "" ) {
   if ( $months > 1 )
     $difftext = $months . ' months ago';
   elseif ( $months == 1 )
     $difftext = $months . ' month ago';
 }
 // Days
 if ( $difftext == "" ) {
   if ( $days > 1 )
     $difftext = $days . ' days ago';
   elseif ( $days == 1 )
     $difftext = $days . ' day ago';
 }
 // Hours
 if ( $difftext == "" ) {
   if ( $hours > 1 )
     $difftext = $hours . ' hours ago';
   elseif ( $hours == 1 )
     $difftext = $hours . ' hour ago';
 }
 // Minutes
 if ( $difftext == "" ) {
   if ( $minutes > 1 )
     $difftext = $minutes . ' minutes ago';
   elseif ( $minutes == 1 )
     $difftext = $minutes . ' minute ago';
 }
 // Seconds
 if ( $difftext == "" ) {
   if ( $seconds > 1 )
     $difftext = $seconds . ' seconds ago';
   elseif ( $seconds == 1 )
     $difftext = $seconds . ' second ago';
 }
 return $difftext;
}
}
if ( ! function_exists( 'rscf_messageShorten' ) ) {
function rscf_messageShorten($text, $limit = 400)
    {
        $text = $text . " ";
        $text = substr($text, 0, $limit);
        $text = substr($text, 0, strrpos($text, ' '));
        $text = $text;
        return $text;
    }
  }
if ( ! function_exists( 'rscf_message_posts_table' ) ) {
function rscf_message_posts_table(){
  global $wpdb;
  return $wpdb->prefix . "posts";
}
}
if ( ! function_exists( 'rscf_message_meta_table' ) ) {
function rscf_message_meta_table(){
  global $wpdb;
  return $wpdb->prefix . "postmeta";
}
}
if ( ! function_exists( 'rscf_get_messages' ) ) {
function rscf_get_messages($post_type){
  global $wpdb;
  $data = array();

   $get_messages = $wpdb->get_results( "SELECT ID, post_type, post_title, post_content, post_excerpt, post_date, post_status, post_author FROM ".rscf_message_posts_table()." WHERE post_type = 'russell-contact' AND post_status = '{$post_type}' ORDER BY post_date DESC" );

   if ( isset( $get_messages ) && count( $get_messages ) > 0 ){
      foreach ($get_messages as $row) {
        $messages_extra = $wpdb->get_results(
            $wpdb->prepare( "SELECT * FROM ".rscf_message_meta_table()." WHERE post_id = %d", $row->ID )
          );
        $row->msg_id       = $row->ID;
        $row->msg_type     = $row->post_type;
        $row->sender_name  = $row->post_title;
        $row->message      = $row->post_content;
        $row->view_status  = $row->post_excerpt;
        $row->msg_date     = $row->post_date;
        $row->msg_status     = $row->post_status;
        $row->auth_id     = $row->post_author;
      
        if ( isset( $messages_extra ) && count( $messages_extra ) > 0 ){
         foreach ($messages_extra as $messages_extra_row) {
       
            if (!empty($messages_extra_row) && $messages_extra_row->meta_key == '_contact_email_value_key') {
                if (!empty($messages_extra_row->meta_value)) {
                   $row->email = $messages_extra_row->meta_value;
                } else {
                  $row->email = '';
                }
            }
            if (!empty($messages_extra_row) && $messages_extra_row->meta_key == '_contact_subject_value_key') {
                if (!empty($messages_extra_row->meta_value)) {
                   $row->subject = $messages_extra_row->meta_value;
                } else {
                  $row->subject = '';
                }
            }
         }
        }
        array_push($data, $row);
      }
    }
return $data;
}
}
if ( ! function_exists( 'rscf_unread_messages' ) ) {
function rscf_unread_messages(){
  global $wpdb;
   $get_messages = $wpdb->get_results( "SELECT * FROM ".rscf_message_posts_table()." WHERE post_type = 'russell-contact' AND post_excerpt = 'inbox_new'" );

return count($get_messages);
}
}
if ( ! function_exists( 'rscf_total_messages' ) ) {
function rscf_total_messages($post_type){
  global $wpdb;
   $get_messages = $wpdb->get_results( "SELECT * FROM ".rscf_message_posts_table()." WHERE post_type = 'russell-contact' AND post_status = '{$post_type}'" );

return count($get_messages);
}
}
if ( ! function_exists( 'rscf_get_message_by_id' ) ) {
function rscf_get_message_by_id($id){
  global $wpdb;
  $data = array();
  $get_messages = $wpdb->get_row(
      $wpdb->prepare( "SELECT ID, post_type, post_title, post_content, post_excerpt, post_date, post_status, post_author FROM ".rscf_message_posts_table()." WHERE post_type = %s AND ID = %d", 'russell-contact', $id )
    );

   if ( isset( $get_messages ) ){
        $messages_extra = $wpdb->get_results(
            $wpdb->prepare( "SELECT * FROM ".rscf_message_meta_table()." WHERE post_id = %d", $get_messages->ID )
          );
        $data['msg_id'] = $get_messages->ID;
        $data['msg_type'] = $get_messages->post_type;
        $data['sender_name'] = $get_messages->post_title;
        $data['message'] = $get_messages->post_content;
        $data['view_status'] = $get_messages->post_excerpt;
        $data['msg_date'] = $get_messages->post_date;
        $data['msg_status'] = $get_messages->post_status;
        $data['auth_id'] = $get_messages->post_author;

        if ( isset( $messages_extra ) && count( $messages_extra ) > 0 ){
         foreach ($messages_extra as $messages_extra_row) {
            if (!empty($messages_extra_row) && $messages_extra_row->meta_key == '_contact_email_value_key') {
                if (!empty($messages_extra_row->meta_value)) {
                   $data['email'] = $messages_extra_row->meta_value;
                } else {
                  $data->email = '';
                }
            }

            if (!empty($messages_extra_row) && $messages_extra_row->meta_key == '_contact_subject_value_key') {
                if (!empty($messages_extra_row->meta_value)) {
                   $data['subject'] = $messages_extra_row->meta_value;
                } else {
                  $data->subject = '';
                }
            }
         }
        }
    }
return $data;
}
}
//User Name

function rscf_message_display_name($user_id) {
    if (!$user = get_userdata($user_id))
        return false;
    return $user->data->display_name;
}
    

