<?php

include('customizer.php');

// Kompetencje

function kompetencje() {

$labels = array(
'name'                  => _x( 'Kompetencje', 'Post Type General Name', 'kategorie' ),
'singular_name'         => _x( 'Kompetencje', 'Post Type Singular Name', 'kategorie' ),
'menu_name'             => __( 'Kompetencje', 'kategorie' ),
'name_admin_bar'        => __( 'Kompetencje', 'kategorie' ),
'archives'              => __( 'Item Archives', 'kategorie' ),
'attributes'            => __( 'Item Attributes', 'kategorie' ),
'parent_item_colon'     => __( 'Parent Item:', 'kategorie' ),
'all_items'             => __( 'Wszytko', 'kategorie' ),
'add_new_item'          => __( 'Nowy', 'kategorie' ),
'add_new'               => __( 'Nowy', 'kategorie' ),
'new_item'              => __( 'Nowy', 'kategorie' ),
'edit_item'             => __( 'Edytuj', 'kategorie' ),
'update_item'           => __( 'Aktualizuj', 'kategorie' ),
'view_item'             => __( 'Zobacz', 'kategorie' ),
'view_items'            => __( 'Zobacz', 'kategorie' ),
'search_items'          => __( 'Szukaj', 'kategorie' ),
'not_found'             => __( 'Brak', 'kategorie' ),
'not_found_in_trash'    => __( 'Brak', 'kategorie' ),
'featured_image'        => __( 'Obrazek', 'kategorie' ),
'set_featured_image'    => __( 'Ustaw obrazek', 'kategorie' ),
'remove_featured_image' => __( 'Usuń obrazek', 'kategorie' ),
'use_featured_image'    => __( 'Użyj jako obrazek wyróżniający', 'kategorie' ),
'insert_into_item'      => __( 'Insert into item', 'kategorie' ),
'uploaded_to_this_item' => __( 'Uploaded to this item', 'kategorie' ),
'items_list'            => __( 'Items list', 'kategorie' ),
'items_list_navigation' => __( 'Items list navigation', 'kategorie' ),
'filter_items_list'     => __( 'Filter items list', 'kategorie' ),
);
$args = array(
'label'                 => __( 'Kompetencje', 'kategorie' ),
'description'           => __( 'Kompetencje', 'kategorie' ),
'labels'                => $labels,
'supports'              => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt' ),
'taxonomies'            => array( 'kategorie' ),
'hierarchical'          => false,
'public'                => true,
'show_ui'               => true,
'show_in_menu'          => true,
'menu_position'         => 6,
'show_in_admin_bar'     => true,
'show_in_nav_menus'     => true,
'can_export'            => true,
'has_archive'           => true,
'exclude_from_search'   => false,
'menu_icon'           => 'dashicons-thumbs-up',
'publicly_queryable'    => true,
'capability_type'       => 'page',
);
register_post_type( 'kompetencje', $args );

}
add_action( 'init', 'kompetencje', 100 );

// Zmienne

function zmienne() {

$labels = array(
'name'                       => _x( 'Kategorie', 'Taxonomy General Name', 'kategorie' ),
'singular_name'              => _x( 'Kategorie', 'Taxonomy Singular Name', 'kategorie' ),
'menu_name'                  => __( 'Kategorie', 'kategorie' ),
'all_items'                  => __( 'Wszystko', 'kategorie' ),
'parent_item'                => __( 'Rodzic', 'kategorie' ),
'parent_item_colon'          => __( 'Rodzic:', 'kategorie' ),
'new_item_name'              => __( 'Dodaj', 'kategorie' ),
'add_new_item'               => __( 'Dodaj', 'kategorie' ),
'edit_item'                  => __( 'Edytuj', 'kategorie' ),
'update_item'                => __( 'Aktualizuj', 'kategorie' ),
'view_item'                  => __( 'Zobacz', 'kategorie' ),
'separate_items_with_commas' => __( 'Oddziel przecinkami', 'kategorie' ),
'add_or_remove_items'        => __( 'Dodaj lub usuń', 'kategorie' ),
'choose_from_most_used'      => __( 'Wybierz z najczęściej używanych', 'kategorie' ),
'popular_items'              => __( 'Popularne', 'kategorie' ),
'search_items'               => __( 'Szukaj', 'kategorie' ),
'not_found'                  => __( 'Nie znaleziono', 'kategorie' ),
'no_terms'                   => __( 'Brak', 'kategorie' ),
'items_list'                 => __( 'Lista', 'kategorie' ),
'items_list_navigation'      => __( 'Lista', 'kategorie' ),
);
$rewrite = array(
'slug'                       => 'kategorie',
'with_front'                 => true,
'hierarchical'               => true,
);
$args = array(
'labels'                     => $labels,
'hierarchical'               => true,
'public'                     => true,
'show_ui'                    => true,
'show_admin_column'          => true,
'show_in_nav_menus'          => true,
'show_tagcloud'              => true,
'rewrite'                    => $rewrite,
);
register_taxonomy( 'kategorie', array( 'kompetencje' ), $args );
}

