function mycred_display_badge_requirements($post_id = NULL, $sep = '<br />') { $requirements = mycred_get_badge_requirements($post_id); if (empty($requirements)) { $reply = '-'; } else { $types = mycred_get_types(); $references = mycred_get_all_references(); $output = array(); foreach ($requirements as $row => $needs) { if (!isset($types[$needs['type']])) { $point_type = '-'; } else { $point_type = $types[$needs['type']]; } if (!isset($references[$needs['reference']])) { $ref = '-'; } else { $ref = $references[$needs['reference']]; } if ($needs['by'] == 'count') { $output[] = $point_type . ' ' . __('for', 'mycred') . ' ' . $ref . ' ' . $needs['amount'] . ' ' . __('time(s)', 'mycred'); } else { $output[] = $needs['amount'] . ' ' . $point_type . ' ' . __('for', 'mycred') . ' ' . $ref . ' ' . __('in total', 'mycred'); } } $reply = implode($sep, $output) . '.'; } return apply_filters('mycred_badge_display_requirements', $reply, $post_id, $sep); }
function mycred_display_badge_requirements($post_id = NULL, $sep = '<br />') { $requirements = mycred_get_badge_requirements($post_id); if (empty($requirements)) { $reply = '-'; } else { $types = mycred_get_types(); $references = mycred_get_all_references(); $req_count = count($requirements); $output = array(); foreach ($requirements as $level => $needs) { if ($needs['type'] == '') { $needs['type'] = 'mycred_default'; } if (!isset($types[$needs['type']])) { continue; } $mycred = mycred($needs['type']); $point_type = $mycred->plural(); if (!isset($references[$needs['reference']])) { $ref = '-'; } else { $ref = $references[$needs['reference']]; } $level_label = ''; if ($req_count > 1) { $level_label = '<strong>' . sprintf(__('Level %s', 'mycred'), $level + 1) . '</strong>'; } if ($needs['by'] == 'count') { $output[] = sprintf(_x('%s for %s %s - %s', '"Points" for "reference" "x time(s)" - Level', 'mycred'), $point_type, $ref, sprintf(_n('1 time', '%d times', $needs['amount'], 'mycred'), $needs['amount']), $level_label); } else { $output[] = sprintf(_x('%s for %s in total', '"x points" for "reference" in total', 'mycred'), $mycred->format_creds($needs['amount']), $ref); } } $reply = implode($sep, $output); } return apply_filters('mycred_badge_display_requirements', $reply, $post_id, $sep); }
/** * Badge Preferences Metabox * @version 1.1 */ public function metabox_badge_requirements($post) { $requirements = mycred_get_badge_requirements($post->ID, true); $references = mycred_get_all_references(); $default_image = get_post_meta($post->ID, 'default_image', true); $main_image = get_post_meta($post->ID, 'main_image', true); $sums = array('count' => __('Time(s)', 'mycred'), 'sum' => __('In total', 'mycred')); ?> <style type="text/css"> #mycred-remove-badge-connections { float: right; } #mycred-badge-actions { padding-bottom: 12px; margin-bottom: 12px; border-bottom: 1px solid #ccc; } <?php if ($post->post_status != 'publish') { ?> #minor-publishing-actions { padding-bottom: 12px; } <?php } else { ?> #minor-publishing-actions { display: none; } <?php } ?> #misc-publishing-actions { display: none; display: none !important; } table#setup-badge-reqs { width: 100%; } table#setup-badge-reqs tr.bodered-row td { padding-bottom: 12px; border-bottom: 1px solid #dedede; } table#setup-badge-reqs tr.badge-requires td { padding-top: 12px; } table#setup-badge-reqs td.level { width: 15%; color: #aaa; } table#setup-badge-reqs td.type { width: 20%; } table#setup-badge-reqs td.for { width: 5%; } table#setup-badge-reqs td.reference { width: auto; } table#setup-badge-reqs td.amount { width: 10%; } table#setup-badge-reqs td.sum { width: 10%; } #setup-badge-reqs td label { display: block; margin-bottom: 4px; font-weight: bold; } p.actions { text-align: right; } #mycred_badge_images .inside, #mycred_badge_images .inside p { margin: 0; padding: 0; } #image-wrapper { float: none; clear: both; min-height: 100px; } #image-wrapper #main-image { border-bottom: none; } #setup-badge-reqs div.inner-box { float: none; clear: both; min-height: 50px; margin: 0; padding: 0; } #setup-badge-reqs div.inner-box .thumb { display: block; width: <?php echo MYCRED_BADGE_WIDTH; ?> px; height: <?php echo MYCRED_BADGE_HEIGHT; ?> px; padding: 12px; overflow: hidden; } #setup-badge-reqs div.inner-box .thumb img { max-width: <?php echo MYCRED_BADGE_WIDTH; ?> px; height: auto; margin: 0 auto; } </style> <script type="text/javascript"> jQuery(function($) { $( '#postimagediv h3.hndle span' ).empty().text( '<?php echo esc_js(esc_attr__('Badge Image', 'mycred')); ?> ' ); $( '#postimagediv div.inside p a' ).attr( 'title', '<?php echo esc_js(esc_attr__('Set badge image', 'mycred')); ?> ' ).empty().text( '<?php echo esc_js(esc_attr__('Set badge image', 'mycred')); ?> ' ); }); </script> <table class="table" style="width: 100%;" id="setup-badge-reqs"> <?php do_action('mycred_edit_badge_before_req', $post); ?> <tr class="bodered-row"> <td class="level"><div class="inner-box"><div class="thumb"><?php if ($default_image != '') { echo '<img src="' . $default_image . '" alt="" />'; } ?> </div></div></td> <td colspan="5"> <label><?php _e('Default Image', 'mycred'); ?> </label> <input type="text" name="mycred_badge[default_image]" placeholder="<?php _e('image url', 'mycred'); ?> " id="mycred-default-image-url" class="regular-text" size="30" value="<?php echo $default_image; ?> " /> <input type="button" data-target="default-image" id="mycred-add-default-image" class="button button-primary mycred-badge-load-image" value="<?php _e('Add Image', 'mycred'); ?> " /> <p><span class="description"><?php _e('Optional image to show when a user has not yet earned this badge.', 'mycred'); ?> </span></p> </td> </tr> <?php foreach ($requirements as $row => $needs) { // First row if ($row == 0) { if (!isset($needs['by'])) { $needs = array('type' => '', 'reference' => '', 'amount' => '', 'by' => ''); } ?> <tr class="badge-requires" id="badge-requirement-<?php echo $row; ?> "> <td class="level"><?php printf(__('Level %d', 'mycred'), $row + 1); ?> </td> <td class="type"> <?php if (count($this->point_types) == 1) { echo $this->core->plural(); } ?> <?php mycred_types_select_from_dropdown('mycred_badge[req][' . $row . '][type]', 'default-badge-req-type', $needs['type']); ?> </td> <td class="for"><?php _e('for', 'mycred'); ?> </td> <td class="reference"> <select name="mycred_badge[req][<?php echo $row; ?> ][reference]" id="default-badge-req-reference"><?php foreach ($references as $ref => $label) { echo '<option value="' . $ref . '"'; if ($needs['reference'] == $ref) { echo ' selected="selected"'; } echo '>' . $label . '</option>'; } ?> </select> </td> <td class="amount"> <input type="text" size="8" name="mycred_badge[req][<?php echo $row; ?> ][amount]" id="default-badge-req-amount" value="<?php echo $needs['amount']; ?> " /> </td> <td class="sum"> <select name="mycred_badge[req][<?php echo $row; ?> ][by]" id="default-badge-req-by"><?php foreach ($sums as $sum => $label) { echo '<option value="' . $sum . '"'; if ($needs['by'] == $sum) { echo ' selected="selected"'; } echo '>' . $label . '</option>'; } ?> </select> </td> </tr> <tr class="bodered-row"> <td class="level"><div class="inner-box"><div class="thumb"><?php if ($main_image != '') { echo '<img src="' . $main_image . '" alt="" />'; } ?> </div></div></td> <td colspan="5"> <label><?php _e('Main Image', 'mycred'); ?> </label> <input type="text" name="mycred_badge[images][<?php echo $row; ?> ]" placeholder="<?php _e('image url', 'mycred'); ?> " id="mycred-badge-level<?php echo $row; ?> " class="regular-text" size="30" value="<?php echo $main_image; ?> " /> <input type="button" data-target="mycred-badge-level<?php echo $row; ?> " data-row="<?php echo $row; ?> " class="button button-primary mycred-badge-load-image" value="<?php _e('Add Image', 'mycred'); ?> " /> </td> </tr> <?php } else { $default = $requirements[0]; $level_image = get_post_meta($post->ID, 'level_image' . $row, true); ?> <tr class="badge-requires" id="badge-requirement-<?php echo $row; ?> "> <td class="level"> <div><?php printf(__('Level %d', 'mycred'), $row + 1); ?> </div> </td> <td class="type"> <div><?php echo $this->point_types[$default['type']]; ?> </div> </td> <td class="for"><?php _e('for', 'mycred'); ?> </td> <td class="reference"><div><?php echo $references[$default['reference']]; ?> </div></td> <td class="amount"> <input type="text" size="8" name="mycred_badge[req][<?php echo $row; ?> ][amount]" value="<?php echo $needs['amount']; ?> " /> </td> <td class="sum"><div><?php echo $sums[$default['by']]; ?> </div></td> </tr> <tr class="bodered-row" id="mycred-badge-image<?php echo $row; ?> "> <td class="level"><div class="inner-box"><div class="thumb"><?php if ($level_image != '') { echo '<img src="' . $level_image . '" alt="" />'; } ?> </div></div></td> <td colspan="5"> <label><?php _e('Badge Image', 'mycred'); ?> </label> <input type="text" name="mycred_badge[images][<?php echo $row; ?> ]" placeholder="<?php _e('image url', 'mycred'); ?> " id="mycred-badge-level<?php echo $row; ?> " class="regular-text" size="30" value="<?php echo $level_image; ?> " /> <input type="button" data-target="mycred-badge-level<?php echo $row; ?> " data-row="<?php echo $row; ?> " class="button button-primary mycred-badge-load-image" value="<?php _e('Add Image', 'mycred'); ?> " /> <p><span class="description"><?php _e('Leave empty if you do not want to assign a custom image for this level.', 'mycred'); ?> </span><button class="button button-secondary button-small pull-right remove-this-row"><?php _e('Remove this level', 'mycred'); ?> </button></p> </td> </tr> <?php } } ?> <?php do_action('mycred_edit_badge_after_req', $post); ?> </table> <p><button id="add-mycred-badge-level" class="pull-right button button-secondary"><?php _e('Add Level', 'mycred'); ?> </button></p> <script type="text/javascript"> (function($) { $( 'select#default-badge-req-type' ).change(function(){ $( 'td.type div' ).empty().text( $(this).find( ':selected' ).text() ); }); $( '#default-badge-req-reference' ).change(function(){ $( 'td.reference div' ).empty().text( $(this).find( ':selected' ).text() ); }); $( 'select#default-badge-req-by' ).change(function(){ $( 'td.sum div' ).empty().text( $(this).find( ':selected' ).text() ); }); var custom_uploader; var rows = <?php echo count($requirements); ?> ; $( 'table#setup-badge-reqs' ).on( 'click', 'button.remove-this-row', function(){ var trrow = $(this).parent().parent().parent(); trrow.prev().remove(); trrow.remove(); rows = rows-1; }); $( '#add-mycred-badge-level' ).on( 'click', function(e){ e.preventDefault(); var badgetype = $( '#default-badge-req-type' ); var badgeref = $( '#default-badge-req-reference' ).find( ':selected' ).text(); var badgeamount = $( '#default-badge-req-amount' ); var badgeby = $( '#default-badge-req-by' ).find( ':selected' ).text(); var reqtemplate = '<tr class="badge-requires" id="badge-requirement-' + rows + '"><td class="level"><?php echo esc_js(esc_attr__('Level', 'mycred')); ?> ' + ( rows + 1 ) + '</td><td class="type"><div>' + badgetype.find( ':selected' ).text() + '</div></td><td class="for"><?php echo esc_js(esc_attr__('for', 'mycred')); ?> </td><td class="reference"><div>' + badgeref + '</div></td><td class="amount"><input type="text" size="8" name="mycred_badge[req][' + rows + '][amount]" value="" /></td><td class="sum"><div>' + badgeby + '</div></td></tr><tr class="bodered-row"><td class="level"><div class="inner-box"><div class="thumb"></div></div></td><td colspan="5"><label><?php echo esc_js(esc_attr__('Badge Image', 'mycred')); ?> </label><input type="text" name="mycred_badge[images][' + rows + ']" placeholder="<?php echo esc_js(esc_attr__('image url', 'mycred')); ?> " id="mycred-badge-level' + rows + '" class="regular-text" size="30" value="" /> <input type="button" data-target="mycred-badge-level' + rows + '" data-row="' + rows + '" class="button button-primary mycred-badge-load-image" value="<?php echo esc_js(esc_attr__('Add Image', 'mycred')); ?> " /><p><span class="description"><?php echo esc_js(esc_attr__('Leave empty if you do not want to assign a custom image for this level.', 'mycred')); ?> </span><br /><button class="button button-secondary button-small pull-right remove-this-row"><?php echo esc_js(esc_attr__('Remove this level', 'mycred')); ?> </button></p></td></tr>'; $( 'table#setup-badge-reqs' ).append( reqtemplate ); rows = rows+1; }); $( 'table#setup-badge-reqs' ).on( 'click', '.mycred-badge-load-image', function(e){ e.preventDefault(); var imagerow = $(this).attr( 'data-target' ); var button = $(this); //Extend the wp.media object custom_uploader = wp.media.frames.file_frame = wp.media({ title : '<?php echo esc_js(esc_attr__('Badge Image', 'mycred')); ?> ', button : { text : '<?php echo esc_js(esc_attr__('Use as Badge', 'mycred')); ?> ' }, multiple : false }); //When a file is selected, grab the URL and set it as the text field's value custom_uploader.on( 'select', function(){ attachment = custom_uploader.state().get('selection').first().toJSON(); if ( attachment.url != '' ) { console.log( attachment ); button.prev().val( attachment.url ); button.parent().prev().find( 'div.thumb' ).empty().append( "<img class='show-selected-image' src='" + attachment.url + "' alt='' />" ); } }); //Open the uploader dialog custom_uploader.open(); }); })( jQuery ); </script> <div class="clear clearfix"></div> <?php }
/** * Badge Preferences Metabox * @version 1.0 */ public function metabox_badge_requirements($post) { $requirements = mycred_get_badge_requirements($post->ID, true); $types = mycred_get_types(); $references = mycred_get_all_references(); $sums = array('count' => __('Time(s)', 'mycred'), 'sum' => __('In total', 'mycred')); ?> <style type="text/css"> #mycred-remove-badge-connections { float: right; } #mycred-badge-actions { padding-bottom: 12px; margin-bottom: 12px; border-bottom: 1px solid #ccc; } <?php if ($post->post_status != 'publish') { ?> #minor-publishing-actions { padding-bottom: 12px; } <?php } else { ?> #minor-publishing-actions { display: none; } <?php } ?> #misc-publishing-actions { display: none; display: none !important; } table#setup-badge-reqs { width: 100%; } table#setup-badge-reqs td.type { width: 25%; } table#setup-badge-reqs td.for { width: 5%; } table#setup-badge-reqs td.reference { width: 50%; } table#setup-badge-reqs td.amount { width: 10%; } table#setup-badge-reqs td.sum { width: 10%; } p.actions { text-align: right; } </style> <script type="text/javascript"> jQuery(function($) { $( '#postimagediv h3.hndle span' ).empty().text( '<?php _e('Badge Image', 'mycred'); ?> ' ); $( '#postimagediv div.inside p a' ).attr( 'title', '<?php _e('Set badge image', 'mycred'); ?> ' ).empty().text( '<?php _e('Set badge image', 'mycred'); ?> ' ); }); </script> <p>To earn this badge, a user must have received:</p> <table class="table" style="width: 100%;" id="setup-badge-reqs"> <?php do_action('mycred_edit_badge_before_req', $post); ?> <?php foreach ($requirements as $row => $needs) { if (!isset($needs['by'])) { $needs = array('type' => '', 'reference' => '', 'amount' => '', 'by' => ''); } ?> <tr class="badge-requires" id="badge-requirement-<?php echo $row; ?> "> <td class="type"> <?php mycred_types_select_from_dropdown('mycred_badge[req][' . $row . '][type]', '', $needs['type']); ?> </td> <td class="for">for</td> <td class="reference"> <select name="mycred_badge[req][<?php echo $row; ?> ][reference]" id=""><?php foreach ($references as $ref => $label) { echo '<option value="' . $ref . '"'; if ($needs['reference'] == $ref) { echo ' selected="selected"'; } echo '>' . $label . '</option>'; } ?> </select> </td> <td class="amount"> <input type="text" size="8" name="mycred_badge[req][<?php echo $row; ?> ][amount]" id="" value="<?php echo $needs['amount']; ?> " /> </td> <td class="sum"> <select name="mycred_badge[req][<?php echo $row; ?> ][by]" id=""><?php foreach ($sums as $sum => $label) { echo '<option value="' . $sum . '"'; if ($needs['by'] == $sum) { echo ' selected="selected"'; } echo '>' . $label . '</option>'; } ?> </select> </td> </tr> <?php } ?> <?php do_action('mycred_edit_badge_after_req', $post); ?> </table> <?php }