add_action( 'init', 'zmienne', 100 );

add_theme_support( 'post-thumbnails', array( 'kompetencje' ) );

add_action( 'user_meta_after_user_update', 'user_meta_after_user_update_function',10,2 );
function user_meta_after_user_update_function( $response, $formName ){
global $userMeta;
if( $formName == 'ankieta1' ){
echo $userMeta->jsRedirect( 'https://kolejnykrok.absolwencinawalizkach.pl/sprawdz-kompetencje-krok-2/' );
}

if( $formName == 'ankieta2' ){
echo $userMeta->jsRedirect( 'https://kolejnykrok.absolwencinawalizkach.pl/sprawdz-kompetencje-krok-3/' );
}

if( $formName == 'ankieta3' ){
echo $userMeta->jsRedirect( 'https://kolejnykrok.absolwencinawalizkach.pl/sprawdz-kompetencje-wynik/' );
}
}


function wpcodex_set_capabilities() {
$editor = get_role( 'subscriber' );
$caps = array(
'read',
);

foreach ( $caps as $cap ) {
$editor->remove_cap( $cap );
}
}
add_action( 'init', 'wpcodex_set_capabilities' );

function pippin_registration_form_fields() {
ob_start(); ?>
<h3 class="pippin_header">Rozpocznij ankietę</h3>
<form id="pippin_registration_form" class="pippin_form" method="POST">
<fieldset>
<p>
<label for="pippin_user_first">Twoję imię: </label>
<input name="pippin_user_first" id="pippin_user_first" type="text"/>
</p>
<p>
<input type="hidden" name="pippin_register_nonce" value="<?php echo wp_create_nonce('pippin-register-nonce'); ?>"/>
<input type="submit" value="Rozpocznij ankietę"/>
</p>
</fieldset>
</form>
<?php
return ob_get_clean();
}

function pippin_add_new_member() {
if (wp_verify_nonce($_POST['pippin_register_nonce'], 'pippin-register-nonce')) {
$user_first 	= $_POST["pippin_user_first"];
require_once(ABSPATH . WPINC . '/registration.php');
$user_login='ankieta_'.date('Y_m_d_H_i');
$new_user_id = wp_insert_user(array(
'user_login'		=> $user_login,
'user_pass'	 		=> $user_pass,
'first_name'		=> $user_first,
'user_registered'	=> date('Y-m-d H:i:s'),
'role'				=> 'subscriber'
)
);
if($new_user_id) {
//wp_new_user_notification($new_user_id);
wp_setcookie($user_login, $user_pass, true);
wp_set_current_user($new_user_id, $user_login);
do_action('wp_login', $user_login);
wp_redirect( '/sprawdz-kompetencje/' ); exit;
}
}
}
add_action('init', 'pippin_add_new_member');

/*
function create_account(){
//You may need some data validation here
$user = ( isset($_POST['uname']) ? $_POST['uname'] : '' );
//  $pass = ( isset($_POST['upass']) ? $_POST['upass'] : '' );
$email = ( isset($_POST['uemail']) ? $_POST['uemail'] : '' );

if ( !username_exists( $user ) ) {
$user_id = wp_create_user( $user, $pass, $email.'@wp.pl');
if( !is_wp_error($user_id) ) {
//user has been created
$user = new WP_User( $user_id );
$user->set_role( 'subscriber' );
//Redirect
wp_redirect( '/ankieta-2/' );
exit;
} else {
//$user_id is a WP_Error object. Manage the error
}
}

}
add_action('init','create_account');
*/

function autologin( $user_id ) {
wp_set_current_user( $user_id );
wp_set_auth_cookie( $user_id );
}
add_action( 'user_register', 'autologin' );

/*
function admin_default_page() {
wp_redirect( home_url( '/ankieta-1/' ) );
}
add_filter('login_redirect', 'admin_default_page');
*/

add_filter('use_block_editor_for_post', '__return_false');
add_filter( 'wpmu_signup_user_notification', '__return_false' );
if ( ! current_user_can( 'manage_options' ) ) { add_filter('show_admin_bar', '__return_false'); }

// security

remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'feed_links', 2);
remove_action('wp_head', 'feed_links_extra', 3);
remove_action('wp_head', 'index_rel_link');
remove_action('wp_head', 'start_post_rel_link', 10, 0);
remove_action('wp_head', 'parent_post_rel_link', 10, 0);
remove_action('wp_head', 'adjacent_posts_rel_link', 10, 0);
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);

// dashboard

add_action('admin_init', 'rw_remove_dashboard_widgets');
function rw_remove_dashboard_widgets() {
remove_meta_box('dashboard_right_now', 'dashboard', 'normal');
remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
remove_meta_box('dashboard_incoming_links', 'dashboard', 'normal');
remove_meta_box('dashboard_plugins', 'dashboard', 'normal');
remove_meta_box('dashboard_quick_press', 'dashboard', 'normal');
remove_meta_box('dashboard_recent_drafts', 'dashboard', 'normal');
remove_meta_box('dashboard_primary', 'dashboard', 'normal');
remove_meta_box('dashboard_secondary', 'dashboard', 'normal');
remove_meta_box( 'backwpup_become_inpsyder', 'dashboard', 'normal' );
}

// menu

function register_my_menu() {
register_nav_menu('menu1',__( 'Menu górne' ));
register_nav_menu('menu2',__( 'Menu dolne' ));
}
add_action( 'init', 'register_my_menu' );

// usunięcie ikonki z admin bar

add_action( 'admin_bar_menu', 'remove_wp_logo', 999 );

function remove_wp_logo( $wp_admin_bar ) {
$wp_admin_bar->remove_node( 'wp-logo' );
}

// usunięcie help

add_action('admin_head', 'mytheme_remove_help_tabs');
function mytheme_remove_help_tabs() {
$screen = get_current_screen();
$screen->remove_help_tabs();
}
// antyspam

function wpcodex_hide_email_shortcode( $atts , $content = null ) {
if ( ! is_email( $content ) ) {
return;
}
return '<a href="mailto:' . antispambot( $content ) . '">' . antispambot( $content ) . '</a>';
}

add_shortcode( 'email', 'wpcodex_hide_email_shortcode' );

// support

add_theme_support( 'post-thumbnails', array( 'post', 'page' ) );

function wpcodex_add_excerpt_support_for_pages() {
add_post_type_support( 'page', 'excerpt' );
}
add_action( 'init', 'wpcodex_add_excerpt_support_for_pages' );

// logowanie

add_filter( 'login_headerurl', 'custom_loginlogo_url' );
function custom_loginlogo_url($url) {
return 'https://kolejnykrok.absolwencinawalizkach.pl';
}

function my_login_logo() { ?>
<style type="text/css">

.g-recaptcha,.wppb-form-field.wppb-recaptcha {
transform: scale(0.75);
transform-origin: 0 0;
padding-left: 0px !important;
}

#login h1 a {
display:none;
}

body.login {

background: url(<?php echo get_stylesheet_directory_uri(); ?>/img/home/background.png) no-repeat center top fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}

body #login{
padding: 3% 0 0 !important;
margin: auto 0px auto auto !important;
}

.login #nav {
text-align:center !important;
padding: 0 63px 0 0px !important;
}

#backtoblog {
display: none;
}

#login h1 {
margin-left:-10px !important;
}
#login form {
text-align:center !important;
width:65% !important;
padding: 20px 24px 25px !important;
background:rgba(0, 0, 0, 0.6) !important;
border-radius:10px !important;
-webkit-border-radius: 10px !important;
-moz-border-radius: 10px !important;
}

#login label {
color:#ffffff !important;
}
#login form .forgetmenot {
text-align:center !important;
margin:0 auto !important;
float:none !important;
}

form#loginform p.submit {
margin-top:20px !important;
}

form#loginform p.submit {
margin-top:30px !important;
}

.wp-core-ui .button.button-large {
height:40px !important;
padding:0 20px 2px !important;
text-transform:uppercase !important;
font-size:16px !important;
}

.wp-core-ui .button-primary {
background:#494C43 !important;
border-color: #494C43 !important;
box-shadow: 0 1px 0 #494C43 !important;
text-shadow: 0 -1px 1px #494C43,1px 0 1px #494C43,0 1px 1px #494C43,-1px 0 1px #494C43 !important;
}

.wp-core-ui .button-primary:hover, .wp-core-ui .button-primary:focus, .wp-core-ui .button-primary:active {
background:#BE1D2D !important;
border-color:#BE1D2D !important;
box-shadow:none !important;
}

.login .button-primary {
width:100% !important;
}

input[type="text"],  input[type="password"], input[type="text"]:focus,  input[type="password"]:focus {
box-shadow:none !important;
}
input[type="text"],  input[type="password"], input[type="checkbox"] {
border: 1px solid #009993 !important;
}
input[type="text"]:focus,  input[type="password"]:focus {
border-color: #4E576B !important;
}
}
</style>
<?php }

add_action( 'login_enqueue_scripts', 'my_login_logo' );

// poziom 2

function wo_second_editor($post) {

global $post, $page;
if ( 'kompetencje' == get_post_type() ){
echo '<h2 style="font-size:20px;">Poziom 2.</h2>';
$content = get_post_meta($post->ID, 'wo_blue_box' , true ) ;
wp_editor( htmlspecialchars_decode($content), 'wo_blue_box', array("media_buttons" => false) );
}
}

add_action('edit_form_advanced', 'wo_second_editor');

function wo_save_postdata($post_id, $post, $update) {

if (!empty($_POST['wo_blue_box'])) {
$data=htmlspecialchars($_POST['wo_blue_box']);
update_post_meta($post_id, 'wo_blue_box', $data );
}
}

add_action('save_post', 'wo_save_postdata');

// poziom 3

function wo_second_editor2($post) {

global $post, $page;
if ( 'kompetencje' == get_post_type() ){
echo '<h2 style="font-size:20px;margin-top:20px;">Poziom 3.</h2>';
$content = get_post_meta($post->ID, 'wo_blue_box2' , true ) ;
wp_editor( htmlspecialchars_decode($content), 'wo_blue_box2', array("media_buttons" => false) );
}
}

add_action('edit_form_advanced', 'wo_second_editor2');

function wo_save_postdata2($post_id, $post, $update) {

if (!empty($_POST['wo_blue_box2'])) {
$data=htmlspecialchars($_POST['wo_blue_box2']);
update_post_meta($post_id, 'wo_blue_box2', $data );
}
}

add_action('save_post', 'wo_save_postdata2');

/**
* Generated by the WordPress Meta Box Generator at http://goo.gl/8nwllb
*/
class Rational_Meta_Box {
private $screens = array(
'page',
);
private $fields = array(
array(
'id' => 'magda',
'label' => 'Magda',
'type' => 'textarea',
),
array(
'id' => 'martyna',
'label' => 'Martyna',
'type' => 'textarea',
),
);

/**
* Class construct method. Adds actions to their respective WordPress hooks.
*/
public function __construct() {
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
add_action( 'save_post', array( $this, 'save_post' ) );
}

/**
* Hooks into WordPress' add_meta_boxes function.
* Goes through screens (post types) and adds the meta box.
*/
public function add_meta_boxes() {
foreach ( $this->screens as $screen ) {
global $post, $page;
if ( '28' == $post->ID ) {
add_meta_box(
'zesp',
__( 'Zespół', 'zespol' ),
array( $this, 'add_meta_box_callback' ),
$screen,
'advanced',
'default'
);
}
}
}

/**
* Generates the HTML for the meta box
*
* @param object $post WordPress post object
*/
public function add_meta_box_callback( $post ) {
wp_nonce_field( 'zesp_data', 'zesp_nonce' );
$this->generate_fields( $post );
}

/**
* Generates the field's HTML for the meta box.
*/
public function generate_fields( $post ) {
$output = '';
foreach ( $this->fields as $field ) {
$label = '<label for="' . $field['id'] . '">' . $field['label'] . '</label>';
$db_value = get_post_meta( $post->ID, 'zesp_' . $field['id'], true );
switch ( $field['type'] ) {
case 'textarea':
$input = sprintf(
'<textarea class="large-text" id="%s" name="%s" rows="5">%s</textarea>',
$field['id'],
$field['id'],
$db_value
);
break;
default:
$input = sprintf(
'<input %s id="%s" name="%s" type="%s" value="%s">',
$field['type'] !== 'color' ? 'class="regular-text"' : '',
$field['id'],
$field['id'],
$field['type'],
$db_value
);
}
$output .= $this->row_format( $label, $input );
}
echo '<table class="form-table"><tbody>' . $output . '</tbody></table>';
}

/**
* Generates the HTML for table rows.
*/
public function row_format( $label, $input ) {
return sprintf(
'<tr><th scope="row">%s</th><td>%s</td></tr>',
$label,
$input
);
}
/**
* Hooks into WordPress' save_post function
*/
public function save_post( $post_id ) {
if ( ! isset( $_POST['zesp_nonce'] ) )
return $post_id;

$nonce = $_POST['zesp_nonce'];
if ( !wp_verify_nonce( $nonce, 'zesp_data' ) )
return $post_id;

if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;

foreach ( $this->fields as $field ) {
if ( isset( $_POST[ $field['id'] ] ) ) {
switch ( $field['type'] ) {
case 'email':
$_POST[ $field['id'] ] = sanitize_email( $_POST[ $field['id'] ] );
break;
case 'text':
$_POST[ $field['id'] ] = sanitize_text_field( $_POST[ $field['id'] ] );
break;
}
update_post_meta( $post_id, 'zesp_' . $field['id'], $_POST[ $field['id'] ] );
} else if ( $field['type'] === 'checkbox' ) {
update_post_meta( $post_id, 'zesp_' . $field['id'], '0' );
}
}
}
}
new Rational_Meta_Box;